Fix recent requests loading on startup

This commit is contained in:
Gregory Schier
2023-10-28 18:27:18 -07:00
parent c4a8603b81
commit 6202e59daa
4 changed files with 16 additions and 14 deletions

View File

@@ -14,23 +14,26 @@ export function useRecentRequests() {
[],
);
useEffect(() => {
setLSState(history);
}, [history, setLSState]);
// Load local storage state on initial render
useEffectOnce(() => {
if (lsState) {
setHistory(lsState);
}
});
// Update local storage state when history changes
useEffect(() => {
setHistory((h: string[]) => {
if (activeRequestId === null) return h;
const withoutCurrentRequest = h.filter((id) => id !== activeRequestId);
setLSState(history);
}, [history, setLSState]);
// Set history when active request changes
useEffect(() => {
setHistory((currentHistory: string[]) => {
if (activeRequestId === null) return currentHistory;
const withoutCurrentRequest = currentHistory.filter((id) => id !== activeRequestId);
return [activeRequestId, ...withoutCurrentRequest];
});
}, [activeRequestId, setHistory]);
return history.slice(1);
return history;
}