Split the 316-line vendors index into VendorToolbar, VendorGrid, VendorList, and PerPageSelector under components/vendors/. Page now owns only search-param state and query orchestration. Replace generic Error throws in lib/directus.ts with a DirectusError class carrying status, statusText, collection, url, and parsed body. Surface useLocalStorage read/write failures via console.warn instead of swallowing them.
26 lines
789 B
TypeScript
26 lines
789 B
TypeScript
import { useCallback, useEffect, useState } from 'react'
|
|
|
|
export function useLocalStorage<T>(key: string, initialValue: T): [T, (v: T) => void] {
|
|
const [value, setValue] = useState<T>(() => {
|
|
if (typeof window === 'undefined') return initialValue
|
|
try {
|
|
const raw = window.localStorage.getItem(key)
|
|
return raw != null ? (JSON.parse(raw) as T) : initialValue
|
|
} catch (err) {
|
|
console.warn(`useLocalStorage: failed to read "${key}"`, err)
|
|
return initialValue
|
|
}
|
|
})
|
|
|
|
useEffect(() => {
|
|
try {
|
|
window.localStorage.setItem(key, JSON.stringify(value))
|
|
} catch (err) {
|
|
console.warn(`useLocalStorage: failed to write "${key}"`, err)
|
|
}
|
|
}, [key, value])
|
|
|
|
const set = useCallback((v: T) => setValue(v), [])
|
|
return [value, set]
|
|
}
|