|
| 1 | +import { useCallback, useEffect, useState } from 'react'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Hook to detect if we're running on the client side after hydration |
| 5 | + */ |
| 6 | +function useIsClient() { |
| 7 | + const [isClient, setIsClient] = useState(false); |
| 8 | + |
| 9 | + useEffect(() => { |
| 10 | + setIsClient(true); |
| 11 | + }, []); |
| 12 | + |
| 13 | + return isClient; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Custom hook for persisting state to localStorage with SSR compatibility. |
| 18 | + * Prevents hydration mismatches by using default values during SSR. |
| 19 | + */ |
| 20 | +export function usePersistentState<T>( |
| 21 | + key: string, |
| 22 | + defaultValue: T |
| 23 | +): [T, (value: T | ((prev: T) => T)) => void] { |
| 24 | + const isClient = useIsClient(); |
| 25 | + |
| 26 | + // Initialize state with localStorage value only on client, default on server |
| 27 | + const [state, setState] = useState<T>(() => { |
| 28 | + if (!isClient) { |
| 29 | + return defaultValue; |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + const item = window.localStorage.getItem(key); |
| 34 | + return item ? JSON.parse(item) : defaultValue; |
| 35 | + } catch (error) { |
| 36 | + console.error(`Error reading localStorage key "${key}":`, error); |
| 37 | + return defaultValue; |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + // Sync with localStorage when client-side and key changes |
| 42 | + useEffect(() => { |
| 43 | + if (!isClient) return; |
| 44 | + |
| 45 | + try { |
| 46 | + const item = window.localStorage.getItem(key); |
| 47 | + if (item) { |
| 48 | + const parsedValue = JSON.parse(item); |
| 49 | + setState(parsedValue); |
| 50 | + } |
| 51 | + } catch (error) { |
| 52 | + console.error(`Error reading localStorage key "${key}":`, error); |
| 53 | + } |
| 54 | + }, [key, isClient]); |
| 55 | + |
| 56 | + const setPersistentState = useCallback( |
| 57 | + (value: T | ((prev: T) => T)) => { |
| 58 | + try { |
| 59 | + setState((prevState) => { |
| 60 | + // Allow function updates |
| 61 | + const valueToStore = value instanceof Function ? value(prevState) : value; |
| 62 | + |
| 63 | + // Only persist to localStorage on client side |
| 64 | + if (isClient && typeof window !== 'undefined') { |
| 65 | + window.localStorage.setItem(key, JSON.stringify(valueToStore)); |
| 66 | + } |
| 67 | + |
| 68 | + return valueToStore; |
| 69 | + }); |
| 70 | + } catch (error) { |
| 71 | + console.error(`Error setting localStorage key "${key}":`, error); |
| 72 | + } |
| 73 | + }, |
| 74 | + [key, isClient] |
| 75 | + ); |
| 76 | + |
| 77 | + return [state, setPersistentState]; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Specialized hook for accordion states in the sidebar navigation. |
| 82 | + * Manages multiple accordion sections with their expanded/collapsed states. |
| 83 | + */ |
| 84 | +export function useAccordionStates(storageKey = 'sidebar-accordion-states') { |
| 85 | + const [accordionStates, setAccordionStates] = usePersistentState< |
| 86 | + Record<string, boolean> |
| 87 | + >(storageKey, {}); |
| 88 | + |
| 89 | + const toggleAccordion = useCallback( |
| 90 | + (sectionTitle: string, defaultState = true) => { |
| 91 | + setAccordionStates((prev) => { |
| 92 | + const currentState = prev[sectionTitle] ?? defaultState; |
| 93 | + return { |
| 94 | + ...prev, |
| 95 | + [sectionTitle]: !currentState, |
| 96 | + }; |
| 97 | + }); |
| 98 | + }, |
| 99 | + [setAccordionStates] |
| 100 | + ); |
| 101 | + |
| 102 | + const getAccordionState = useCallback( |
| 103 | + (sectionTitle: string, defaultState = true) => { |
| 104 | + return accordionStates[sectionTitle] ?? defaultState; |
| 105 | + }, |
| 106 | + [accordionStates] |
| 107 | + ); |
| 108 | + |
| 109 | + const setAccordionState = useCallback( |
| 110 | + (sectionTitle: string, isExpanded: boolean) => { |
| 111 | + setAccordionStates((prev) => ({ |
| 112 | + ...prev, |
| 113 | + [sectionTitle]: isExpanded, |
| 114 | + })); |
| 115 | + }, |
| 116 | + [setAccordionStates] |
| 117 | + ); |
| 118 | + |
| 119 | + return { |
| 120 | + toggleAccordion, |
| 121 | + getAccordionState, |
| 122 | + setAccordionState, |
| 123 | + }; |
| 124 | +} |
0 commit comments