Spaces:
Running
Running
File size: 910 Bytes
a32aee9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import { useCallback, useEffect, useState } from "react";
/**
* useState mirrored into localStorage. Used for auth session, selected model,
* and API keys so a reload doesn't lose them (same convenience the Streamlit
* session_state provided).
*/
export function useLocalStorage<T>(key: string, initial: T) {
const [value, setValue] = useState<T>(() => {
try {
const raw = window.localStorage.getItem(key);
return raw !== null ? (JSON.parse(raw) as T) : initial;
} catch {
return initial;
}
});
useEffect(() => {
try {
window.localStorage.setItem(key, JSON.stringify(value));
} catch {
/* storage full or unavailable — ignore */
}
}, [key, value]);
const remove = useCallback(() => {
try {
window.localStorage.removeItem(key);
} catch {
/* ignore */
}
}, [key]);
return [value, setValue, remove] as const;
}
|