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

main
tsx 501 lines 19.9 KB
Raw
1 import type { CSSProperties } from "react";
2 import { useState } from "react";
3 import { msg, useGT, useMessages } from "gt-react";
4 import {
5 DEFAULT_ENVIRONMENT_IDENTIFICATION_MODE,
6 DEFAULT_UNIFIED_SETTINGS,
7 type ChatWidth,
8 type EffortDisplay,
9 type EnvironmentIdentificationMode,
10 MAX_APPEARANCE_CONTRAST,
11 MAX_GLASS_OPACITY,
12 MAX_PANEL_ANIMATION_DURATION_MS,
13 MIN_APPEARANCE_CONTRAST,
14 MIN_GLASS_OPACITY,
15 MIN_PANEL_ANIMATION_DURATION_MS,
16 } from "@t3tools/contracts/settings";
17 import {
18 resolveSidebarStageBackdropVariant,
19 type SidebarStageBackdropVariant,
20 useEnvironmentStageLabel,
21 } from "../SidebarStageBackdrop";
22 import { useCustomThemes } from "../../hooks/useCustomThemes";
23 import { useEnvironmentIdentificationMode } from "../../hooks/useSettings";
24 import { useTheme } from "../../hooks/useTheme";
25 import { useScopedSettings, useUpdateScopedSettings } from "./useScopedSettings";
26 import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
27 import { Switch } from "../ui/switch";
28 import { ThemeLibrary } from "./ThemeSettings";
29 import {
30 SettingResetButton,
31 SettingsPageContainer,
32 SettingsRow,
33 SettingsSection,
34 } from "./settingsLayout";
35 import { useSearchableSetting } from "./settingsSearch";
36 import { AdvancedSettings } from "./AdvancedSettings";
37 import { PanelAnimationsPreview } from "./PanelAnimationsPreview";
38 import { TypographySection } from "./TypographySettings";
39 import { ThreadDetailsItemsRow } from "./ThreadDetailsItemsRow";
40
41 const ENVIRONMENT_IDENTIFICATION_LABELS: Record<EnvironmentIdentificationMode, string> = {
42 artwork: msg("Artwork"),
43 "nightly-artwork": msg("Nightly artwork"),
44 "dev-artwork": msg("Dev artwork"),
45 pill: msg("Version pill"),
46 none: msg("None"),
47 };
48
49 // The default "artwork" follows the running channel, so it is labeled as that channel's art,
50 // or as "None" on Latest. The other channel's art is offered as a fixed choice.
51 function environmentIdentificationOptions(
52 stageVariant: SidebarStageBackdropVariant | null,
53 ): ReadonlyArray<readonly [EnvironmentIdentificationMode, string]> {
54 if (stageVariant === "dev") {
55 return [
56 ["artwork", msg("Dev artwork")],
57 ["nightly-artwork", msg("Nightly artwork")],
58 ["pill", msg("Version pill")],
59 ["none", msg("None")],
60 ];
61 }
62 if (stageVariant === "nightly") {
63 return [
64 ["artwork", msg("Nightly artwork")],
65 ["dev-artwork", msg("Dev artwork")],
66 ["pill", msg("Version pill")],
67 ["none", msg("None")],
68 ];
69 }
70 return [
71 ["artwork", msg("None")],
72 ["nightly-artwork", msg("Nightly artwork")],
73 ["dev-artwork", msg("Dev artwork")],
74 ];
75 }
76
77 const CHAT_WIDTH_LABELS: Record<ChatWidth, string> = {
78 comfortable: msg("Comfortable"),
79 wide: msg("Wide"),
80 full: msg("Full", { $context: "chat width" }),
81 };
82
83 const EFFORT_DISPLAY_LABELS: Record<EffortDisplay, string> = {
84 label: msg("Word", { $context: "show effort as a word" }),
85 bars: msg("Bars", { $context: "show effort as bars" }),
86 };
87
88 export function AppearanceSettingsPanel() {
89 const searchableSetting = useSearchableSetting();
90 const {
91 appearanceMode,
92 refreshTheme,
93 resolvedTheme,
94 setAppearanceMode,
95 setTheme,
96 setThemeHalf,
97 theme,
98 themeHalves,
99 } = useTheme();
100 const gt = useGT();
101 const m = useMessages();
102 const customThemes = useCustomThemes();
103 const [isImportThemeOpen, setIsImportThemeOpen] = useState(false);
104 const settings = useScopedSettings();
105 const updateSettings = useUpdateScopedSettings();
106 const environmentStageLabel = useEnvironmentStageLabel();
107 const environmentStageVariant = resolveSidebarStageBackdropVariant(environmentStageLabel);
108 const environmentIdentificationOptionList =
109 environmentIdentificationOptions(environmentStageVariant);
110 const environmentIdentificationLabel = m(
111 environmentIdentificationOptionList.find(
112 ([value]) => value === settings.environmentIdentificationMode,
113 )?.[1] ?? ENVIRONMENT_IDENTIFICATION_LABELS[settings.environmentIdentificationMode],
114 );
115 const effectiveEnvironmentIdentificationMode = useEnvironmentIdentificationMode();
116 const themeHidesEnvironmentArtwork =
117 effectiveEnvironmentIdentificationMode === "pill" &&
118 resolveSidebarStageBackdropVariant(
119 environmentStageLabel,
120 settings.environmentIdentificationMode,
121 ) !== null;
122 const environmentIdentificationDescription = themeHidesEnvironmentArtwork
123 ? environmentStageVariant
124 ? gt("Only built-in themes show artwork, so this theme shows the version pill.")
125 : gt("Only built-in themes show artwork.")
126 : environmentStageVariant
127 ? gt("Choose how Dev and Nightly environments are identified.")
128 : gt("Show Dev or Nightly artwork in the sidebar header.");
129 const glassOpacityRatio =
130 (settings.glassOpacity - MIN_GLASS_OPACITY) / (MAX_GLASS_OPACITY - MIN_GLASS_OPACITY);
131 const glassOpacitySliderStyle = {
132 "--settings-slider-progress": `${glassOpacityRatio * 100}%`,
133 "--settings-slider-fill-offset": `${0.5 - glassOpacityRatio}rem`,
134 } as CSSProperties;
135 const appearanceContrastRatio =
136 (settings.appearanceContrast - MIN_APPEARANCE_CONTRAST) /
137 (MAX_APPEARANCE_CONTRAST - MIN_APPEARANCE_CONTRAST);
138 const appearanceContrastSliderStyle = {
139 "--settings-slider-progress": `${appearanceContrastRatio * 100}%`,
140 "--settings-slider-fill-offset": `${0.5 - appearanceContrastRatio}rem`,
141 } as CSSProperties;
142 const panelAnimationDurationRatio =
143 (settings.panelAnimationDurationMs - MIN_PANEL_ANIMATION_DURATION_MS) /
144 (MAX_PANEL_ANIMATION_DURATION_MS - MIN_PANEL_ANIMATION_DURATION_MS);
145 const panelAnimationDurationSliderStyle = {
146 "--settings-slider-progress": `${panelAnimationDurationRatio * 100}%`,
147 "--settings-slider-fill-offset": `${0.5 - panelAnimationDurationRatio}rem`,
148 } as CSSProperties;
149
150 return (
151 <SettingsPageContainer>
152 <SettingsSection id="appearance" title={gt("Colors & themes")} variant="plain" hideTitle>
153 <div id={searchableSetting("theme").id}>
154 <ThemeLibrary
155 appearanceMode={appearanceMode}
156 customThemes={customThemes}
157 initialAppearance={resolvedTheme}
158 refreshTheme={refreshTheme}
159 isImportOpen={isImportThemeOpen}
160 setAppearanceMode={setAppearanceMode}
161 setTheme={setTheme}
162 setThemeHalf={setThemeHalf}
163 theme={theme}
164 themeHalves={themeHalves}
165 onImportOpenChange={setIsImportThemeOpen}
166 />
167 </div>
168 </SettingsSection>
169
170 <SettingsSection id="appearance-interface" title={gt("Interface")}>
171 <SettingsRow
172 {...searchableSetting("setting-appearance-contrast")}
173 description={gt("Adjust the contrast of colors and borders across the interface.")}
174 resetAction={
175 settings.appearanceContrast !== DEFAULT_UNIFIED_SETTINGS.appearanceContrast ? (
176 <SettingResetButton
177 ariaLabel={gt("Reset contrast to default")}
178 onClick={() =>
179 updateSettings({
180 appearanceContrast: DEFAULT_UNIFIED_SETTINGS.appearanceContrast,
181 })
182 }
183 />
184 ) : null
185 }
186 control={
187 <div className="flex w-full items-center gap-3 sm:w-52">
188 <output
189 className="min-w-12 rounded-md bg-muted px-2 py-1 text-center font-mono text-xs font-medium tabular-nums text-foreground"
190 htmlFor="appearance-contrast"
191 >
192 {settings.appearanceContrast}%
193 </output>
194 <input
195 aria-label={gt("Contrast")}
196 className="settings-slider min-w-0 flex-1"
197 id="appearance-contrast"
198 max={MAX_APPEARANCE_CONTRAST}
199 min={MIN_APPEARANCE_CONTRAST}
200 onChange={(event) => {
201 const appearanceContrast = Number(event.currentTarget.value);
202 if (
203 Number.isInteger(appearanceContrast) &&
204 appearanceContrast >= MIN_APPEARANCE_CONTRAST &&
205 appearanceContrast <= MAX_APPEARANCE_CONTRAST
206 ) {
207 updateSettings({ appearanceContrast });
208 }
209 }}
210 step={5}
211 style={appearanceContrastSliderStyle}
212 type="range"
213 value={settings.appearanceContrast}
214 />
215 </div>
216 }
217 />
218
219 <SettingsRow
220 {...searchableSetting("setting-glass-opacity")}
221 description={gt("Higher values make menus, dialogs, and the composer more solid.")}
222 resetAction={
223 settings.glassOpacity !== DEFAULT_UNIFIED_SETTINGS.glassOpacity ? (
224 <SettingResetButton
225 ariaLabel={gt("Reset glass opacity to default")}
226 onClick={() =>
227 updateSettings({ glassOpacity: DEFAULT_UNIFIED_SETTINGS.glassOpacity })
228 }
229 />
230 ) : null
231 }
232 control={
233 <div className="flex w-full items-center gap-3 sm:w-52">
234 <output
235 className="min-w-12 rounded-md bg-muted px-2 py-1 text-center font-mono text-xs font-medium tabular-nums text-foreground"
236 htmlFor="glass-opacity"
237 >
238 {settings.glassOpacity}%
239 </output>
240 <input
241 aria-label={gt("Glass opacity")}
242 className="settings-slider min-w-0 flex-1"
243 id="glass-opacity"
244 max={MAX_GLASS_OPACITY}
245 min={MIN_GLASS_OPACITY}
246 onChange={(event) => {
247 const glassOpacity = Number(event.currentTarget.value);
248 if (
249 Number.isInteger(glassOpacity) &&
250 glassOpacity >= MIN_GLASS_OPACITY &&
251 glassOpacity <= MAX_GLASS_OPACITY
252 ) {
253 updateSettings({ glassOpacity });
254 }
255 }}
256 step={5}
257 style={glassOpacitySliderStyle}
258 type="range"
259 value={settings.glassOpacity}
260 />
261 </div>
262 }
263 />
264
265 <SettingsRow
266 {...searchableSetting("chat-width")}
267 description={gt("Set how wide messages and the composer can grow on large screens.")}
268 resetAction={
269 settings.chatWidth !== DEFAULT_UNIFIED_SETTINGS.chatWidth ? (
270 <SettingResetButton
271 ariaLabel={gt("Reset chat width to default")}
272 onClick={() => updateSettings({ chatWidth: DEFAULT_UNIFIED_SETTINGS.chatWidth })}
273 />
274 ) : null
275 }
276 control={
277 <div className="w-full sm:w-40">
278 <Select
279 value={settings.chatWidth}
280 onValueChange={(value) => {
281 if (value === "comfortable" || value === "wide" || value === "full")
282 updateSettings({ chatWidth: value });
283 }}
284 >
285 <SelectTrigger size="sm" className="w-full min-w-0" aria-label={gt("Chat width")}>
286 <SelectValue>{m(CHAT_WIDTH_LABELS[settings.chatWidth])}</SelectValue>
287 </SelectTrigger>
288 <SelectPopup align="end" alignItemWithTrigger={false}>
289 <SelectItem value="comfortable">{gt("Comfortable (default)")}</SelectItem>
290 <SelectItem value="wide">{m(CHAT_WIDTH_LABELS.wide)}</SelectItem>
291 <SelectItem value="full">{m(CHAT_WIDTH_LABELS.full)}</SelectItem>
292 </SelectPopup>
293 </Select>
294 </div>
295 }
296 />
297 <ThreadDetailsItemsRow />
298 <SettingsRow
299 {...searchableSetting("effort-display")}
300 description={gt(
301 "Show reasoning effort in the composer as its name, such as High, or as bars.",
302 )}
303 resetAction={
304 settings.effortDisplay !== DEFAULT_UNIFIED_SETTINGS.effortDisplay ? (
305 <SettingResetButton
306 ariaLabel={gt("Reset effort display to default")}
307 onClick={() =>
308 updateSettings({ effortDisplay: DEFAULT_UNIFIED_SETTINGS.effortDisplay })
309 }
310 />
311 ) : null
312 }
313 control={
314 <div className="w-full sm:w-40">
315 <Select
316 value={settings.effortDisplay}
317 onValueChange={(value) => {
318 if (value === "bars" || value === "label")
319 updateSettings({ effortDisplay: value });
320 }}
321 >
322 <SelectTrigger
323 size="sm"
324 className="w-full min-w-0"
325 aria-label={gt("Effort display")}
326 >
327 <SelectValue>{m(EFFORT_DISPLAY_LABELS[settings.effortDisplay])}</SelectValue>
328 </SelectTrigger>
329 <SelectPopup align="end" alignItemWithTrigger={false}>
330 <SelectItem value="label">{gt("Word (default)")}</SelectItem>
331 <SelectItem value="bars">{m(EFFORT_DISPLAY_LABELS.bars)}</SelectItem>
332 </SelectPopup>
333 </Select>
334 </div>
335 }
336 />
337 <SettingsRow
338 {...searchableSetting("math-rendering")}
339 description={gt(
340 "Typeset LaTeX math, such as $x^2$ or \\[x^2\\], in messages and Markdown previews. The composer keeps TeX source editable.",
341 )}
342 resetAction={
343 settings.mathRenderingEnabled !== DEFAULT_UNIFIED_SETTINGS.mathRenderingEnabled ? (
344 <SettingResetButton
345 ariaLabel={gt("Reset math rendering to default")}
346 onClick={() =>
347 updateSettings({
348 mathRenderingEnabled: DEFAULT_UNIFIED_SETTINGS.mathRenderingEnabled,
349 })
350 }
351 />
352 ) : null
353 }
354 control={
355 <Switch
356 checked={settings.mathRenderingEnabled}
357 onCheckedChange={(checked) =>
358 updateSettings({ mathRenderingEnabled: Boolean(checked) })
359 }
360 aria-label={gt("Render math")}
361 />
362 }
363 />
364 <AdvancedSettings
365 targetIds={[
366 searchableSetting("panel-animations").id,
367 searchableSetting("environment-identification").id,
368 // Environment identification's search result lands on this section.
369 "appearance-interface",
370 searchableSetting("composer-context").id,
371 ]}
372 >
373 <SettingsRow
374 {...searchableSetting("panel-animations")}
375 description={gt("Set how fast panels open and close.")}
376 control={
377 <div className="grid w-full grid-cols-[5rem_minmax(0,1fr)] items-center gap-3 sm:w-auto sm:grid-cols-[7rem_13rem] sm:gap-4">
378 <PanelAnimationsPreview durationMs={settings.panelAnimationDurationMs} />
379 <div className="flex w-full items-center gap-3">
380 <output
381 className="min-w-16 rounded-md bg-muted px-2 py-1 text-center font-mono text-xs font-medium tabular-nums text-foreground"
382 htmlFor="panel-animation-duration"
383 >
384 {settings.panelAnimationDurationMs} ms
385 </output>
386 <input
387 aria-label={gt("Panel animation duration")}
388 className="settings-slider min-w-0 flex-1"
389 id="panel-animation-duration"
390 max={MAX_PANEL_ANIMATION_DURATION_MS}
391 min={MIN_PANEL_ANIMATION_DURATION_MS}
392 onChange={(event) => {
393 const panelAnimationDurationMs = Number(event.currentTarget.value);
394 if (
395 Number.isInteger(panelAnimationDurationMs) &&
396 panelAnimationDurationMs >= MIN_PANEL_ANIMATION_DURATION_MS &&
397 panelAnimationDurationMs <= MAX_PANEL_ANIMATION_DURATION_MS
398 ) {
399 updateSettings({ panelAnimationDurationMs });
400 }
401 }}
402 step={25}
403 style={panelAnimationDurationSliderStyle}
404 type="range"
405 value={settings.panelAnimationDurationMs}
406 />
407 </div>
408 </div>
409 }
410 resetAction={
411 settings.panelAnimationDurationMs !==
412 DEFAULT_UNIFIED_SETTINGS.panelAnimationDurationMs ? (
413 <SettingResetButton
414 ariaLabel={gt("Reset panel animations to default")}
415 onClick={() =>
416 updateSettings({
417 panelAnimationDurationMs: DEFAULT_UNIFIED_SETTINGS.panelAnimationDurationMs,
418 })
419 }
420 />
421 ) : null
422 }
423 />
424 <SettingsRow
425 {...searchableSetting("environment-identification")}
426 description={environmentIdentificationDescription}
427 resetAction={
428 settings.environmentIdentificationMode !== DEFAULT_ENVIRONMENT_IDENTIFICATION_MODE ? (
429 <SettingResetButton
430 ariaLabel={gt("Reset environment identification to default")}
431 onClick={() =>
432 updateSettings({
433 environmentIdentificationMode: DEFAULT_ENVIRONMENT_IDENTIFICATION_MODE,
434 })
435 }
436 />
437 ) : null
438 }
439 control={
440 <Select
441 value={settings.environmentIdentificationMode}
442 onValueChange={(value) => {
443 const option = environmentIdentificationOptionList.find(
444 ([optionValue]) => optionValue === value,
445 );
446 if (option) updateSettings({ environmentIdentificationMode: option[0] });
447 }}
448 >
449 <SelectTrigger
450 size="sm"
451 className="w-full sm:w-40"
452 aria-label={gt("Environment identification")}
453 >
454 <SelectValue>{environmentIdentificationLabel}</SelectValue>
455 </SelectTrigger>
456 <SelectPopup align="end" alignItemWithTrigger={false}>
457 {environmentIdentificationOptionList.map(([value, label]) => (
458 <SelectItem hideIndicator key={value} value={value}>
459 {m(label)}
460 </SelectItem>
461 ))}
462 </SelectPopup>
463 </Select>
464 }
465 />
466 <SettingsRow
467 {...searchableSetting("composer-context")}
468 description={gt(
469 "Keep branch and worktree controls below the composer after a thread starts.",
470 )}
471 resetAction={
472 settings.persistComposerContextStrip !==
473 DEFAULT_UNIFIED_SETTINGS.persistComposerContextStrip ? (
474 <SettingResetButton
475 ariaLabel={gt("Reset composer context to default")}
476 onClick={() =>
477 updateSettings({
478 persistComposerContextStrip:
479 DEFAULT_UNIFIED_SETTINGS.persistComposerContextStrip,
480 })
481 }
482 />
483 ) : null
484 }
485 control={
486 <Switch
487 checked={settings.persistComposerContextStrip}
488 onCheckedChange={(checked) =>
489 updateSettings({ persistComposerContextStrip: Boolean(checked) })
490 }
491 aria-label={gt("Keep composer context visible in active threads")}
492 />
493 }
494 />
495 </AdvancedSettings>
496 </SettingsSection>
497
498 <TypographySection />
499 </SettingsPageContainer>
500 );
501 }