Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { useEffect } from "react";
3
4import { primaryEnvironmentIdAtom } from "../state/primaryEnvironment";
5import { primaryServerSettingsAtom } from "../state/server";
6import { getThemeDefinition, singleAppearanceOf } from "../themePalette";
7import { useEnvironmentThemeDefinitions } from "./useEnvironmentTheme";
8import { useTheme } from "./useTheme";
9
10const APPLIED_DEFAULT_THEME_STORAGE_PREFIX = "t3code:default-theme-applied:v2:";
11
12/**
13 * One generation per set: keyed on when the theme was set, not just its value,
14 * so re-asserting the same theme still acts.
15 */
16export function defaultThemeGeneration(theme: string, setAt: string): string {
17 return setAt.length > 0 ? `${theme}@${setAt}` : theme;
18}
19
20export function defaultThemeToApply(input: {
21 readonly environmentId: string | null;
22 readonly defaultTheme: string;
23 readonly defaultThemeSetAt: string;
24 readonly appliedGeneration: string | null;
25 readonly resolves: boolean;
26}): string | null {
27 if (input.environmentId === null || input.defaultTheme.length === 0) return null;
28 const generation = defaultThemeGeneration(input.defaultTheme, input.defaultThemeSetAt);
29 if (input.appliedGeneration === generation) return null;
30 // The setting and the palette it names arrive independently, so an id that
31 // does not resolve yet is not a failure -- the effect runs again when the
32 // published set changes.
33 if (!input.resolves) return null;
34 return generation;
35}
36
37function readAppliedGeneration(storageKey: string): string | null {
38 if (typeof window === "undefined") return null;
39 try {
40 return window.localStorage.getItem(storageKey);
41 } catch {
42 return null;
43 }
44}
45
46function writeAppliedGeneration(storageKey: string, generation: string): void {
47 try {
48 window.localStorage.setItem(storageKey, generation);
49 } catch {}
50}
51
52export function useDefaultThemeAdoption(): void {
53 const environmentId = useAtomValue(primaryEnvironmentIdAtom);
54 const settings = useAtomValue(primaryServerSettingsAtom);
55 const { defaultTheme, defaultThemeSetAt } = settings;
56 const { setTheme, setAppearanceMode } = useTheme();
57 const environmentThemes = useEnvironmentThemeDefinitions();
58
59 useEffect(() => {
60 if (typeof window === "undefined" || environmentId === null) return;
61 const storageKey = `${APPLIED_DEFAULT_THEME_STORAGE_PREFIX}${environmentId}`;
62 const definition = getThemeDefinition(defaultTheme);
63 const generation = defaultThemeToApply({
64 environmentId,
65 defaultTheme,
66 defaultThemeSetAt,
67 appliedGeneration: readAppliedGeneration(storageKey),
68 resolves: definition !== null,
69 });
70 if (generation === null || definition === null) return;
71
72 // Clicking a card is a user choosing one half of their own mix; a set theme
73 // is the environment saying what this client opens on, so it takes the base
74 // preference and, for a single-appearance theme, the matching mode --
75 // otherwise a dark-only theme on a light client is recorded as applied
76 // while nothing changes.
77 if (!setTheme(defaultTheme)) return;
78 const half = singleAppearanceOf(definition);
79 // Recorded only once both land: marking the generation applied while the
80 // appearance switch failed would leave a dark-only theme rendering its
81 // light half with no retry.
82 if (half !== null && !setAppearanceMode(half)) return;
83 writeAppliedGeneration(storageKey, generation);
84 }, [
85 environmentId,
86 defaultTheme,
87 defaultThemeSetAt,
88 environmentThemes,
89 setTheme,
90 setAppearanceMode,
91 ]);
92}