Add theme persistence - syncs with user preferences
This commit is contained in:
parent
296cc4d2e2
commit
9a7246f55a
1 changed files with 20 additions and 1 deletions
|
|
@ -4,15 +4,34 @@ import * as React from "react"
|
||||||
import { Moon, Sun } from "lucide-react"
|
import { Moon, Sun } from "lucide-react"
|
||||||
import { useTheme } from "next-themes"
|
import { useTheme } from "next-themes"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
|
import { usePreferences } from "@/contexts/preferences-context"
|
||||||
|
|
||||||
export function ThemeToggle() {
|
export function ThemeToggle() {
|
||||||
const { theme, setTheme } = useTheme()
|
const { theme, setTheme } = useTheme()
|
||||||
|
const { preferences, updatePreferences, loading: prefsLoading } = usePreferences()
|
||||||
const [mounted, setMounted] = React.useState(false)
|
const [mounted, setMounted] = React.useState(false)
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
setMounted(true)
|
setMounted(true)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
// Sync theme from preferences when loaded
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (!prefsLoading && preferences.theme && mounted) {
|
||||||
|
// Only sync if the preference is different from current theme
|
||||||
|
if (preferences.theme !== theme && preferences.theme !== "system") {
|
||||||
|
setTheme(preferences.theme)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [prefsLoading, preferences.theme, mounted])
|
||||||
|
|
||||||
|
const handleToggle = () => {
|
||||||
|
const newTheme = theme === "dark" ? "light" : "dark"
|
||||||
|
setTheme(newTheme)
|
||||||
|
// Persist to backend
|
||||||
|
updatePreferences({ theme: newTheme })
|
||||||
|
}
|
||||||
|
|
||||||
if (!mounted) {
|
if (!mounted) {
|
||||||
return (
|
return (
|
||||||
<Button variant="ghost" size="icon" className="h-9 w-9">
|
<Button variant="ghost" size="icon" className="h-9 w-9">
|
||||||
|
|
@ -26,7 +45,7 @@ export function ThemeToggle() {
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="icon"
|
size="icon"
|
||||||
className="h-9 w-9"
|
className="h-9 w-9"
|
||||||
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
|
onClick={handleToggle}
|
||||||
>
|
>
|
||||||
{theme === "dark" ? (
|
{theme === "dark" ? (
|
||||||
<Sun className="h-4 w-4" />
|
<Sun className="h-4 w-4" />
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue