Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AppearanceSettingsPanel.tsx

550 lines21.9 KB

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