mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-28 03:37:02 +02:00
Refactor desktop app into separate client and proxy apps
This commit is contained in:
51
apps/yaak-client/hooks/useRecentEnvironments.ts
Normal file
51
apps/yaak-client/hooks/useRecentEnvironments.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { useEffect, useMemo } from 'react';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { getKeyValue, setKeyValue } from '../lib/keyValueStore';
|
||||
import { activeEnvironmentAtom } from './useActiveEnvironment';
|
||||
import { useEnvironmentsBreakdown } from './useEnvironmentsBreakdown';
|
||||
import { useKeyValue } from './useKeyValue';
|
||||
|
||||
const kvKey = (workspaceId: string) => `recent_environments::${workspaceId}`;
|
||||
const namespace = 'global';
|
||||
const fallback: string[] = [];
|
||||
|
||||
export function useRecentEnvironments() {
|
||||
const { subEnvironments, allEnvironments } = useEnvironmentsBreakdown();
|
||||
const kv = useKeyValue<string[]>({
|
||||
key: kvKey(allEnvironments[0]?.workspaceId ?? 'n/a'),
|
||||
namespace,
|
||||
fallback,
|
||||
});
|
||||
|
||||
const onlyValidIds = useMemo(
|
||||
() => kv.value?.filter((id) => subEnvironments.some((e) => e.id === id)) ?? [],
|
||||
[kv.value, subEnvironments],
|
||||
);
|
||||
|
||||
return onlyValidIds;
|
||||
}
|
||||
|
||||
export function useSubscribeRecentEnvironments() {
|
||||
useEffect(() => {
|
||||
return jotaiStore.sub(activeEnvironmentAtom, async () => {
|
||||
const activeEnvironment = jotaiStore.get(activeEnvironmentAtom);
|
||||
if (activeEnvironment == null) return;
|
||||
|
||||
const key = kvKey(activeEnvironment.workspaceId);
|
||||
const recentIds = getKeyValue<string[]>({ namespace, key, fallback });
|
||||
if (recentIds[0] === activeEnvironment.id) return; // Short-circuit
|
||||
|
||||
const withoutActiveId = recentIds.filter((id) => id !== activeEnvironment.id);
|
||||
const value = [activeEnvironment.id, ...withoutActiveId];
|
||||
await setKeyValue({ namespace, key, value });
|
||||
});
|
||||
}, []);
|
||||
}
|
||||
|
||||
export async function getRecentEnvironments(workspaceId: string) {
|
||||
return getKeyValue<string[]>({
|
||||
namespace,
|
||||
key: kvKey(workspaceId),
|
||||
fallback,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user