Open History to see the last commit
1import type { EnvironmentTheme } from "@t3tools/contracts";
2import { useAtomValue } from "@effect/atom-react";
3import * as Equal from "effect/Equal";
4import { useEffect, useRef, useSyncExternalStore } from "react";
5
6import { primaryServerEnvironmentThemesAtom } from "../state/server";
7import {
8 createVividThemeColors,
9 getDefaultThemeColors,
10 getEnvironmentThemes,
11 isReservedThemeId,
12 lenientThemeColorOverrides,
13 setEnvironmentThemes,
14 subscribeToCustomThemes,
15 type ThemeAppearance,
16 type ThemeColors,
17 type ThemeDefinition,
18} from "../themePalette";
19import { useTheme } from "./useTheme";
20
21function publishedThemeColors(
22 theme: EnvironmentTheme,
23 appearance: ThemeAppearance,
24 colors: Readonly<Record<string, string>> | undefined,
25): ThemeColors {
26 // A variant builds on that appearance's stock defaults: the generator follows
27 // the seed canvas's luminance, so dark seeds would give a light variant
28 // unreadable colors.
29 const base =
30 appearance === theme.appearance && theme.canvas !== undefined && theme.accent !== undefined
31 ? createVividThemeColors(appearance, theme.canvas, theme.accent)
32 : getDefaultThemeColors(appearance);
33 return { ...base, ...lenientThemeColorOverrides(colors ?? {}) };
34}
35
36export function environmentThemeDefinition(theme: EnvironmentTheme): ThemeDefinition {
37 const variants: Partial<Record<ThemeAppearance, ThemeColors>> = {};
38 for (const [variantAppearance, variantColors] of Object.entries(theme.variants ?? {})) {
39 if (variantAppearance === theme.appearance) continue;
40 variants[variantAppearance as ThemeAppearance] = publishedThemeColors(
41 theme,
42 variantAppearance as ThemeAppearance,
43 variantColors,
44 );
45 }
46
47 return {
48 id: theme.id,
49 label: theme.name,
50 appearance: theme.appearance,
51 colors: publishedThemeColors(theme, theme.appearance, theme.colors),
52 ...(Object.keys(variants).length > 0 ? { variants } : {}),
53 // Only the pure seeded form is guided-generator output. An exported file,
54 // or seeds carrying explicit role overrides, must open Duplicate in the
55 // advanced editor -- the guided one regenerates from canvas and accent and
56 // would discard whatever the machine hand-tuned.
57 ...(theme.canvas !== undefined &&
58 theme.accent !== undefined &&
59 theme.colors === undefined &&
60 theme.variants === undefined
61 ? { managed: true }
62 : {}),
63 };
64}
65
66/**
67 * Published palettes this client can render. Reserved ids are dropped here rather
68 * than rendered: a published `t3-iris.json` would show this palette on its
69 * card while "Use" resolved the built-in, and a published `dark.json` would
70 * capture everyone whose stored preference is the stock `"dark"`.
71 */
72export function publishedThemeDefinitions(
73 themes: ReadonlyArray<EnvironmentTheme>,
74): ReadonlyArray<ThemeDefinition> {
75 return themes
76 .filter((theme) => {
77 if (isReservedThemeId(theme.id)) return false;
78 if (theme.canvas !== undefined && theme.accent !== undefined) return true;
79 const otherAppearance = theme.appearance === "dark" ? "light" : "dark";
80 return [theme.colors, theme.variants?.[otherAppearance]].some(
81 (colors) =>
82 colors !== undefined && Object.keys(lenientThemeColorOverrides(colors)).length > 0,
83 );
84 })
85 .map(environmentThemeDefinition);
86}
87
88export function useEnvironmentThemeDefinitions(): ReadonlyArray<ThemeDefinition> {
89 return useSyncExternalStore(subscribeToCustomThemes, getEnvironmentThemes, () => []);
90}
91
92export function useEnvironmentThemeSync(): void {
93 const published = useAtomValue(primaryServerEnvironmentThemesAtom);
94 const { refreshTheme } = useTheme();
95 const lastPublished = useRef<ReadonlyArray<EnvironmentTheme> | null>(null);
96
97 useEffect(() => {
98 // Every reconnect snapshot delivers a fresh but usually identical array;
99 // regenerating palettes and repainting the document for it is exactly the
100 // wasted-frame class this codebase audits for.
101 if (lastPublished.current !== null && Equal.equals(lastPublished.current, published)) return;
102 lastPublished.current = published;
103
104 // The palette is painted from a snapshot taken when the theme last
105 // changed, so new colors only land if the active theme is re-applied.
106 if (setEnvironmentThemes(publishedThemeDefinitions(published))) {
107 refreshTheme({ preservePreview: true });
108 }
109 }, [published, refreshTheme]);
110}