2026-06-25 00:28:23 +00:00
|
|
|
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react'
|
|
|
|
|
|
|
|
|
|
export type Theme = 'light' | 'dark'
|
|
|
|
|
|
|
|
|
|
type ThemeContextValue = {
|
|
|
|
|
theme: Theme
|
|
|
|
|
toggle: () => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ThemeContext = createContext<ThemeContextValue | null>(null)
|
|
|
|
|
const STORAGE_KEY = 'atc-command-center-theme'
|
|
|
|
|
|
|
|
|
|
function readStored(): Theme {
|
|
|
|
|
const v = localStorage.getItem(STORAGE_KEY)
|
2026-06-27 19:37:50 +00:00
|
|
|
return v === 'dark' || v === 'light' ? v : 'dark'
|
2026-06-25 00:28:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ThemeProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
const [theme, setTheme] = useState<Theme>(() => {
|
2026-06-27 19:37:50 +00:00
|
|
|
if (typeof window === 'undefined') return 'dark'
|
2026-06-25 00:28:23 +00:00
|
|
|
return readStored()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const root = document.documentElement
|
|
|
|
|
root.classList.remove('light', 'dark')
|
|
|
|
|
root.classList.add(theme)
|
|
|
|
|
localStorage.setItem(STORAGE_KEY, theme)
|
|
|
|
|
}, [theme])
|
|
|
|
|
|
|
|
|
|
const toggle = () => setTheme((t) => (t === 'light' ? 'dark' : 'light'))
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ThemeContext.Provider value={{ theme, toggle }}>
|
|
|
|
|
{children}
|
|
|
|
|
</ThemeContext.Provider>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useTheme() {
|
|
|
|
|
const ctx = useContext(ThemeContext)
|
|
|
|
|
if (!ctx) throw new Error('useTheme outside ThemeProvider')
|
|
|
|
|
return ctx
|
|
|
|
|
}
|