mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 17:40:18 +08:00
46 lines
993 B
TypeScript
46 lines
993 B
TypeScript
/**
|
|
* useTheme hook for managing theme throughout the application
|
|
*/
|
|
import { useState, useSyncExternalStore } from "react";
|
|
import {
|
|
setTheme,
|
|
getStoredTheme,
|
|
initializeTheme,
|
|
type Theme,
|
|
} from "@/lib/theme";
|
|
|
|
// Use useSyncExternalStore for safe client-side initialization
|
|
const emptySubscribe = () => () => {};
|
|
function useIsClient() {
|
|
return useSyncExternalStore(
|
|
emptySubscribe,
|
|
() => true,
|
|
() => false,
|
|
);
|
|
}
|
|
|
|
export function useTheme() {
|
|
const isClient = useIsClient();
|
|
const [theme, setThemeState] = useState<Theme>(() => {
|
|
// Lazy initialization - safe for SSR
|
|
if (typeof window !== "undefined") {
|
|
return initializeTheme();
|
|
}
|
|
return "light";
|
|
});
|
|
const isLoaded = isClient;
|
|
|
|
const updateTheme = (newTheme: Theme) => {
|
|
setTheme(newTheme);
|
|
setThemeState(newTheme);
|
|
};
|
|
|
|
return {
|
|
theme: theme || "light",
|
|
isLoaded,
|
|
setTheme: updateTheme,
|
|
isDark: theme === "dark",
|
|
isLight: theme === "light",
|
|
};
|
|
}
|