mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-28 11:47:01 +02:00
Start on plugin ctx API (#64)
This commit is contained in:
47
src-web/hooks/useRecentCookieJars.ts
Normal file
47
src-web/hooks/useRecentCookieJars.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { useEffect, useMemo } from 'react';
|
||||
import { getKeyValue } from '../lib/keyValueStore';
|
||||
import { useActiveCookieJar } from './useActiveCookieJar';
|
||||
import { useActiveWorkspace } from './useActiveWorkspace';
|
||||
import { useCookieJars } from './useCookieJars';
|
||||
import { useKeyValue } from './useKeyValue';
|
||||
|
||||
const kvKey = (workspaceId: string) => 'recent_cookie_jars::' + workspaceId;
|
||||
const namespace = 'global';
|
||||
const fallback: string[] = [];
|
||||
|
||||
export function useRecentCookieJars() {
|
||||
const cookieJars = useCookieJars();
|
||||
const activeWorkspace = useActiveWorkspace();
|
||||
const [activeCookieJar] = useActiveCookieJar();
|
||||
const activeCookieJarId = activeCookieJar?.id ?? null;
|
||||
const kv = useKeyValue<string[]>({
|
||||
key: kvKey(activeWorkspace?.id ?? 'n/a'),
|
||||
namespace,
|
||||
fallback,
|
||||
});
|
||||
|
||||
// Set history when active request changes
|
||||
useEffect(() => {
|
||||
kv.set((currentHistory: string[]) => {
|
||||
if (activeCookieJarId === null) return currentHistory;
|
||||
const withoutCurrent = currentHistory.filter((id) => id !== activeCookieJarId);
|
||||
return [activeCookieJarId, ...withoutCurrent];
|
||||
}).catch(console.error);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [activeCookieJarId]);
|
||||
|
||||
const onlyValidIds = useMemo(
|
||||
() => kv.value?.filter((id) => cookieJars.data?.some((e) => e.id === id)) ?? [],
|
||||
[kv.value, cookieJars],
|
||||
);
|
||||
|
||||
return onlyValidIds;
|
||||
}
|
||||
|
||||
export async function getRecentCookieJars(workspaceId: string) {
|
||||
return getKeyValue<string[]>({
|
||||
namespace,
|
||||
key: kvKey(workspaceId),
|
||||
fallback,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user