@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 128 lines 5.24 KB
Raw
1 import { appAtomRegistry } from "../rpc/atomRegistry";
2 import { localEnvironmentIdAtom } from "../state/primaryEnvironment";
3 import { themeServerSettingsAtom } from "../state/server";
4 import { getThemeDefinition, singleAppearanceOf, subscribeToCustomThemes } from "../themePalette";
5 import { getThemeSnapshot, setAppearanceMode, setTheme } from "./useTheme";
6
7 const APPLIED_DEFAULT_THEME_STORAGE_PREFIX = "t3code:default-theme-applied:v2:";
8 const APPLIED_DEFAULT_APPEARANCE_STORAGE_PREFIX = "t3code:default-appearance-applied:v1:";
9
10 /** Include set time so reasserting the same theme still applies. */
11 export function defaultThemeGeneration(theme: string, setAt: string): string {
12 return setAt.length > 0 ? `${theme}@${setAt}` : theme;
13 }
14
15 export function defaultThemeToApply(input: {
16 readonly environmentId: string | null;
17 readonly defaultTheme: string;
18 readonly defaultThemeSetAt: string;
19 readonly appliedGeneration: string | null;
20 readonly resolves: boolean;
21 }): string | null {
22 // Empty with a set time means the stock theme was set; with none, no theme was.
23 if (input.environmentId === null) return null;
24 if (input.defaultTheme.length === 0 && input.defaultThemeSetAt.length === 0) return null;
25 const generation = defaultThemeGeneration(input.defaultTheme, input.defaultThemeSetAt);
26 if (input.appliedGeneration === generation) return null;
27 // The setting and the palette it names arrive independently, so an id that
28 // does not resolve yet is not a failure -- adoption re-checks when the
29 // published set changes.
30 if (!input.resolves) return null;
31 return generation;
32 }
33
34 /** A single-appearance theme sets its own mode unless a mode was set with it or after it. */
35 export function themeSetsOwnMode(input: {
36 readonly defaultThemeSetAt: string;
37 readonly defaultAppearance: string;
38 readonly defaultAppearanceSetAt: string;
39 }): boolean {
40 // ISO timestamps from one server compare correctly as strings.
41 return input.defaultAppearance === "" || input.defaultAppearanceSetAt < input.defaultThemeSetAt;
42 }
43
44 function readAppliedGeneration(storageKey: string): string | null {
45 if (typeof window === "undefined") return null;
46 try {
47 return window.localStorage.getItem(storageKey);
48 } catch {
49 return null;
50 }
51 }
52
53 function writeAppliedGeneration(storageKey: string, generation: string): void {
54 try {
55 window.localStorage.setItem(storageKey, generation);
56 } catch {}
57 }
58
59 function adoptDefaultTheme(): void {
60 if (typeof window === "undefined") return;
61 const environmentId = appAtomRegistry.get(localEnvironmentIdAtom);
62 if (environmentId === null) return;
63 const settings = appAtomRegistry.get(themeServerSettingsAtom);
64 const { defaultTheme, defaultThemeSetAt } = settings;
65 const definition = getThemeDefinition(defaultTheme);
66 const storageKey = `${APPLIED_DEFAULT_THEME_STORAGE_PREFIX}${environmentId}`;
67 const generation = defaultThemeToApply({
68 environmentId,
69 defaultTheme,
70 defaultThemeSetAt,
71 appliedGeneration: readAppliedGeneration(storageKey),
72 resolves: definition !== null || defaultTheme === "",
73 });
74 if (generation === null) return;
75 if (definition === null) {
76 // The stock theme keeps the client's own light, dark, or system mode.
77 if (setTheme(getThemeSnapshot().appearanceMode)) writeAppliedGeneration(storageKey, generation);
78 return;
79 }
80
81 // A single-appearance theme needs both preference and appearance writes to take effect.
82 if (!setTheme(defaultTheme)) return;
83 const half = singleAppearanceOf(definition);
84 // Only record adoption after both writes succeed, so an appearance failure remains retryable.
85 if (half !== null && themeSetsOwnMode(settings) && !setAppearanceMode(half)) return;
86 writeAppliedGeneration(storageKey, generation);
87 }
88
89 /** Like the theme, the environment's mode applies once per set, then local choices win. */
90 function adoptDefaultAppearance(): void {
91 if (typeof window === "undefined") return;
92 const environmentId = appAtomRegistry.get(localEnvironmentIdAtom);
93 const { defaultAppearance, defaultAppearanceSetAt } =
94 appAtomRegistry.get(themeServerSettingsAtom);
95 const storageKey = `${APPLIED_DEFAULT_APPEARANCE_STORAGE_PREFIX}${environmentId}`;
96 const generation = defaultThemeToApply({
97 environmentId,
98 defaultTheme: defaultAppearance,
99 defaultThemeSetAt: defaultAppearanceSetAt,
100 appliedGeneration: readAppliedGeneration(storageKey),
101 resolves: true,
102 });
103 if (generation === null || defaultAppearance === "") return;
104 if (setAppearanceMode(defaultAppearance)) writeAppliedGeneration(storageKey, generation);
105 }
106
107 function adoptDefaults(): void {
108 adoptDefaultTheme();
109 adoptDefaultAppearance();
110 }
111
112 /**
113 * Applies the theme environment's default theme once per generation. Start it after the
114 * environment theme sync so a first-run client adopting the environment's own theme finds it
115 * already in the library; it re-checks when published or custom palettes change, so a theme
116 * that resolves late still applies.
117 */
118 export function startDefaultThemeAdoption(): () => void {
119 const unsubscribes = [
120 appAtomRegistry.subscribe(localEnvironmentIdAtom, adoptDefaults),
121 appAtomRegistry.subscribe(themeServerSettingsAtom, adoptDefaults),
122 subscribeToCustomThemes(adoptDefaults),
123 ];
124 adoptDefaults();
125 return () => {
126 for (const unsubscribe of unsubscribes) unsubscribe();
127 };
128 }