15b138cdd7
- 主题默认值 light → system(首次访问自动跟随 prefers-color-scheme, 与原版默认 light 不同,为产品决策) - 音效默认值 off → on(localStorage 无记录即开启,显式关闭才停; 首次真实播放仍受自动播放策略限制,由 useBgm 手势恢复逻辑兜底)
210 lines
6.0 KiB
TypeScript
210 lines
6.0 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, 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 "system" — follow the
|
|
* device (auto), per product decision (original defaulted to "light").
|
|
* - resolvedTheme = theme === "system"
|
|
* ? (matchMedia("(prefers-color-scheme: dark)").matches ? dark : light)
|
|
* : theme;
|
|
* <html> 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); default ON — BGM starts (autoplay policies defer actual playback
|
|
* to the first user gesture, handled in useBgm's recovery effect).
|
|
*
|
|
* 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: "system",
|
|
resolvedTheme: "light",
|
|
soundEnabled: true,
|
|
};
|
|
|
|
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<ShellMediaState>) {
|
|
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 <html> (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);
|
|
// 无有效存储 → 默认 system(自动跟随设备)
|
|
const theme: ThemeSetting = SETTINGS.includes(stored as ThemeSetting)
|
|
? (stored as ThemeSetting)
|
|
: "system";
|
|
mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
mediaQuery.addEventListener("change", onSystemChange);
|
|
state = { ...state, theme, resolvedTheme: resolve(theme) };
|
|
applyDocument(theme);
|
|
state = {
|
|
...state,
|
|
// 无有效存储 → 默认开启 BGM;显式 "off" 才关闭
|
|
soundEnabled: readStored(SOUND_KEY) !== "off",
|
|
};
|
|
}
|
|
|
|
let hydrateRequested = false;
|
|
/** Idempotent, safe before mount (no-ops on the server). */
|
|
function hydrateIfNeeded() {
|
|
if (hydrateRequested) return;
|
|
hydrateRequested = true;
|
|
hydrate();
|
|
emit();
|
|
}
|
|
|
|
function onSystemChange() {
|
|
if (state.theme !== "system") return;
|
|
const resolved = resolve("system");
|
|
applyDocument("system");
|
|
setState({ resolvedTheme: resolved });
|
|
}
|
|
|
|
function setTheme(next: ThemeSetting) {
|
|
hydrateIfNeeded();
|
|
writeStored(THEME_KEY, next);
|
|
applyDocument(next);
|
|
setState({ theme: next, resolvedTheme: resolve(next) });
|
|
}
|
|
|
|
function setSoundEnabled(next: boolean) {
|
|
hydrateIfNeeded();
|
|
writeStored(SOUND_KEY, next ? "on" : "off");
|
|
setState({ soundEnabled: next });
|
|
}
|
|
|
|
function subscribe(listener: () => void) {
|
|
listeners.add(listener);
|
|
return () => {
|
|
listeners.delete(listener);
|
|
};
|
|
}
|
|
|
|
const getSnapshot = () => state;
|
|
|
|
// Hydration-safe SSR snapshot: must equal what the server rendered
|
|
// (system/light defaults per product decision; sound defaults ON — the
|
|
// audio only starts after the first user gesture, see useBgm). React uses
|
|
// this for the hydration render, then re-renders with the live snapshot
|
|
// once the client store hydrates.
|
|
const SERVER_SNAPSHOT: ShellMediaState = {
|
|
theme: "system",
|
|
resolvedTheme: "light",
|
|
soundEnabled: true,
|
|
};
|
|
|
|
function getServerSnapshot(): ShellMediaState {
|
|
return SERVER_SNAPSHOT;
|
|
}
|
|
|
|
export function useShellMedia(): ShellMediaState & {
|
|
setTheme: (next: ThemeSetting) => void;
|
|
setSoundEnabled: (next: boolean) => void;
|
|
} {
|
|
// Defer store hydration to the first effect tick (original: mounted flag
|
|
// + useEffect). Hydrating at module evaluation would run BEFORE React's
|
|
// hydration pass and flip aria-labels/classes under the SSR HTML — the
|
|
// exact "attributes of the server rendered HTML didn't match" warning.
|
|
useEffect(() => {
|
|
hydrateIfNeeded();
|
|
}, []);
|
|
const snapshot = useSyncExternalStore(
|
|
subscribe,
|
|
getSnapshot,
|
|
getServerSnapshot
|
|
);
|
|
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<typeof useShellMedia>;
|