Open History to see the last commit
1import { useRef } from "react";
2import { BranchNamingMode, DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
3
4import { Input } from "../ui/input";
5import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
6import { Textarea } from "../ui/textarea";
7import { SettingsRow, SettingResetButton } from "./settingsLayout";
8import { useSettingsScope } from "./SettingsScopeContext";
9import { searchableSetting } from "./settingsSearch";
10import {
11 useScopedSettings,
12 useScopedSettingsMixed,
13 useUpdateScopedSettings,
14} from "./useScopedSettings";
15
16const MODES = {
17 static: "Static prefix",
18 semantic: "Semantic prefix",
19 custom: "Custom instructions",
20} satisfies Record<BranchNamingMode, string>;
21
22export function BranchNamingSettings() {
23 const settings = useScopedSettings();
24 const { targets } = useSettingsScope();
25 const scopeKey = targets.map((target) => `${target.environmentId}:${target.projectId}`).join(",");
26 const prefixEdited = useRef(false);
27 const instructionsEdited = useRef(false);
28 const updateSettings = useUpdateScopedSettings();
29 const modeMixed = useScopedSettingsMixed(["branchNamingMode"]);
30 const prefixMixed = useScopedSettingsMixed(["branchNamePrefix"]);
31 const instructionsMixed = useScopedSettingsMixed(["branchNameInstructions"]);
32
33 return (
34 <>
35 <SettingsRow
36 serverScoped
37 settingKeys={["branchNamingMode"]}
38 {...searchableSetting("worktree-branch-naming")}
39 description="Choose how new worktree branches are named from your first message."
40 resetAction={
41 settings.branchNamingMode !== DEFAULT_SERVER_SETTINGS.branchNamingMode || modeMixed ? (
42 <SettingResetButton
43 label="branch naming"
44 onClick={() =>
45 updateSettings({ branchNamingMode: DEFAULT_SERVER_SETTINGS.branchNamingMode })
46 }
47 />
48 ) : null
49 }
50 control={
51 <Select
52 value={modeMixed ? null : settings.branchNamingMode}
53 onValueChange={(value) => {
54 if (BranchNamingMode.literals.includes(value as BranchNamingMode)) {
55 updateSettings({ branchNamingMode: value as BranchNamingMode });
56 }
57 }}
58 >
59 <SelectTrigger size="sm" aria-label="Worktree branch naming">
60 <SelectValue>
61 {(value: BranchNamingMode | null) => (value === null ? "Mixed" : MODES[value])}
62 </SelectValue>
63 </SelectTrigger>
64 <SelectPopup align="end" alignItemWithTrigger={false}>
65 {BranchNamingMode.literals.map((mode) => (
66 <SelectItem key={mode} value={mode}>
67 {MODES[mode]}
68 </SelectItem>
69 ))}
70 </SelectPopup>
71 </Select>
72 }
73 />
74 {!modeMixed && settings.branchNamingMode === "static" ? (
75 <SettingsRow
76 serverScoped
77 settingKeys={["branchNamePrefix"]}
78 title="Branch prefix"
79 description="For example, t3code or t3code/ produces t3code/add-search. Leave empty for no prefix."
80 resetAction={
81 prefixMixed ||
82 settings.branchNamePrefix !== DEFAULT_SERVER_SETTINGS.branchNamePrefix ? (
83 <SettingResetButton
84 label="branch prefix"
85 onClick={() =>
86 updateSettings({ branchNamePrefix: DEFAULT_SERVER_SETTINGS.branchNamePrefix })
87 }
88 />
89 ) : null
90 }
91 control={
92 <Input
93 key={`${scopeKey}:${prefixMixed}:${settings.branchNamePrefix}`}
94 aria-label="Branch prefix"
95 autoCapitalize="none"
96 spellCheck={false}
97 onChange={() => {
98 prefixEdited.current = true;
99 }}
100 placeholder={prefixMixed ? "Mixed" : "No prefix"}
101 defaultValue={prefixMixed ? "" : settings.branchNamePrefix}
102 onBlur={(event) => {
103 const value = event.target.value.trim();
104 if (prefixEdited.current && (prefixMixed || value !== settings.branchNamePrefix))
105 updateSettings({ branchNamePrefix: value });
106 prefixEdited.current = false;
107 }}
108 />
109 }
110 />
111 ) : null}
112 {!modeMixed && settings.branchNamingMode === "semantic" ? (
113 <p className="pb-3 text-sm text-muted-foreground">
114 The model chooses a prefix that describes the work, such as feat/add-search,
115 fix/login-timeout, or refactor/auth.
116 </p>
117 ) : null}
118 {!modeMixed && settings.branchNamingMode === "custom" ? (
119 <SettingsRow
120 serverScoped
121 settingKeys={["branchNameInstructions"]}
122 title="Branch naming instructions"
123 description="Appended to the naming prompt. The model returns the complete branch name; no prefix or suffix is added."
124 resetAction={
125 instructionsMixed || settings.branchNameInstructions !== "" ? (
126 <SettingResetButton
127 label="branch naming instructions"
128 onClick={() => updateSettings({ branchNameInstructions: "" })}
129 />
130 ) : null
131 }
132 >
133 <div className="mt-3 max-w-2xl pb-3.5">
134 <Textarea
135 key={`${scopeKey}:${instructionsMixed}:${settings.branchNameInstructions}`}
136 aria-label="Branch naming instructions"
137 onChange={() => {
138 instructionsEdited.current = true;
139 }}
140 rows={4}
141 defaultValue={instructionsMixed ? "" : settings.branchNameInstructions}
142 placeholder={
143 instructionsMixed
144 ? "Mixed. Enter instructions to apply to all selected targets."
145 : "Use julius/ followed by the issue ID and a short description."
146 }
147 onBlur={(event) => {
148 const value = event.target.value.trim();
149 if (
150 instructionsEdited.current &&
151 (instructionsMixed || value !== settings.branchNameInstructions)
152 )
153 updateSettings({ branchNameInstructions: value });
154 instructionsEdited.current = false;
155 }}
156 />
157 </div>
158 </SettingsRow>
159 ) : null}
160 </>
161 );
162}