feat: 添加滚动环境和媒体状态管理,优化头部组件功能,支持背景音乐和主题切换

This commit is contained in:
2026-09-01 14:39:37 +08:00
parent b8483e94a6
commit 602f1c82ed
8 changed files with 537 additions and 22 deletions
+67
View File
@@ -0,0 +1,67 @@
"use client";
import { useCallback, useEffect, useState } from "react";
/**
* Shell media state (theme + sound) — mirrors the original useShellMedia /
* useThemeMode contract: theme ∈ {light, dark, system}, persisted to
* localStorage; soundEnabled persisted likewise (original keys
* "hq-theme" / "hq-sound"; namespaced here). The THEME cycle is
* light → dark → system, keyboard [L]/[D]/[A] shortcuts live in the
* header component (original ASCIIThemeToggle behavior).
*/
export type ThemeSetting = "light" | "dark" | "system";
const THEME_KEY = "vh-theme";
const SOUND_KEY = "vh-sound";
function readStored(key: string): string | null {
try {
return window.localStorage.getItem(key);
} catch {
return null;
}
}
export function useShellMedia() {
const [theme, setThemeState] = useState<ThemeSetting>("light");
const [soundEnabled, setSoundEnabledState] = useState(false);
useEffect(() => {
const t = readStored(THEME_KEY);
if (t === "light" || t === "dark" || t === "system") setThemeState(t);
setSoundEnabledState(readStored(SOUND_KEY) === "1");
}, []);
// Theme is light-only in this port: system/dark resolve to light.
useEffect(() => {
document.documentElement.dataset.themeSetting = theme;
}, [theme]);
const setTheme = useCallback((next: ThemeSetting) => {
setThemeState(next);
try {
window.localStorage.setItem(THEME_KEY, next);
} catch {}
}, []);
const setSoundEnabled = useCallback((next: boolean) => {
setSoundEnabledState(next);
try {
window.localStorage.setItem(SOUND_KEY, next ? "1" : "0");
} catch {}
}, []);
return { theme, setTheme, soundEnabled, setSoundEnabled };
}
export type ShellMedia = ReturnType<typeof useShellMedia>;
let contextValue: ShellMedia | null = null;
/** Module-singleton variant (no provider tree needed for this port). */
export function useShellMediaSingleton(): ShellMedia {
const own = useShellMedia();
return contextValue ?? own;
}