mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 01:13:55 +00:00
67ddcd8c68
* feat/table add react-table * feat/table add protected table route * feat/table upgrade dependencies * feat/table support parsing of new properties Co-authored-by: DeepSource Bot <bot@deepsource.io>
29 lines
799 B
JavaScript
29 lines
799 B
JavaScript
import { useState } from 'react';
|
|
|
|
// Roughly from useHooks - useLocalStorage
|
|
|
|
export const useLocalStorage = (key, initialValue) => {
|
|
const [storedValue, setStoredValue] = useState(() => {
|
|
try {
|
|
const item = window.localStorage.getItem(`ontime-${key}`);
|
|
return item ? JSON.parse(item) : initialValue;
|
|
} catch (error) {
|
|
return initialValue;
|
|
}
|
|
});
|
|
|
|
const setValue = (value) => {
|
|
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];
|
|
}
|