refactor: userpreferences are now in the DB
This commit is contained in:
@@ -14,34 +14,22 @@ const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
|
||||
|
||||
interface ThemeProviderProps {
|
||||
children: ReactNode;
|
||||
initialTheme?: Theme;
|
||||
}
|
||||
|
||||
export function ThemeProvider({ children }: ThemeProviderProps) {
|
||||
const [theme, setThemeState] = useState<Theme>('dark');
|
||||
export function ThemeProvider({ children, initialTheme = 'dark' }: ThemeProviderProps) {
|
||||
const [theme, setThemeState] = useState<Theme>(initialTheme);
|
||||
const [mounted, setMounted] = useState(false);
|
||||
|
||||
// Hydration safe initialization
|
||||
useEffect(() => {
|
||||
setMounted(true);
|
||||
|
||||
// Check localStorage first
|
||||
const savedTheme = localStorage.getItem('theme') as Theme | null;
|
||||
if (savedTheme) {
|
||||
setThemeState(savedTheme);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback to system preference
|
||||
if (window.matchMedia('(prefers-color-scheme: light)').matches) {
|
||||
setThemeState('light');
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Apply theme class to document
|
||||
useEffect(() => {
|
||||
if (mounted) {
|
||||
document.documentElement.className = theme;
|
||||
localStorage.setItem('theme', theme);
|
||||
}
|
||||
}, [theme, mounted]);
|
||||
|
||||
@@ -55,7 +43,7 @@ export function ThemeProvider({ children }: ThemeProviderProps) {
|
||||
|
||||
return (
|
||||
<ThemeContext.Provider value={{ theme, toggleTheme, setTheme }}>
|
||||
<div className={mounted ? theme : 'dark'}>
|
||||
<div className={mounted ? theme : initialTheme}>
|
||||
{children}
|
||||
</div>
|
||||
</ThemeContext.Provider>
|
||||
|
||||
Reference in New Issue
Block a user