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