mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-25 01:58:39 +02:00
Fix recent workspaces when open in new window
This commit is contained in:
@@ -26,18 +26,23 @@ export function useRecentWorkspaces() {
|
|||||||
|
|
||||||
export function useSubscribeRecentWorkspaces() {
|
export function useSubscribeRecentWorkspaces() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return jotaiStore.sub(activeWorkspaceIdAtom, async () => {
|
const unsub = jotaiStore.sub(activeWorkspaceIdAtom, updateRecentWorkspaces);
|
||||||
const activeWorkspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
updateRecentWorkspaces().catch(console.error); // Update when opened in a new window
|
||||||
if (activeWorkspaceId == null) return;
|
return unsub;
|
||||||
|
|
||||||
const key = kvKey();
|
|
||||||
|
|
||||||
const recentIds = 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 });
|
|
||||||
});
|
|
||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function updateRecentWorkspaces() {
|
||||||
|
const activeWorkspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||||
|
if (activeWorkspaceId == null) return;
|
||||||
|
|
||||||
|
const key = kvKey();
|
||||||
|
|
||||||
|
const recentIds = getKeyValue<string[]>({ 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 });
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,9 +30,8 @@ export function getKeyValueRaw({
|
|||||||
key: string | string[];
|
key: string | string[];
|
||||||
}) {
|
}) {
|
||||||
const key = buildKeyValueKey(keyOrKeys);
|
const key = buildKeyValueKey(keyOrKeys);
|
||||||
const kv = jotaiStore
|
const keyValues = jotaiStore.get(keyValuesAtom);
|
||||||
.get(keyValuesAtom)
|
const kv = keyValues.find((kv) => kv.namespace === namespace && kv?.key === key);
|
||||||
.find((kv) => kv.namespace === namespace && kv?.key === key);
|
|
||||||
return kv ?? null;
|
return kv ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user