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