diff --git a/src-web/hooks/useRecentWorkspaces.ts b/src-web/hooks/useRecentWorkspaces.ts index ea56bba2..bee339ac 100644 --- a/src-web/hooks/useRecentWorkspaces.ts +++ b/src-web/hooks/useRecentWorkspaces.ts @@ -26,18 +26,23 @@ export function useRecentWorkspaces() { export function useSubscribeRecentWorkspaces() { useEffect(() => { - return jotaiStore.sub(activeWorkspaceIdAtom, async () => { - const activeWorkspaceId = jotaiStore.get(activeWorkspaceIdAtom); - if (activeWorkspaceId == null) return; - - const key = kvKey(); - - const recentIds = getKeyValue({ 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 }); - }); + const unsub = jotaiStore.sub(activeWorkspaceIdAtom, updateRecentWorkspaces); + updateRecentWorkspaces().catch(console.error); // Update when opened in a new window + return unsub; }, []); } + +async function updateRecentWorkspaces() { + const activeWorkspaceId = jotaiStore.get(activeWorkspaceIdAtom); + if (activeWorkspaceId == null) return; + + const key = kvKey(); + + const recentIds = getKeyValue({ namespace, key, fallback }); + if (recentIds[0] === activeWorkspaceId) return; // Short-circuit + + const withoutActiveId = recentIds.filter((id) => id !== activeWorkspaceId); + const value = [activeWorkspaceId, ...withoutActiveId]; + console.log('Recent workspaces update', activeWorkspaceId); + await setKeyValue({ namespace, key, value }); +} diff --git a/src-web/lib/keyValueStore.ts b/src-web/lib/keyValueStore.ts index e58eb38b..029d7c3a 100644 --- a/src-web/lib/keyValueStore.ts +++ b/src-web/lib/keyValueStore.ts @@ -30,9 +30,8 @@ export function getKeyValueRaw({ key: string | string[]; }) { const key = buildKeyValueKey(keyOrKeys); - const kv = jotaiStore - .get(keyValuesAtom) - .find((kv) => kv.namespace === namespace && kv?.key === key); + const keyValues = jotaiStore.get(keyValuesAtom); + const kv = keyValues.find((kv) => kv.namespace === namespace && kv?.key === key); return kv ?? null; }