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 { 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 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 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- saves unsent branch naming drafts to server settings when the section unmounts
54 useEffect(() => () => saveDrafts(), []);
55 return (
56 <SettingsSection
57 title="Worktree branch naming"
58 trailing={
59 props.mode === null ? <Text className="text-xs text-foreground-muted">Mixed</Text> : null
60 }
61 >
62 {BranchNamingMode.literals.map((mode, index) => (
63 <SettingsChoiceRow
64 key={mode}
65 label={MODES[mode].label}
66 description={MODES[mode].description}
67 selected={props.mode === mode}
68 separated={index > 0}
69 disabled={props.disabled}
70 onPress={() => props.onChange({ branchNamingMode: mode })}
71 />
72 ))}
73 {props.mode === "static" ? (
74 <View className="gap-2 px-4 py-3">
75 <Text className="text-sm text-foreground-muted">
76 Use t3 or t3/ for t3/add-search. Leave empty for no prefix.
77 </Text>
78 <AppTextInput
79 key={props.prefix}
80 accessibilityLabel="Branch prefix"
81 onChangeText={(text) => edit("branchNamePrefix", text)}
82 defaultValue={props.prefix ?? ""}
83 placeholder={props.prefix === null ? "Mixed" : "No prefix"}
84 editable={!props.disabled}
85 autoCapitalize="none"
86 autoCorrect={false}
87 className="min-h-10 rounded-xl px-3 py-2 text-base text-foreground"
88 onEndEditing={() => send(take("branchNamePrefix"))}
89 />
90 </View>
91 ) : null}
92 {props.mode === "custom" ? (
93 <View className="gap-2 px-4 py-3">
94 <Text className="text-sm text-foreground-muted">
95 Append instructions to the naming prompt.
96 </Text>
97 <AppTextInput
98 key={props.instructions}
99 accessibilityLabel="Branch naming instructions"
100 onChangeText={(text) => edit("branchNameInstructions", text)}
101 defaultValue={props.instructions ?? ""}
102 placeholder={
103 props.instructions === null
104 ? "Mixed. Enter instructions for all selected targets."
105 : "Use julius/ followed by the issue ID and a short description."
106 }
107 editable={!props.disabled}
108 multiline
109 autoCapitalize="sentences"
110 className="min-h-24 rounded-xl px-3 py-2 text-base text-foreground"
111 onEndEditing={() => send(take("branchNameInstructions"))}
112 />
113 </View>
114 ) : null}
115 </SettingsSection>
116 );
117}