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

useEnvironmentTheme.ts

96 lines3.6 KB

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 // The generator follows canvas luminance; dark seeds would make a light variant unreadable.
27 const base =
28 appearance === theme.appearance && theme.canvas !== undefined && theme.accent !== undefined
29 ? createVividThemeColors(appearance, theme.canvas, theme.accent)
30 : getDefaultThemeColors(appearance);
31 return { ...base, ...lenientThemeColorOverrides(colors ?? {}) };
33​
34export function environmentThemeDefinition(theme: EnvironmentTheme): ThemeDefinition {
35 const variants: Partial<Record<ThemeAppearance, ThemeColors>> = {};
36 for (const [variantAppearance, variantColors] of Object.entries(theme.variants ?? {})) {
37 if (variantAppearance === theme.appearance) continue;
38 variants[variantAppearance as ThemeAppearance] = publishedThemeColors(
39 theme,
40 variantAppearance as ThemeAppearance,
41 variantColors,
42 );
43 }
44​
45 return {
46 id: theme.id,
47 label: theme.name,
48 appearance: theme.appearance,
49 colors: publishedThemeColors(theme, theme.appearance, theme.colors),
50 ...(Object.keys(variants).length > 0 ? { variants } : {}),
51 // Guided editing regenerates from seeds, so hand-tuned role overrides must use the advanced editor.
52 ...(theme.canvas !== undefined &&
53 theme.accent !== undefined &&
54 theme.colors === undefined &&
55 theme.variants === undefined
56 ? { managed: true }
57 : {}),
58 };
60​
61/** Filters reserved ids that would select built-ins instead of the published palette. */
62export function publishedThemeDefinitions(
63 themes: ReadonlyArray<EnvironmentTheme>,
64): ReadonlyArray<ThemeDefinition> {
65 return themes
66 .filter((theme) => {
67 if (isReservedThemeId(theme.id)) return false;
68 if (theme.canvas !== undefined && theme.accent !== undefined) return true;
69 const otherAppearance = theme.appearance === "dark" ? "light" : "dark";
70 return [theme.colors, theme.variants?.[otherAppearance]].some(
71 (colors) =>
72 colors !== undefined && Object.keys(lenientThemeColorOverrides(colors)).length > 0,
73 );
74 })
75 .map(environmentThemeDefinition);
77​
78export function useEnvironmentThemeDefinitions(): ReadonlyArray<ThemeDefinition> {
79 return useSyncExternalStore(subscribeToCustomThemes, getEnvironmentThemes, () => []);
81​
82export function useEnvironmentThemeSync(): void {
83 const published = useAtomValue(primaryServerEnvironmentThemesAtom);
84 const { refreshTheme } = useTheme();
85 const lastPublished = useRef<ReadonlyArray<EnvironmentTheme> | null>(null);
86​
87 useEffect(() => {
88 if (lastPublished.current !== null && Equal.equals(lastPublished.current, published)) return;
89 lastPublished.current = published;
90​
91 // Active palettes are snapshots; changed colors require reapplying the theme.
92 if (setEnvironmentThemes(publishedThemeDefinitions(published))) {
93 refreshTheme({ preservePreview: true });
94 }
95 }, [published, refreshTheme]);