"use client"; import type React from "react"; import { createContext, useState, useContext, useEffect } from "react"; type Theme = "light" | "dark"; type ThemeContextType = { theme: Theme; toggleTheme: () => void; }; const ThemeContext = createContext(undefined); export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ children, }) => { const [theme, setTheme] = useState("light"); const [isInitialized, setIsInitialized] = useState(false); useEffect(() => { // This code will only run on the client side const savedTheme = localStorage.getItem("theme") as Theme | null; const initialTheme = savedTheme || "light"; // Default to light theme setTheme(initialTheme); setIsInitialized(true); }, []); useEffect(() => { if (isInitialized) { localStorage.setItem("theme", theme); if (theme === "dark") { document.documentElement.classList.add("dark"); } else { document.documentElement.classList.remove("dark"); } } }, [theme, isInitialized]); const toggleTheme = () => { setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light")); }; return ( {children} ); }; export const useTheme = () => { const context = useContext(ThemeContext); if (context === undefined) { throw new Error("useTheme must be used within a ThemeProvider"); } return context; };