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