in paimon-web-ui/src/hooks/localStorage/index.tsx [20:49]
export function useLocalStorage(key: string, initialValue: any) {
const readValue = useCallback(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (error) {
console.warn(error);
return initialValue;
}
}, [key, initialValue]);
const [localState, setLocalState] = useState(readValue);
const handleSetState = useCallback(
(value: any) => {
try {
const nextState =
typeof value === "function" ? value(localState) : value;
window.localStorage.setItem(key, JSON.stringify(nextState));
setLocalState(nextState);
window.dispatchEvent(new Event("local-storage-change"));
} catch (error) {
console.warn(error);
}
},
[key, localState]
);
return [localState, handleSetState];
}