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
10/**
11 * Scoped per environment: each machine's `t3 theme set` is its own act, so
12 * hopping between primary environments neither replays one environment's
13 * theme over the user's pick nor swallows another's.
14 */
15const APPLIED_DEFAULT_THEME_STORAGE_PREFIX = "t3code:default-theme-applied:v2:";
16
17/**
18 * One generation per set: keyed on when the theme was set, not just its
19 * value, so re-asserting the same theme still acts. Environments provisioned
20 * by builds without the timestamp degrade to applying once per value.
21 */
22export function defaultThemeGeneration(theme: string, setAt: string): string {
23 return setAt.length > 0 ? `${theme}@${setAt}` : theme;
24}
25
26/**
27 * The generation to apply, or null to leave this client alone. Pure so the
28 * rule -- apply once per set, never replay one already applied, wait for a
29 * theme that has not arrived yet -- can be tested without a renderer.
30 */
31export function defaultThemeToApply(input: {
32 readonly environmentId: string | null;
33 readonly defaultTheme: string;
34 readonly defaultThemeSetAt: string;
35 readonly appliedGeneration: string | null;
36 readonly resolves: boolean;
37}): string | null {
38 if (input.environmentId === null || input.defaultTheme.length === 0) return null;
39 const generation = defaultThemeGeneration(input.defaultTheme, input.defaultThemeSetAt);
40 if (input.appliedGeneration === generation) return null;
41 // The setting and the palette it names arrive independently, so an id that
42 // does not resolve yet is not a failure -- the effect runs again when the
43 // published set changes.
44 if (!input.resolves) return null;
45 return generation;
46}
47
48function readAppliedGeneration(storageKey: string): string | null {
49 if (typeof window === "undefined") return null;
50 try {
51 return window.localStorage.getItem(storageKey);
52 } catch {
53 return null;
54 }
55}
56
57function writeAppliedGeneration(storageKey: string, generation: string): void {
58 try {
59 window.localStorage.setItem(storageKey, generation);
60 } catch {
61 // Unrecordable means the next config event applies again; harmless.
62 }
63}
64
65/**
66 * Applies the environment's theme (`t3 theme set <id>`). Each set switches
67 * this client once — live when connected, on the next connect otherwise —
68 * and then steps aside: a theme the user picks in Settings afterwards wins
69 * until the environment's theme is set again. The environment's own published
70 * themes are valid targets, which is why this waits for an id that does not
71 * resolve yet — the setting and the palette it names arrive independently.
72 */
73export function useDefaultThemeAdoption(): void {
74 const environmentId = useAtomValue(primaryEnvironmentIdAtom);
75 const settings = useAtomValue(primaryServerSettingsAtom);
76 const { defaultTheme, defaultThemeSetAt } = settings;
77 const { setTheme, setAppearanceMode } = useTheme();
78 // Re-runs adoption when a late-arriving published theme makes the
79 // requested id resolvable.
80 const environmentThemes = useEnvironmentThemeDefinitions();
81
82 useEffect(() => {
83 if (typeof window === "undefined" || environmentId === null) return;
84 const storageKey = `${APPLIED_DEFAULT_THEME_STORAGE_PREFIX}${environmentId}`;
85 const definition = getThemeDefinition(defaultTheme);
86 const generation = defaultThemeToApply({
87 environmentId,
88 defaultTheme,
89 defaultThemeSetAt,
90 appliedGeneration: readAppliedGeneration(storageKey),
91 resolves: definition !== null,
92 });
93 if (generation === null || definition === null) return;
94
95 // Deliberately not the card's rule. Clicking a card is a user choosing one
96 // half of their own mix; a set theme is the environment saying what this
97 // client opens on, so it takes the base preference and, for a
98 // single-appearance theme, the matching mode -- otherwise a dark-only
99 // theme on a light client is recorded as applied while nothing changes.
100 if (!setTheme(defaultTheme)) return;
101 const half = singleAppearanceOf(definition);
102 // Recorded only once both land: marking the generation applied while the
103 // appearance switch failed would leave a dark-only theme rendering its
104 // light half with no retry.
105 if (half !== null && !setAppearanceMode(half)) return;
106 writeAppliedGeneration(storageKey, generation);
107 }, [
108 environmentId,
109 defaultTheme,
110 defaultThemeSetAt,
111 environmentThemes,
112 setTheme,
113 setAppearanceMode,
114 ]);
115}