ResearchRAG / frontend /src /hooks /useLocalStorage.ts
riezqidr's picture
feat: initialize frontend with React, Vite, and Tailwind CSS
a32aee9
Raw
History Blame Contribute Delete
910 Bytes
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;
}