refactor: rootWorkspacesMetadataAtom loading logic (#2882)

This commit is contained in:
Alex Yang
2023-06-29 16:48:12 +08:00
parent df7f782e05
commit f7b07f4216
22 changed files with 436 additions and 353 deletions

View File

@@ -8,13 +8,9 @@ import {
ToolContainer,
WorkspaceFallback,
} from '@affine/component/workspace';
import { DebugLogger } from '@affine/debug';
import { initEmptyPage, initPageWithPreloading } from '@affine/env/blocksuite';
import { DEFAULT_HELLO_WORLD_PAGE_ID, isDesktop } from '@affine/env/constant';
import { WorkspaceFlavour } from '@affine/env/workspace';
import { setUpLanguage, useI18N } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { createAffineGlobalChannel } from '@affine/workspace/affine/sync';
import {
rootCurrentPageIdAtom,
rootCurrentWorkspaceIdAtom,
@@ -33,7 +29,6 @@ import {
useSensors,
} from '@dnd-kit/core';
import { useBlockSuiteWorkspaceHelper } from '@toeverything/hooks/use-block-suite-workspace-helper';
import { rootStore } from '@toeverything/plugin-infra/manager';
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import Head from 'next/head';
import { useRouter } from 'next/router';
@@ -144,12 +139,6 @@ export const Setting: FC = () => {
);
};
const logger = new DebugLogger('workspace-layout');
const affineGlobalChannel = createAffineGlobalChannel(
WorkspaceAdapters[WorkspaceFlavour.AFFINE].CRUD
);
export const AllWorkspaceContext = ({
children,
}: PropsWithChildren): ReactElement => {
@@ -226,14 +215,6 @@ export const CurrentWorkspaceContext = ({
export const WorkspaceLayout: FC<PropsWithChildren> =
function WorkspacesSuspense({ children }) {
const i18n = useI18N();
useEffect(() => {
document.documentElement.lang = i18n.language;
// todo(himself65): this is a hack, we should use a better way to set the language
setUpLanguage(i18n)?.catch(error => {
console.error(error);
});
}, [i18n]);
useTrackRouterHistoryEffect();
const currentWorkspaceId = useAtomValue(rootCurrentWorkspaceIdAtom);
const jotaiWorkspaces = useAtomValue(rootWorkspacesMetadataAtom);
@@ -241,67 +222,6 @@ export const WorkspaceLayout: FC<PropsWithChildren> =
() => jotaiWorkspaces.find(x => x.id === currentWorkspaceId),
[currentWorkspaceId, jotaiWorkspaces]
);
const set = useSetAtom(rootWorkspacesMetadataAtom);
useEffect(() => {
logger.info('mount');
const controller = new AbortController();
const lists = Object.values(WorkspaceAdapters)
.sort((a, b) => a.loadPriority - b.loadPriority)
.map(({ CRUD }) => CRUD.list);
async function fetch() {
const jotaiWorkspaces = rootStore.get(rootWorkspacesMetadataAtom);
const items = [];
for (const list of lists) {
try {
const item = await list();
if (jotaiWorkspaces.length) {
item.sort((a, b) => {
return (
jotaiWorkspaces.findIndex(x => x.id === a.id) -
jotaiWorkspaces.findIndex(x => x.id === b.id)
);
});
}
items.push(
...item.map(x => ({
id: x.id,
flavour: x.flavour,
version: undefined,
}))
);
} catch (e) {
logger.error('list data error:', e);
}
}
if (controller.signal.aborted) {
return;
}
set([...items]);
logger.info('mount first data:', items);
}
fetch().catch(e => {
logger.error('fetch error:', e);
});
return () => {
controller.abort();
logger.info('unmount');
};
}, [set]);
useEffect(() => {
const flavour = jotaiWorkspaces.find(
x => x.id === currentWorkspaceId
)?.flavour;
if (flavour === WorkspaceFlavour.AFFINE) {
affineGlobalChannel.connect();
return () => {
affineGlobalChannel.disconnect();
};
}
return;
}, [currentWorkspaceId, jotaiWorkspaces]);
const Provider =
(meta && WorkspaceAdapters[meta.flavour].UI.Provider) ?? DefaultProvider;
@@ -335,31 +255,35 @@ export const WorkspaceLayoutInner: FC<PropsWithChildren> = ({ children }) => {
const router = useRouter();
const { jumpToPage } = useRouterHelper(router);
// fixme(himself65):
// we should move the page into jotai atom since it's an async value
//#region init workspace
if (currentWorkspace.blockSuiteWorkspace.isEmpty) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (currentWorkspace.blockSuiteWorkspace.meta._proxy.isEmpty !== true) {
// this is a new workspace, so we should redirect to the new page
const pageId = DEFAULT_HELLO_WORLD_PAGE_ID;
const page = currentWorkspace.blockSuiteWorkspace.createPage({
id: pageId,
});
assertEquals(page.id, pageId);
if (runtimeConfig.enablePreloading) {
initPageWithPreloading(page).catch(error => {
console.error('import error:', error);
});
} else {
initEmptyPage(page).catch(error => {
console.error('init empty page error', error);
});
}
if (!router.query.pageId) {
setCurrentPageId(pageId);
jumpToPage(currentWorkspace.id, pageId).catch(err => {
console.error(err);
if (currentWorkspace.blockSuiteWorkspace.getPage(pageId) === null) {
const page = currentWorkspace.blockSuiteWorkspace.createPage({
id: pageId,
});
assertEquals(page.id, pageId);
if (runtimeConfig.enablePreloading) {
initPageWithPreloading(page).catch(error => {
console.error('import error:', error);
});
} else {
initEmptyPage(page).catch(error => {
console.error('init empty page error', error);
});
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
currentWorkspace.blockSuiteWorkspace.meta._proxy.isEmpty = false;
if (!router.query.pageId) {
setCurrentPageId(pageId);
jumpToPage(currentWorkspace.id, pageId).catch(err => {
console.error(err);
});
}
}
}
//#endregion