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 // Save on exit too: Android Back hides the keyboard without ending editing.
29 type Field = "branchNamePrefix" | "branchNameInstructions";
30 const drafts = useRef<Partial<Record<Field, { text: string; over: string | null }>>>({});
31 const savedValue = (field: Field) =>
32 field === "branchNamePrefix" ? props.prefix : props.instructions;
33 const edit = (field: Field, text: string) => {
34 drafts.current[field] = { text, over: savedValue(field) };
35 };
36 // Drop a draft typed over an older saved value: the updated value has already remounted its input.
37 const take = (field: Field): ServerSettingsPatch => {
38 const draft = drafts.current[field];
39 delete drafts.current[field];
40 const saved = savedValue(field);
41 // Save even while disabled; the route queues behind pending writes and checks access.
42 if (draft === undefined || draft.over !== saved) return {};
43 const value = draft.text.trim();
44 return saved === null || value !== saved ? { [field]: value } : {};
45 };
46 const send = (patch: ServerSettingsPatch) => {
47 if (Object.keys(patch).length > 0) props.onChange(patch);
48 };
49 // One patch, because the screen drops writes while another is in flight.
50 const saveDrafts = useEffectEvent(() =>
51 send({ ...take("branchNamePrefix"), ...take("branchNameInstructions") }),
52 );
53 useEffect(() => () => saveDrafts(), []);
54 return (
55 <SettingsSection
56 title="Worktree branch naming"
57 trailing={
58 props.mode === null ? <Text className="text-xs text-foreground-muted">Mixed</Text> : null
59 }
60 >
61 {BranchNamingMode.literals.map((mode, index) => (
62 <SettingsChoiceRow
63 key={mode}
64 label={MODES[mode].label}
65 description={MODES[mode].description}
66 selected={props.mode === mode}
67 separated={index > 0}
68 disabled={props.disabled}
69 onPress={() => props.onChange({ branchNamingMode: mode })}
70 />
71 ))}
72 {props.mode === "static" ? (
73 <View className="gap-2 px-4 py-3">
74 <Text className="text-sm text-foreground-muted">
75 Use t3 or t3/ for t3/add-search. Leave empty for no prefix.
76 </Text>
77 <AppTextInput
78 key={props.prefix}
79 accessibilityLabel="Branch prefix"
80 onChangeText={(text) => edit("branchNamePrefix", text)}
81 defaultValue={props.prefix ?? ""}
82 placeholder={props.prefix === null ? "Mixed" : "No prefix"}
83 editable={!props.disabled}
84 autoCapitalize="none"
85 autoCorrect={false}
86 className="min-h-10 rounded-xl px-3 py-2 text-base text-foreground"
87 onEndEditing={() => send(take("branchNamePrefix"))}
88 />
89 </View>
90 ) : null}
91 {props.mode === "custom" ? (
92 <View className="gap-2 px-4 py-3">
93 <Text className="text-sm text-foreground-muted">
94 Append instructions to the naming prompt.
95 </Text>
96 <AppTextInput
97 key={props.instructions}
98 accessibilityLabel="Branch naming instructions"
99 onChangeText={(text) => edit("branchNameInstructions", text)}
100 defaultValue={props.instructions ?? ""}
101 placeholder={
102 props.instructions === null
103 ? "Mixed. Enter instructions for all selected targets."
104 : "Use julius/ followed by the issue ID and a short description."
105 }
106 editable={!props.disabled}
107 multiline
108 autoCapitalize="sentences"
109 className="min-h-24 rounded-xl px-3 py-2 text-base text-foreground"
110 onEndEditing={() => send(take("branchNameInstructions"))}
111 />
112 </View>
113 ) : null}
114 </SettingsSection>
115 );
116}