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

AppearanceSettingsPanel.tsx

475 lines18.4 KB

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