forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDashboardSettings.ts
More file actions
88 lines (77 loc) · 2.87 KB
/
Copy pathuseDashboardSettings.ts
File metadata and controls
88 lines (77 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// src/components/dashboard/useDashboardSettings.ts
"use client"
import { useCallback, useEffect, useState } from "react"
import { STORAGE_KEYS } from "@/lib/storage-keys"
import { DASHBOARD_SETTINGS_DEFAULTS, type DashboardSettings } from "@/types/api"
const DEFAULTS = DASHBOARD_SETTINGS_DEFAULTS
function useDashboardSettings() {
const [settings, setSettings] = useState<DashboardSettings>(DEFAULTS)
useEffect(() => {
let cancelled = false
async function load() {
try {
const res = await fetch("/api/settings/dashboard")
if (!res.ok) return
const data = (await res.json()) as DashboardSettings
if (cancelled) return
// One-time migration: if DB has defaults and localStorage has data, migrate
const legacy = localStorage.getItem(STORAGE_KEYS.DASHBOARD_SETTINGS)
if (legacy) {
try {
const parsed = { ...DEFAULTS, ...(JSON.parse(legacy) as Partial<DashboardSettings>) }
// Save to DB, then clear localStorage
fetch("/api/settings/dashboard", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(parsed),
})
localStorage.removeItem(STORAGE_KEYS.DASHBOARD_SETTINGS)
if (!cancelled) setSettings(parsed)
return
} catch {
localStorage.removeItem(STORAGE_KEYS.DASHBOARD_SETTINGS)
}
}
setSettings({ ...DEFAULTS, ...data })
} catch {
// Fall back to defaults on network error
}
}
load()
return () => {
cancelled = true
}
}, [])
const update = useCallback(
<K extends keyof DashboardSettings>(key: K, value: DashboardSettings[K]) => {
setSettings((prev) => {
const next = { ...prev, [key]: value }
// Fire-and-forget save inside updater so `next` is guaranteed correct
// even under rapid sequential calls (prev is always the latest queued state).
// PUT is idempotent — duplicate calls in StrictMode are harmless.
fetch("/api/settings/dashboard", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(next),
})
.then((res) => {
if (!res.ok) throw new Error(`PUT failed: ${res.status}`)
})
.catch(() => {
// Resync from server to revert the optimistic toggle
fetch("/api/settings/dashboard")
.then((res) => (res.ok ? res.json() : null))
.then((data) => {
if (data) setSettings({ ...DEFAULTS, ...(data as Partial<DashboardSettings>) })
})
.catch(() => {})
})
return next
})
},
[]
)
return { settings, update }
}
export type { DashboardSettings } from "@/types/api"
export { useDashboardSettings }