mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-15 16:23:25 +01:00
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { createGlobalState, useEffectOnce, useLocalStorage } from 'react-use';
|
|
import { useActiveRequestId } from './useActiveRequestId';
|
|
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
|
|
|
const useHistoryState = createGlobalState<string[]>([]);
|
|
|
|
export function useRecentRequests() {
|
|
const activeWorkspaceId = useActiveWorkspaceId();
|
|
const activeRequestId = useActiveRequestId();
|
|
const [history, setHistory] = useHistoryState();
|
|
const [lsState, setLSState] = useLocalStorage<string[]>(
|
|
'recent_requests::' + activeWorkspaceId,
|
|
[],
|
|
);
|
|
|
|
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.slice(1);
|
|
}
|