Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BranchNamingSettings.tsx

118 lines5.0 KB

1import { BranchNamingMode, type ServerSettingsPatch } from "@t3tools/contracts";
2import { useEffect, useEffectEvent, useRef } from "react";
3import { View } from "react-native";
4​
5import { AppText as Text, AppTextInput } from "../../../components/ui/AppText";
6import { ListChoiceRow as SettingsChoiceRow } from "../../../components/ui/ListChoiceRow";
7import { SettingsSection } from "./SettingsSection";
8​
9const MODES = {
10 static: { label: "Static prefix", description: "Add your prefix to the generated branch name." },
11 semantic: {
12 label: "Semantic prefix",
13 description: "Let the model choose feat/, fix/, refactor/, or another prefix.",
14 },
15 custom: {
16 label: "Custom instructions",
17 description: "Generate the complete name with no added prefix or suffix.",
18 },
19} satisfies Record<BranchNamingMode, { label: string; description: string }>;
20​
21export function BranchNamingSettings(props: {
22 mode: BranchNamingMode | null;
23 prefix: string | null;
24 instructions: string | null;
25 /** Per setting, since a policy can lock one and leave the others to the user. */
26 disabled: Readonly<Record<"mode" | "prefix" | "instructions", boolean>>;
27 onChange: (patch: ServerSettingsPatch) => void;
28}) {
29 // Save on exit too: Android Back hides the keyboard without ending editing.
30 type Field = "branchNamePrefix" | "branchNameInstructions";
31 const drafts = useRef<Partial<Record<Field, { text: string; over: string | null }>>>({});
32 const savedValue = (field: Field) =>
33 field === "branchNamePrefix" ? props.prefix : props.instructions;
34 const edit = (field: Field, text: string) => {
35 drafts.current[field] = { text, over: savedValue(field) };
36 };
37 // Drop a draft typed over an older saved value: the updated value has already remounted its input.
38 const take = (field: Field): ServerSettingsPatch => {
39 const draft = drafts.current[field];
40 delete drafts.current[field];
41 const saved = savedValue(field);
42 // Save even while disabled; the route queues behind pending writes and checks access.
43 if (draft === undefined || draft.over !== saved) return {};
44 const value = draft.text.trim();
45 return saved === null || value !== saved ? { [field]: value } : {};
46 };
47 const send = (patch: ServerSettingsPatch) => {
48 if (Object.keys(patch).length > 0) props.onChange(patch);
49 };
50 // One patch, because the screen drops writes while another is in flight.
51 const saveDrafts = useEffectEvent(() =>
52 send({ ...take("branchNamePrefix"), ...take("branchNameInstructions") }),
53 );
54 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- saves unsent branch naming drafts to server settings when the section unmounts
55 useEffect(() => () => saveDrafts(), []);
56 return (
57 <SettingsSection
58 title="Worktree branch naming"
59 trailing={
60 props.mode === null ? <Text className="text-xs text-foreground-muted">Mixed</Text> : null
61 }
62 >
63 {BranchNamingMode.literals.map((mode, index) => (
64 <SettingsChoiceRow
65 key={mode}
66 label={MODES[mode].label}
67 description={MODES[mode].description}
68 selected={props.mode === mode}
69 separated={index > 0}
70 disabled={props.disabled.mode}
71 onPress={() => props.onChange({ branchNamingMode: mode })}
72 />
73 ))}
74 {props.mode === "static" ? (
75 <View className="gap-2 px-4 py-3">
76 <Text className="text-sm text-foreground-muted">
77 Use t3 or t3/ for t3/add-search. Leave empty for no prefix.
78 </Text>
79 <AppTextInput
80 key={props.prefix}
81 accessibilityLabel="Branch prefix"
82 onChangeText={(text) => edit("branchNamePrefix", text)}
83 defaultValue={props.prefix ?? ""}
84 placeholder={props.prefix === null ? "Mixed" : "No prefix"}
85 editable={!props.disabled.prefix}
86 autoCapitalize="none"
87 autoCorrect={false}
88 className="min-h-10 rounded-xl px-3 py-2 text-base text-foreground"
89 onEndEditing={() => send(take("branchNamePrefix"))}
90 />
91 </View>
92 ) : null}
93 {props.mode === "custom" ? (
94 <View className="gap-2 px-4 py-3">
95 <Text className="text-sm text-foreground-muted">
96 Append instructions to the naming prompt.
97 </Text>
98 <AppTextInput
99 key={props.instructions}
100 accessibilityLabel="Branch naming instructions"
101 onChangeText={(text) => edit("branchNameInstructions", text)}
102 defaultValue={props.instructions ?? ""}
103 placeholder={
104 props.instructions === null
105 ? "Mixed. Enter instructions for all selected targets."
106 : "Use julius/ followed by the issue ID and a short description."
107 }
108 editable={!props.disabled.instructions}
109 multiline
110 autoCapitalize="sentences"
111 className="min-h-24 rounded-xl px-3 py-2 text-base text-foreground"
112 onEndEditing={() => send(take("branchNameInstructions"))}
113 />
114 </View>
115 ) : null}
116 </SettingsSection>
117 );