mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-02 11:29:02 -05:00
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { useEffect, useMemo } from 'react';
|
|
import { getKeyValue } from '../lib/keyValueStore';
|
|
import { useActiveWorkspaceId } from './useActiveWorkspaceId';
|
|
import { useKeyValue } from './useKeyValue';
|
|
import { useWorkspaces } from './useWorkspaces';
|
|
|
|
const kvKey = () => 'recent_workspaces';
|
|
const namespace = 'global';
|
|
const fallback: string[] = [];
|
|
|
|
export function useRecentWorkspaces() {
|
|
const workspaces = useWorkspaces();
|
|
const activeWorkspaceId = useActiveWorkspaceId();
|
|
const kv = useKeyValue<string[]>({
|
|
key: kvKey(),
|
|
namespace,
|
|
fallback,
|
|
});
|
|
|
|
// Set history when active request changes
|
|
useEffect(() => {
|
|
kv.set((currentHistory: string[]) => {
|
|
if (activeWorkspaceId === null) return currentHistory;
|
|
const withoutCurrent = currentHistory.filter((id) => id !== activeWorkspaceId);
|
|
return [activeWorkspaceId, ...withoutCurrent];
|
|
}).catch(console.error);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [activeWorkspaceId]);
|
|
|
|
const onlyValidIds = useMemo(
|
|
() => kv.value?.filter((id) => workspaces.some((w) => w.id === id)) ?? [],
|
|
[kv.value, workspaces],
|
|
);
|
|
|
|
return onlyValidIds;
|
|
}
|
|
|
|
export async function getRecentWorkspaces() {
|
|
return getKeyValue<string[]>({
|
|
namespace,
|
|
key: kvKey(),
|
|
fallback: fallback,
|
|
});
|
|
}
|