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
89
90
91
92
93
94
95
96
|
"use client"
import {
createContext,
type ReactNode,
useContext,
useEffect,
useState,
} from "react"
import { analytics } from "@/lib/analytics"
type ViewMode = "graph" | "list"
interface ViewModeContextType {
viewMode: ViewMode
setViewMode: (mode: ViewMode) => void
isInitialized: boolean
}
const ViewModeContext = createContext<ViewModeContextType | undefined>(
undefined,
)
// Cookie utility functions
const setCookie = (name: string, value: string, days = 365) => {
if (typeof document === "undefined") return
const expires = new Date()
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000)
document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`
}
const getCookie = (name: string): string | null => {
if (typeof document === "undefined") return null
const nameEQ = `${name}=`
const ca = document.cookie.split(";")
for (let i = 0; i < ca.length; i++) {
let c = ca[i]
if (!c) continue
while (c.charAt(0) === " ") c = c.substring(1, c.length)
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length)
}
return null
}
const isMobileDevice = () => {
if (typeof window === "undefined") return false
return window.innerWidth < 768
}
export function ViewModeProvider({ children }: { children: ReactNode }) {
// Start with a default that works for SSR
const [viewMode, setViewModeState] = useState<ViewMode>("graph")
const [isInitialized, setIsInitialized] = useState(false)
// Load preferences on the client side
useEffect(() => {
if (!isInitialized) {
// Check for saved preference first
const savedMode = getCookie("memoryViewMode")
if (savedMode === "list" || savedMode === "graph") {
setViewModeState(savedMode)
} else {
// If no saved preference, default to list on mobile, graph on desktop
setViewModeState(isMobileDevice() ? "list" : "graph")
}
setIsInitialized(true)
}
}, [isInitialized])
// Save to cookie whenever view mode changes
const handleSetViewMode = (mode: ViewMode) => {
analytics.viewModeChanged(mode)
setViewModeState(mode)
setCookie("memoryViewMode", mode)
}
return (
<ViewModeContext.Provider
value={{
viewMode,
setViewMode: handleSetViewMode,
isInitialized,
}}
>
{children}
</ViewModeContext.Provider>
)
}
export function useViewMode() {
const context = useContext(ViewModeContext)
if (!context) {
throw new Error("useViewMode must be used within a ViewModeProvider")
}
return context
}
|