mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-08 17:03:53 +00:00
2c47d90f34
* chore(lint): configure project-wide linter * chore(lint): improve linting in files * chore(lint): hide warnings in CI
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
/**
|
|
* @description utility hook to handle state in local storage
|
|
* @param key
|
|
* @param initialValue
|
|
*/
|
|
export const useLocalStorage = <T>(key: string, initialValue: T): [T, (value: T | ((val: T) => T)) => void] => {
|
|
const [storedValue, setStoredValue] = useState<T>(() => {
|
|
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.error(error);
|
|
}
|
|
};
|
|
return [storedValue, setValue];
|
|
};
|