claude-code

This commit is contained in:
ashutoshpythoncs@gmail.com
2026-03-31 18:58:05 +05:30
parent a2a44a5841
commit b564857c0b
2148 changed files with 564518 additions and 2 deletions

74
web/lib/theme.tsx Normal file
View File

@@ -0,0 +1,74 @@
'use client'
import React, { createContext, useContext, useEffect, useState } from 'react'
export type Theme = 'dark' | 'light' | 'system'
export type ResolvedTheme = 'dark' | 'light'
interface ThemeContextValue {
theme: Theme
resolvedTheme: ResolvedTheme
setTheme: (theme: Theme) => void
}
const ThemeContext = createContext<ThemeContextValue | null>(null)
export function ThemeProvider({
children,
defaultTheme = 'dark',
storageKey = 'ui-theme',
}: {
children: React.ReactNode
defaultTheme?: Theme
storageKey?: string
}) {
const [theme, setThemeState] = useState<Theme>(defaultTheme)
const [resolvedTheme, setResolvedTheme] = useState<ResolvedTheme>('dark')
useEffect(() => {
const stored = localStorage.getItem(storageKey) as Theme | null
if (stored && ['dark', 'light', 'system'].includes(stored)) {
setThemeState(stored)
}
}, [storageKey])
useEffect(() => {
const root = document.documentElement
const apply = (resolved: ResolvedTheme) => {
setResolvedTheme(resolved)
if (resolved === 'light') {
root.classList.add('light')
} else {
root.classList.remove('light')
}
}
if (theme === 'system') {
const mq = window.matchMedia('(prefers-color-scheme: light)')
apply(mq.matches ? 'light' : 'dark')
const handler = (e: MediaQueryListEvent) => apply(e.matches ? 'light' : 'dark')
mq.addEventListener('change', handler)
return () => mq.removeEventListener('change', handler)
} else {
apply(theme)
}
}, [theme])
const setTheme = (t: Theme) => {
setThemeState(t)
localStorage.setItem(storageKey, t)
}
return (
<ThemeContext.Provider value={{ theme, resolvedTheme, setTheme }}>
{children}
</ThemeContext.Provider>
)
}
export function useTheme(): ThemeContextValue {
const ctx = useContext(ThemeContext)
if (!ctx) throw new Error('useTheme must be used within a ThemeProvider')
return ctx
}