mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-30 14:12:07 +02:00
Request history navigator
This commit is contained in:
31
src-web/hooks/useRecentRequests.ts
Normal file
31
src-web/hooks/useRecentRequests.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user