Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API. Co-authored-by: Cursor <cursoragent@cursor.com>
96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useState,
|
|
type ReactNode,
|
|
} from "react";
|
|
|
|
export type ThemeMode = "light" | "dark" | "system";
|
|
|
|
type ThemeContextValue = {
|
|
theme: ThemeMode;
|
|
resolvedTheme: "light" | "dark";
|
|
setTheme: (theme: ThemeMode) => void;
|
|
};
|
|
|
|
const ThemeContext = createContext<ThemeContextValue | null>(null);
|
|
const STORAGE_KEY = "nomadro-theme";
|
|
const DEFAULT_THEME: ThemeMode = "dark";
|
|
|
|
function systemTheme(): "light" | "dark" {
|
|
if (typeof window === "undefined") return "dark";
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
? "dark"
|
|
: "light";
|
|
}
|
|
|
|
function resolveMode(mode: ThemeMode): "light" | "dark" {
|
|
if (mode === "system") return systemTheme();
|
|
return mode;
|
|
}
|
|
|
|
export function ThemeProvider({ children }: { children: ReactNode }) {
|
|
const [theme, setThemeState] = useState<ThemeMode>(DEFAULT_THEME);
|
|
const [resolvedTheme, setResolvedTheme] = useState<"light" | "dark">("dark");
|
|
|
|
const apply = useCallback((mode: "light" | "dark") => {
|
|
const root = document.documentElement;
|
|
root.classList.remove("light", "dark");
|
|
root.classList.add(mode);
|
|
root.dataset.theme = mode;
|
|
setResolvedTheme(mode);
|
|
}, []);
|
|
|
|
const setTheme = useCallback(
|
|
(next: ThemeMode) => {
|
|
setThemeState(next);
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, next);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
apply(resolveMode(next));
|
|
},
|
|
[apply],
|
|
);
|
|
|
|
useEffect(() => {
|
|
let stored: ThemeMode = DEFAULT_THEME;
|
|
try {
|
|
const raw = localStorage.getItem(STORAGE_KEY) as ThemeMode | null;
|
|
if (raw === "light" || raw === "dark" || raw === "system") stored = raw;
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
setThemeState(stored);
|
|
apply(resolveMode(stored));
|
|
|
|
const media = window.matchMedia("(prefers-color-scheme: dark)");
|
|
const onChange = () => {
|
|
const cur = (localStorage.getItem(STORAGE_KEY) as ThemeMode) || DEFAULT_THEME;
|
|
if (cur === "system") apply(media.matches ? "dark" : "light");
|
|
};
|
|
media.addEventListener("change", onChange);
|
|
return () => media.removeEventListener("change", onChange);
|
|
}, [apply]);
|
|
|
|
return (
|
|
<ThemeContext.Provider value={{ theme, resolvedTheme, setTheme }}>
|
|
{children}
|
|
</ThemeContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useTheme() {
|
|
const ctx = useContext(ThemeContext);
|
|
if (!ctx) throw new Error("useTheme must be used within ThemeProvider");
|
|
return ctx;
|
|
}
|
|
|
|
/** Inline before paint — keep in sync with STORAGE_KEY; default dark */
|
|
export const themeInitScript = `(function(){try{var t=localStorage.getItem('${STORAGE_KEY}');var m=t==='light'||t==='dark'?t:(t==='system'?(window.matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light'):'dark');var r=document.documentElement;r.classList.remove('light','dark');r.classList.add(m);r.dataset.theme=m;}catch(e){document.documentElement.classList.add('dark');document.documentElement.dataset.theme='dark';}})();`;
|