"use client"; import { useCallback, useSyncExternalStore } from "react"; /** * Shell media state (theme + sound) — 1:1 port of the original * ThemeModeProvider (chunk 2689132c4e070b68.js module 90975) + ShellMedia * sound store (module 23525): * * - theme ∈ {light, dark, system}, persisted to localStorage "theme" * (original key, kept verbatim for parity); default "light". * - resolvedTheme = theme === "system" * ? (matchMedia("(prefers-color-scheme: dark)").matches ? dark : light) * : theme; * gets BOTH the resolved class (light/dark) and the setting, so * CSS tokens flip via the .dark block in globals.css. * - "system" follows OS changes live (original matchMedia change listener). * - soundEnabled persisted to localStorage "sound" ("on"/"off", original * keys). * * Implemented as a module-singleton external store (useSyncExternalStore) * instead of the original context: the header and the canvas components * (DotOverlay, LoadingCurtain, GlassModels) live in different trees and the * canvas reads theme without re-mounting. */ export type ThemeSetting = "light" | "dark" | "system"; export type ResolvedTheme = "light" | "dark"; const THEME_KEY = "theme"; const SOUND_KEY = "sound"; const SETTINGS: ThemeSetting[] = ["light", "dark", "system"]; type ShellMediaState = { theme: ThemeSetting; resolvedTheme: ResolvedTheme; soundEnabled: boolean; }; let state: ShellMediaState = { theme: "light", resolvedTheme: "light", soundEnabled: false, }; const listeners = new Set<() => void>(); let hydrated = false; let mediaQuery: MediaQueryList | null = null; function emit() { for (const l of listeners) l(); } function setState(patch: Partial) { state = { ...state, ...patch }; emit(); } function resolve(theme: ThemeSetting): ResolvedTheme { if (theme !== "system") return theme; return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; } /** Mirror the resolved theme onto (original: classList + remove/add). */ function applyDocument(theme: ThemeSetting) { const el = document.documentElement; const resolved = resolve(theme); el.classList.remove("light", "dark"); el.classList.add(resolved); el.dataset.themeSetting = theme; } function readStored(key: string): string | null { try { return window.localStorage.getItem(key); } catch { return null; } } function writeStored(key: string, value: string) { try { window.localStorage.setItem(key, value); } catch {} } function hydrate() { if (hydrated || typeof window === "undefined") return; hydrated = true; const stored = readStored(THEME_KEY); const theme: ThemeSetting = SETTINGS.includes(stored as ThemeSetting) ? (stored as ThemeSetting) : "light"; mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); mediaQuery.addEventListener("change", onSystemChange); state = { ...state, theme, resolvedTheme: resolve(theme) }; applyDocument(theme); state = { ...state, soundEnabled: readStored(SOUND_KEY) === "on", }; } function onSystemChange() { if (state.theme !== "system") return; const resolved = resolve("system"); applyDocument("system"); setState({ resolvedTheme: resolved }); } if (typeof window !== "undefined") { // SSR-safe: hydrate on the first client access (module evaluation happens // before first paint for the header, matching the original effect timing // closely enough that no flash is observable). hydrate(); } function setTheme(next: ThemeSetting) { writeStored(THEME_KEY, next); applyDocument(next); setState({ theme: next, resolvedTheme: resolve(next) }); } function setSoundEnabled(next: boolean) { writeStored(SOUND_KEY, next ? "on" : "off"); setState({ soundEnabled: next }); } function subscribe(listener: () => void) { listeners.add(listener); return () => listeners.delete(listener); } const getSnapshot = () => state; export function useShellMedia(): ShellMediaState & { setTheme: (next: ThemeSetting) => void; setSoundEnabled: (next: boolean) => void; } { const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot); // Stable identity via module-level functions (no per-render closures). return useSnapshotWithActions(snapshot); } // Keep the actions on the same object shape as the original hook return. function useSnapshotWithActions( snapshot: ShellMediaState ): ShellMediaState & { setTheme: (next: ThemeSetting) => void; setSoundEnabled: (next: boolean) => void; } { return { ...snapshot, setTheme, setSoundEnabled, }; } export function getResolvedTheme(): ResolvedTheme { return state.resolvedTheme; } export function subscribeShellMedia(listener: () => void) { return subscribe(listener); } export function useResolvedTheme(): ResolvedTheme { const s = useShellMedia(); return s.resolvedTheme; } export type ShellMedia = ReturnType;