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

AppearanceSettingsPanel.tsx

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