68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
"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;
|
|
}
|