d5a1228aac
- 暗色主题 1:1:ThemeModeProvider 原逻辑(localStorage 'theme' light/dark/system、system 跟随 prefers-color-scheme、<html> class 切换), CSS tokens 按原站 .dark 块翻值(--label/--background-deep/--code-*); shellMedia 重构为 useSyncExternalStore 单例 store(Header 与 Canvas 共享) - WebGL 主题分支:BackgroundFX 暗色渐变(#2c4bd5/#00000d/#00344C + outputMix .95/edgeIntensity -.82,原 sj 配置)、DotOverlay #0F1111、 LoadingCurtain #191b1b、玻璃模型暗色参数组(shininess 100/fresnel .72/ brightness .6/uDark=1 Hard-Light)与暗色 tint(hello #64c3ff、 footer #8e9dc4/#64c3ff) - /2026 路由:rewrite 补 2026(此前 404),workArticles 增加 /2026 占位文章 (原站内容被服务端口令保密无法读取,已注释说明);解锁屏→刷新→服务端 放行全链路 CDP 验证通过 - 逐页 <title>:app/work/[slug]/layout.tsx generateMetadata 映射作品名 (reunimos→Reunimos™),受保护 /2026 未解锁时 SSR 'Protected'(原站行为), 根标题改回原站 'HAOQI©2026'
175 lines
4.9 KiB
TypeScript
175 lines
4.9 KiB
TypeScript
"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;
|
|
* <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).
|
|
*
|
|
* 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<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);
|
|
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<typeof useShellMedia>;
|