@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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