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