mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 20:38:52 +00:00
fix: delay setAom on rootWorkspacesMetadataAtom (#2271)
This commit is contained in:
@@ -40,17 +40,24 @@ rootWorkspacesMetadataAtom.onMount = setAtom => {
|
||||
}).filter((ids): ids is RootWorkspaceMetadata => !!ids);
|
||||
}
|
||||
|
||||
setAtom(metadata => {
|
||||
if (metadata.length === 0) {
|
||||
const newMetadata = createFirst();
|
||||
logger.info('create first workspace', newMetadata);
|
||||
return newMetadata;
|
||||
}
|
||||
return metadata;
|
||||
});
|
||||
const abortController = new AbortController();
|
||||
|
||||
// next tick to make sure the hydration is correct
|
||||
const id = setTimeout(() => {
|
||||
setAtom(metadata => {
|
||||
if (abortController.signal.aborted) return metadata;
|
||||
if (metadata.length === 0) {
|
||||
const newMetadata = createFirst();
|
||||
logger.info('create first workspace', newMetadata);
|
||||
return newMetadata;
|
||||
}
|
||||
return metadata;
|
||||
});
|
||||
}, 0);
|
||||
|
||||
if (environment.isDesktop) {
|
||||
window.apis?.workspace.list().then(workspaceIDs => {
|
||||
if (abortController.signal.aborted) return;
|
||||
const newMetadata = workspaceIDs.map(w => ({
|
||||
id: w[0],
|
||||
flavour: WorkspaceFlavour.LOCAL,
|
||||
@@ -63,6 +70,11 @@ rootWorkspacesMetadataAtom.onMount = setAtom => {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return () => {
|
||||
clearTimeout(id);
|
||||
abortController.abort();
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -202,6 +202,8 @@ describe('useWorkspaces', () => {
|
||||
const { result } = renderHook(() => useAppHelper(), {
|
||||
wrapper: ProviderWrapper,
|
||||
});
|
||||
// next tick
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
{
|
||||
const workspaces = await store.get(workspacesAtom);
|
||||
expect(workspaces.length).toEqual(1);
|
||||
|
||||
Reference in New Issue
Block a user