mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 06:28:51 +00:00
feat(nui): useLocalStorage hook for client storage
This commit is contained in:
1 parent
74f43efddb
commit
274f2bf286
1 file changed
+49
@@ -0,0 +1,49 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
// TODO: Integrate debug log to fetch when that branch is merged
|
||||
|
||||
/**
|
||||
* A LocalStorage hook designed to behave like the useState hook.
|
||||
* @param {string} key - The unique identifier to be used as a key
|
||||
* @param {string} intialValue - The initialValue to be used
|
||||
* if there is no savedState
|
||||
*/
|
||||
|
||||
function useLocalStorage<T>(key: string, initialValue: T) {
|
||||
// Serves as a nice identifying prefix for the key
|
||||
const UNIQ_PREFIX = 'npwd_phone_';
|
||||
const [localVal, setLocalVal] = useState<T>(() => {
|
||||
try {
|
||||
const data = window.localStorage.getItem(UNIQ_PREFIX + key);
|
||||
// return stored or initValue if none stored
|
||||
return data ? JSON.parse(data) : initialValue;
|
||||
} catch (e) {
|
||||
// TODO: Integrate debug error state here
|
||||
console.log(e);
|
||||
// We still return initVal if error is thrown
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
|
||||
// Trying to rep useState setter pretty close just with localStorage
|
||||
const setVal = (value: T | ((val: T) => T)) => {
|
||||
try {
|
||||
// Same API as useState meaning we can accept a function for value
|
||||
const valForStore = value instanceof Function ? value(localVal) : value;
|
||||
// Save that state so we repli useState
|
||||
setLocalVal(valForStore);
|
||||
// Set that localStorage yo
|
||||
window.localStorage.setItem(
|
||||
UNIQ_PREFIX + key,
|
||||
JSON.stringify(valForStore)
|
||||
);
|
||||
} catch (e) {
|
||||
// TODO: Also integrate debug log state error here
|
||||
console.log(e);
|
||||
}
|
||||
};
|
||||
|
||||
return [localVal, setVal];
|
||||
}
|
||||
|
||||
export default useLocalStorage;
|
||||
Reference in new issue
Block a user