mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-24 09:48:28 +02:00
A bunch of fixes
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { useEffect, useMemo } from 'react';
|
||||
import { getKeyValue } from '../lib/keyValueStore';
|
||||
import { useActiveCookieJar } from './useActiveCookieJar';
|
||||
import { useActiveWorkspace } from './useActiveWorkspace';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { getKeyValue, setKeyValue } from '../lib/keyValueStore';
|
||||
import { activeCookieJarIdAtom } from './useActiveCookieJar';
|
||||
import { activeWorkspaceIdAtom, useActiveWorkspace } from './useActiveWorkspace';
|
||||
import { useCookieJars } from './useCookieJars';
|
||||
import { useKeyValue } from './useKeyValue';
|
||||
|
||||
@@ -12,24 +13,12 @@ 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?.some((e) => e.id === id)) ?? [],
|
||||
[kv.value, cookieJars],
|
||||
@@ -38,6 +27,26 @@ export function useRecentCookieJars() {
|
||||
return onlyValidIds;
|
||||
}
|
||||
|
||||
export function useSubscribeRecentCookieJars() {
|
||||
useEffect(() => {
|
||||
return jotaiStore.sub(activeCookieJarIdAtom, async () => {
|
||||
const activeWorkspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
const activeCookieJarId = jotaiStore.get(activeCookieJarIdAtom);
|
||||
if (activeWorkspaceId == null) return;
|
||||
if (activeCookieJarId == null) return;
|
||||
|
||||
const key = kvKey(activeWorkspaceId);
|
||||
|
||||
const recentIds = await getKeyValue<string[]>({ namespace, key, fallback });
|
||||
if (recentIds[0] === activeCookieJarId) return; // Short-circuit
|
||||
|
||||
const withoutActiveId = recentIds.filter((id) => id !== activeCookieJarId);
|
||||
const value = [activeCookieJarId, ...withoutActiveId];
|
||||
await setKeyValue({ namespace, key, value });
|
||||
});
|
||||
}, []);
|
||||
}
|
||||
|
||||
export async function getRecentCookieJars(workspaceId: string) {
|
||||
return getKeyValue<string[]>({
|
||||
namespace,
|
||||
|
||||
Reference in New Issue
Block a user