Merge pull request #94 from takshakmudgal/ui/theme-persistence

fix(theme): add persistence & update icon
This commit is contained in:
Akhilesh Sudhakar 2025-04-29 01:08:44 +05:30 committed by GitHub
commit 739b7caa46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 5 deletions

View file

@ -13,6 +13,7 @@ import {
ChevronLeftIcon,
ChevronRightIcon,
Moon,
Sun,
HelpCircle
} from "lucide-react";
import { getProjectConfig } from "@/app/actions/project_actions";
@ -213,7 +214,7 @@ export default function Sidebar({ projectId, useRag, useAuth, collapsed = false,
text-zinc-600 dark:text-zinc-400
`}
>
<Moon size={COLLAPSED_ICON_SIZE} />
{ theme == "light" ? <Moon size={COLLAPSED_ICON_SIZE} /> : <Sun size={COLLAPSED_ICON_SIZE} /> }
{!collapsed && <span>Appearance</span>}
</button>
</Tooltip>

View file

@ -21,15 +21,27 @@ export function ThemeProvider({
defaultTheme = 'light',
}: ThemeProviderProps) {
const [theme, setTheme] = useState<Theme>(defaultTheme)
useEffect(() => {
const root = window.document.documentElement
const root = document.documentElement
const storedTheme = localStorage.getItem("theme")
if (storedTheme === 'dark' || storedTheme === 'light') {
setTheme(storedTheme)
}
root.classList.remove('light', 'dark')
root.classList.add(theme)
}, [theme])
const toggleTheme = () => {
setTheme(theme === 'light' ? 'dark' : 'light')
setTheme((prevTheme) => {
const newTheme = prevTheme === 'light' ? 'dark' : 'light'
if (typeof window !== 'undefined') {
localStorage.setItem("theme", newTheme)
}
return newTheme
})
}
return (
@ -45,4 +57,4 @@ export function useTheme() {
throw new Error('useTheme must be used within a ThemeProvider')
}
return context
}
}