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