Request history navigator

This commit is contained in:
Gregory Schier
2023-04-09 15:26:54 -07:00
parent ab5681c7ad
commit 38ba8625d8
20 changed files with 277 additions and 67 deletions

View File

@@ -0,0 +1,31 @@
import { useEffect } from 'react';
import { createGlobalState, useEffectOnce, useLocalStorage } from 'react-use';
import { useActiveRequestId } from './useActiveRequestId';
const useHistoryState = createGlobalState<string[]>([]);
export function useRecentRequests() {
const [history, setHistory] = useHistoryState();
const activeRequestId = useActiveRequestId();
const [lsState, setLSState] = useLocalStorage<string[]>('recent_requests', []);
useEffect(() => {
setLSState(history);
}, [history, setLSState]);
useEffectOnce(() => {
if (lsState) {
setHistory(lsState);
}
});
useEffect(() => {
setHistory((h: string[]) => {
if (activeRequestId === null) return h;
const withoutCurrentRequest = h.filter((id) => id !== activeRequestId);
return [activeRequestId, ...withoutCurrentRequest];
});
}, [activeRequestId, setHistory]);
return history;
}