"use client"; import { createContext, useContext, useEffect, useState } from "react"; export type Theme = "dark" | "light"; type ThemeContextValue = { theme: Theme; toggle: () => void; }; const ThemeContext = createContext(null); const STORAGE_KEY = "oudelaa.theme"; function applyTheme(theme: Theme) { if (typeof document === "undefined") return; const root = document.documentElement; root.classList.remove("theme-dark", "theme-light"); root.classList.add(`theme-${theme}`); } export function ThemeProvider({ children }: { children: React.ReactNode }) { const [theme, setTheme] = useState("dark"); useEffect(() => { const stored = window.localStorage.getItem(STORAGE_KEY) as Theme | null; const initial = stored ?? "dark"; setTheme(initial); applyTheme(initial); }, []); const toggle = () => { setTheme((prev) => { const next = prev === "dark" ? "light" : "dark"; window.localStorage.setItem(STORAGE_KEY, next); applyTheme(next); return next; }); }; return {children}; } export function useTheme() { const ctx = useContext(ThemeContext); if (!ctx) throw new Error("useTheme must be used within ThemeProvider"); return ctx; }