"use client"; import { createContext, useContext, useEffect, useRef, useState } from "react"; import { type StoreApi, useStore } from "zustand"; import { type FontKey, fontRegistry } from "@/lib/fonts/registry"; import { CONTENT_LAYOUT_VALUES, NAVBAR_STYLE_VALUES, SIDEBAR_COLLAPSIBLE_VALUES, SIDEBAR_VARIANT_VALUES, } from "@/lib/preferences/layout"; import { THEME_MODE_VALUES, THEME_PRESET_VALUES } from "@/lib/preferences/theme"; import { applyThemeMode, subscribeToSystemTheme } from "@/lib/preferences/theme-utils"; import { createPreferencesStore, type PreferencesState } from "./preferences-store"; const PreferencesStoreContext = createContext | null>(null); const FONT_VALUES = Object.keys(fontRegistry) as FontKey[]; function getSafeValue(raw: string | null, allowed: readonly T[]): T | undefined { if (!raw) return undefined; return allowed.includes(raw as T) ? (raw as T) : undefined; } function readDomState(): Partial { const root = document.documentElement; const themeModeAttr = getSafeValue(root.getAttribute("data-theme-mode"), THEME_MODE_VALUES); const resolvedMode = root.classList.contains("dark") ? "dark" : "light"; return { themeMode: themeModeAttr ?? resolvedMode, resolvedThemeMode: resolvedMode, themePreset: getSafeValue(root.getAttribute("data-theme-preset"), THEME_PRESET_VALUES), font: getSafeValue(root.getAttribute("data-font"), FONT_VALUES), contentLayout: getSafeValue(root.getAttribute("data-content-layout"), CONTENT_LAYOUT_VALUES), navbarStyle: getSafeValue(root.getAttribute("data-navbar-style"), NAVBAR_STYLE_VALUES), sidebarVariant: getSafeValue(root.getAttribute("data-sidebar-variant"), SIDEBAR_VARIANT_VALUES), sidebarCollapsible: getSafeValue(root.getAttribute("data-sidebar-collapsible"), SIDEBAR_COLLAPSIBLE_VALUES), }; } export const PreferencesStoreProvider = ({ children, themeMode, themePreset, font, contentLayout, navbarStyle, }: { children: React.ReactNode; themeMode: PreferencesState["themeMode"]; themePreset: PreferencesState["themePreset"]; font: PreferencesState["font"]; contentLayout: PreferencesState["contentLayout"]; navbarStyle: PreferencesState["navbarStyle"]; }) => { const [store] = useState>(() => createPreferencesStore({ themeMode, themePreset, font, contentLayout, navbarStyle, }), ); const domSnapshotRef = useRef | null>(null); useEffect(() => { const domState = readDomState(); domSnapshotRef.current = domState; store.setState((prev) => ({ ...prev, ...domState, isSynced: true, })); }, [store]); useEffect(() => { let unsubscribeMedia: (() => void) | undefined; const applyFromMode = (mode: PreferencesState["themeMode"]) => { unsubscribeMedia?.(); const resolved = applyThemeMode(mode); store.setState((prev) => ({ ...prev, resolvedThemeMode: resolved })); if (mode === "system") { unsubscribeMedia = subscribeToSystemTheme(() => { const next = applyThemeMode("system"); store.setState((prev) => ({ ...prev, resolvedThemeMode: next })); }); } }; const startMode = domSnapshotRef.current?.themeMode ?? store.getState().themeMode; applyFromMode(startMode); const unsubscribeStore = store.subscribe((s, p) => { if (s.themeMode !== p.themeMode) applyFromMode(s.themeMode); }); return () => { unsubscribeMedia?.(); unsubscribeStore(); }; }, [store]); return {children}; }; export const usePreferencesStore = (selector: (state: PreferencesState) => T): T => { const store = useContext(PreferencesStoreContext); if (!store) throw new Error("Missing PreferencesStoreProvider"); return useStore(store, selector); };