mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-23 18:01:21 +01:00
A bunch of fixes
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { useEffect, useMemo } from 'react';
|
||||
import { getKeyValue } from '../lib/keyValueStore';
|
||||
import { useActiveWorkspace } from './useActiveWorkspace';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { getKeyValue, setKeyValue } from '../lib/keyValueStore';
|
||||
import { activeWorkspaceIdAtom } from './useActiveWorkspace';
|
||||
import { useKeyValue } from './useKeyValue';
|
||||
import { useWorkspaces } from './useWorkspaces';
|
||||
|
||||
@@ -10,22 +11,7 @@ const fallback: string[] = [];
|
||||
|
||||
export function useRecentWorkspaces() {
|
||||
const workspaces = useWorkspaces();
|
||||
const activeWorkspace = useActiveWorkspace();
|
||||
const {value, set} = useKeyValue<string[]>({
|
||||
key: kvKey(),
|
||||
namespace,
|
||||
fallback,
|
||||
});
|
||||
|
||||
// Set history when active request changes
|
||||
useEffect(() => {
|
||||
set((currentHistory: string[]) => {
|
||||
if (activeWorkspace === null) return currentHistory;
|
||||
const withoutCurrent = currentHistory.filter((id) => id !== activeWorkspace.id);
|
||||
return [activeWorkspace.id, ...withoutCurrent];
|
||||
}).catch(console.error);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [activeWorkspace]);
|
||||
const { value } = useKeyValue<string[]>({ key: kvKey(), namespace, fallback });
|
||||
|
||||
const onlyValidIds = useMemo(
|
||||
() => value?.filter((id) => workspaces.some((w) => w.id === id)) ?? [],
|
||||
@@ -35,10 +21,20 @@ export function useRecentWorkspaces() {
|
||||
return onlyValidIds;
|
||||
}
|
||||
|
||||
export async function getRecentWorkspaces() {
|
||||
return getKeyValue<string[]>({
|
||||
namespace,
|
||||
key: kvKey(),
|
||||
fallback: fallback,
|
||||
});
|
||||
export function useSubscribeRecentWorkspaces() {
|
||||
useEffect(() => {
|
||||
return jotaiStore.sub(activeWorkspaceIdAtom, async () => {
|
||||
const activeWorkspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
if (activeWorkspaceId == null) return;
|
||||
|
||||
const key = kvKey();
|
||||
|
||||
const recentIds = await getKeyValue<string[]>({ namespace, key, fallback });
|
||||
if (recentIds[0] === activeWorkspaceId) return; // Short-circuit
|
||||
|
||||
const withoutActiveId = recentIds.filter((id) => id !== activeWorkspaceId);
|
||||
const value = [activeWorkspaceId, ...withoutActiveId];
|
||||
await setKeyValue({ namespace, key, value });
|
||||
});
|
||||
}, []);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user