import React, { createContext, useContext, useEffect, useState, ReactNode } from 'react'; import { useLocalStorage } from '../hooks/useLocalStorage'; type Theme = 'light' | 'dark'; interface ThemeContextType { theme: Theme; toggleTheme: () => void; } const ThemeContext = createContext(undefined); export const ThemeProvider: React.FC<{ children: ReactNode }> = ({ children }) => { const [theme, setTheme] = useLocalStorage('theme', 'light'); useEffect(() => { const root = window.document.documentElement; if (theme === 'dark') { root.classList.add('dark'); } else { root.classList.remove('dark'); } }, [theme]); const toggleTheme = () => { setTheme(prevTheme => (prevTheme === 'light' ? 'dark' : 'light')); }; return ( {children} ); }; export const useTheme = (): ThemeContextType => { const context = useContext(ThemeContext); if (context === undefined) { throw new Error('useTheme must be used within a ThemeProvider'); } return context; };