Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useDefaultTheme.ts

76 lines3.0 KB

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/** Include set time so reasserting the same theme still applies. */
12export function defaultThemeGeneration(theme: string, setAt: string): string {
13 return setAt.length > 0 ? `${theme}@${setAt}` : theme;
15​
16export function defaultThemeToApply(input: {
17 readonly environmentId: string | null;
18 readonly defaultTheme: string;
19 readonly defaultThemeSetAt: string;
20 readonly appliedGeneration: string | null;
21 readonly resolves: boolean;
22}): string | null {
23 if (input.environmentId === null || input.defaultTheme.length === 0) return null;
24 const generation = defaultThemeGeneration(input.defaultTheme, input.defaultThemeSetAt);
25 if (input.appliedGeneration === generation) return null;
26 // Settings and palettes arrive independently; an unresolved theme must retry when palettes arrive.
27 if (!input.resolves) return null;
28 return generation;
30​
31function readAppliedGeneration(storageKey: string): string | null {
32 if (typeof window === "undefined") return null;
33 try {
34 return window.localStorage.getItem(storageKey);
35 } catch {
36 return null;
37 }
39​
40function writeAppliedGeneration(storageKey: string, generation: string): void {
41 try {
42 window.localStorage.setItem(storageKey, generation);
43 } catch {}
45​
46export function useDefaultThemeAdoption(): void {
47 const environmentId = useAtomValue(primaryEnvironmentIdAtom);
48 const settings = useAtomValue(primaryServerSettingsAtom);
49 const { defaultTheme, defaultThemeSetAt } = settings;
50 const { setTheme, setAppearanceMode } = useTheme();
51 const definition = useSyncExternalStore(
52 subscribeToCustomThemes,
53 () => getThemeDefinition(defaultTheme),
54 () => null,
55 );
56​
57 useEffect(() => {
58 if (typeof window === "undefined" || environmentId === null) return;
59 const storageKey = `${APPLIED_DEFAULT_THEME_STORAGE_PREFIX}${environmentId}`;
60 const generation = defaultThemeToApply({
61 environmentId,
62 defaultTheme,
63 defaultThemeSetAt,
64 appliedGeneration: readAppliedGeneration(storageKey),
65 resolves: definition !== null,
66 });
67 if (generation === null || definition === null) return;
68​
69 // A single-appearance theme needs both preference and appearance writes to take effect.
70 if (!setTheme(defaultTheme)) return;
71 const half = singleAppearanceOf(definition);
72 // Only record adoption after both writes succeed, so an appearance failure remains retryable.
73 if (half !== null && !setAppearanceMode(half)) return;
74 writeAppliedGeneration(storageKey, generation);
75 }, [environmentId, defaultTheme, defaultThemeSetAt, definition, setTheme, setAppearanceMode]);