import { useEffect, useState } from 'react'; /** * @description utility hook to handle state in local storage * @param key * @param initialValue */ export const useLocalStorage = (key: string, initialValue: T): [T, (value: T | ((val: T) => T)) => void] => { const [storedValue, setStoredValue] = useState(() => { try { const item = window.localStorage.getItem(`ontime-${key}`); return item ? JSON.parse(item) : initialValue; } catch (error) { return initialValue; } }); useEffect(() => { const handleStorageChange = (event: StorageEvent) => { if (event.storageArea === window.localStorage && event.key === key) { try { const newValue = event.newValue ? JSON.parse(event.newValue) : initialValue; setStoredValue(newValue); } catch (_) { /* empty */ } } }; window.addEventListener('storage', handleStorageChange); return () => { window.removeEventListener('storage', handleStorageChange); }; }, [initialValue, key]); /** * @description Set value to local storage * @param value */ const setValue = (value: T | ((val: T) => T)) => { try { // Allow value to be a function so we have same API as useState const valueToStore = value instanceof Function ? value(storedValue) : value; setStoredValue(valueToStore); window.localStorage.setItem(`ontime-${key}`, JSON.stringify(valueToStore)); } catch (error) { console.log(error); } }; return [storedValue, setValue]; };