Open History to see the last commit
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 { SettingsChoiceRow } from "./SettingsChoiceRow";
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 disabled: boolean;
26 onChange: (patch: ServerSettingsPatch) => void;
27}) {
28 // Typed text waits here until it saves. Android's Back closes the keyboard
29 // without ending editing, so leaving the screen saves it too.
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 // Takes a field's draft as a patch, or {} when there is nothing new to save.
38 // A newer saved value remounts the input, so a draft typed over an older one
39 // is no longer on screen and is dropped.
40 const take = (field: Field): ServerSettingsPatch => {
41 const draft = drafts.current[field];
42 delete drafts.current[field];
43 const saved = savedValue(field);
44 // Not gated on `disabled`: a pending write disables the inputs, and the
45 // route queues this save behind it and checks write access itself.
46 if (draft === undefined || draft.over !== saved) return {};
47 const value = draft.text.trim();
48 return saved === null || value !== saved ? { [field]: value } : {};
49 };
50 const send = (patch: ServerSettingsPatch) => {
51 if (Object.keys(patch).length > 0) props.onChange(patch);
52 };
53 // One patch, because the screen drops writes while another is in flight.
54 const saveDrafts = useEffectEvent(() =>
55 send({ ...take("branchNamePrefix"), ...take("branchNameInstructions") }),
56 );
57 useEffect(() => () => saveDrafts(), []);
58 return (
59 <SettingsSection
60 title="Worktree branch naming"
61 trailing={
62 props.mode === null ? <Text className="text-xs text-foreground-muted">Mixed</Text> : null
63 }
64 >
65 {BranchNamingMode.literals.map((mode, index) => (
66 <SettingsChoiceRow
67 key={mode}
68 label={MODES[mode].label}
69 description={MODES[mode].description}
70 selected={props.mode === mode}
71 separated={index > 0}
72 disabled={props.disabled}
73 onPress={() => props.onChange({ branchNamingMode: mode })}
74 />
75 ))}
76 {props.mode === "static" ? (
77 <View className="gap-2 px-4 py-3">
78 <Text className="text-sm text-foreground-muted">
79 Use t3 or t3/ for t3/add-search. Leave empty for no prefix.
80 </Text>
81 <AppTextInput
82 key={props.prefix}
83 accessibilityLabel="Branch prefix"
84 onChangeText={(text) => edit("branchNamePrefix", text)}
85 defaultValue={props.prefix ?? ""}
86 placeholder={props.prefix === null ? "Mixed" : "No prefix"}
87 editable={!props.disabled}
88 autoCapitalize="none"
89 autoCorrect={false}
90 className="min-h-10 rounded-xl px-3 py-2 text-base text-foreground"
91 onEndEditing={() => send(take("branchNamePrefix"))}
92 />
93 </View>
94 ) : null}
95 {props.mode === "custom" ? (
96 <View className="gap-2 px-4 py-3">
97 <Text className="text-sm text-foreground-muted">
98 Append instructions to the naming prompt.
99 </Text>
100 <AppTextInput
101 key={props.instructions}
102 accessibilityLabel="Branch naming instructions"
103 onChangeText={(text) => edit("branchNameInstructions", text)}
104 defaultValue={props.instructions ?? ""}
105 placeholder={
106 props.instructions === null
107 ? "Mixed. Enter instructions for all selected targets."
108 : "Use julius/ followed by the issue ID and a short description."
109 }
110 editable={!props.disabled}
111 multiline
112 autoCapitalize="sentences"
113 className="min-h-24 rounded-xl px-3 py-2 text-base text-foreground"
114 onEndEditing={() => send(take("branchNameInstructions"))}
115 />
116 </View>
117 ) : null}
118 </SettingsSection>
119 );
120}