fix: wrap React.lazy with Suspense (#1915)

This commit is contained in:
Himself65 2023-04-12 22:33:31 -05:00 committed by GitHub
parent 2bcda973d3
commit 6180a4c3cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 138 additions and 120 deletions

View File

@ -5,7 +5,7 @@ import { useBlockSuiteWorkspacePageTitle } from '@toeverything/hooks/use-blocksu
import { useAtomValue, useSetAtom } from 'jotai';
import Head from 'next/head';
import type React from 'react';
import { lazy, startTransition, useCallback } from 'react';
import { lazy, startTransition, Suspense, useCallback } from 'react';
import { currentEditorAtom, workspacePreferredModeAtom } from '../atoms';
import { usePageMeta } from '../hooks/use-page-meta';
@ -64,32 +64,34 @@ export const PageDetailEditor: React.FC<PageDetailEditorProps> = ({
>
{header}
</WorkspaceHeader>
<Editor
style={{
height: 'calc(100% - 52px)',
}}
key={pageId}
mode={isPublic ? 'page' : currentMode}
page={page}
onInit={useCallback(
(page: Page, editor: Readonly<EditorContainer>) => {
startTransition(() => {
setEditor(editor);
});
onInit(page, editor);
},
[onInit, setEditor]
)}
onLoad={useCallback(
(page: Page, editor: EditorContainer) => {
startTransition(() => {
setEditor(editor);
});
onLoad?.(page, editor);
},
[onLoad, setEditor]
)}
/>
<Suspense>
<Editor
style={{
height: 'calc(100% - 52px)',
}}
key={pageId}
mode={isPublic ? 'page' : currentMode}
page={page}
onInit={useCallback(
(page: Page, editor: Readonly<EditorContainer>) => {
startTransition(() => {
setEditor(editor);
});
onInit(page, editor);
},
[onInit, setEditor]
)}
onLoad={useCallback(
(page: Page, editor: EditorContainer) => {
startTransition(() => {
setEditor(editor);
});
onLoad?.(page, editor);
},
[onLoad, setEditor]
)}
/>
</Suspense>
</>
);
};

View File

@ -2,7 +2,7 @@ import { MuiFade, Tooltip } from '@affine/component';
import { config } from '@affine/env';
import { useTranslation } from '@affine/i18n';
import { CloseIcon, NewIcon } from '@blocksuite/icons';
import { lazy, useState } from 'react';
import { lazy, Suspense, useState } from 'react';
import { ShortcutsModal } from '../shortcuts-modal';
import { ContactIcon, HelpIcon, KeyboardIcon } from './Icons';
@ -113,11 +113,13 @@ export const HelpIsland = ({
</StyledTriggerWrapper>
</MuiFade>
</StyledIsland>
<ContactModal
open={open}
onClose={() => setOpen(false)}
logoSrc="/imgs/affine-text-logo.png"
/>
<Suspense>
<ContactModal
open={open}
onClose={() => setOpen(false)}
logoSrc="/imgs/affine-text-logo.png"
/>
</Suspense>
<ShortcutsModal
open={openShortCut}
onClose={() => setOpenShortCut(false)}

View File

@ -69,12 +69,14 @@ export const PublicQuickSearch: FC = () => {
openQuickSearchModalAtom
);
return (
<QuickSearchModal
blockSuiteWorkspace={publicWorkspace.blockSuiteWorkspace}
open={openQuickSearchModal}
setOpen={setOpenQuickSearchModalAtom}
router={router}
/>
<Suspense>
<QuickSearchModal
blockSuiteWorkspace={publicWorkspace.blockSuiteWorkspace}
open={openQuickSearchModal}
setOpen={setOpenQuickSearchModalAtom}
router={router}
/>
</Suspense>
);
};

View File

@ -28,12 +28,14 @@ export const PublicQuickSearch: React.FC = () => {
openQuickSearchModalAtom
);
return (
<QuickSearchModal
blockSuiteWorkspace={publicWorkspace.blockSuiteWorkspace}
open={openQuickSearchModal}
setOpen={setOpenQuickSearchModalAtom}
router={router}
/>
<Suspense>
<QuickSearchModal
blockSuiteWorkspace={publicWorkspace.blockSuiteWorkspace}
open={openQuickSearchModal}
setOpen={setOpenQuickSearchModalAtom}
router={router}
/>
</Suspense>
);
};

View File

@ -1,6 +1,6 @@
import { NoSsr } from '@mui/material';
import { useRouter } from 'next/router';
import { lazy } from 'react';
import { lazy, Suspense } from 'react';
import { StyledPage, StyledWrapper } from '../../layouts/styles';
import type { NextPageWithLayout } from '../../shared';
@ -26,7 +26,9 @@ const InitPagePage: NextPageWithLayout = () => {
return (
<StyledPage>
<StyledWrapper>
<Editor onInit={initPage} testType={testType} />
<Suspense>
<Editor onInit={initPage} testType={testType} />
</Suspense>
<div id="toolWrapper" />
</StyledWrapper>
</StyledPage>

View File

@ -11,7 +11,7 @@ import {
} from '@affine/workspace/affine/login';
import { useAtom } from 'jotai';
import type { NextPage } from 'next';
import { lazy, useMemo } from 'react';
import { lazy, Suspense, useMemo } from 'react';
import { toast } from '../../utils';
@ -91,10 +91,12 @@ const LoginDevPage: NextPage = () => {
>
Check Permission
</Button>
<Viewer
theme={useTheme().resolvedTheme === 'light' ? 'light' : 'dark'}
value={user}
/>
<Suspense>
<Viewer
theme={useTheme().resolvedTheme === 'light' ? 'light' : 'dark'}
value={user}
/>
</Suspense>
</StyledWrapper>
</StyledPage>
);

View File

@ -71,10 +71,12 @@ const ListPageInner: React.FC<{
<SearchIcon />
</IconButton>
</NavContainer>
<BlockSuitePublicPageList
onOpenPage={handleClickPage}
blockSuiteWorkspace={blockSuiteWorkspace}
/>
<Suspense>
<BlockSuitePublicPageList
onOpenPage={handleClickPage}
blockSuiteWorkspace={blockSuiteWorkspace}
/>
</Suspense>
</>
);
};

View File

@ -3,7 +3,7 @@ import { arrayMove } from '@dnd-kit/sortable';
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
import { useRouter } from 'next/router';
import type React from 'react';
import { lazy, useCallback, useTransition } from 'react';
import { lazy, Suspense, useCallback, useTransition } from 'react';
import {
currentWorkspaceIdAtom,
@ -49,71 +49,75 @@ export function Modals() {
return (
<>
<WorkspaceListModal
disabled={transitioning}
user={user}
workspaces={workspaces}
currentWorkspaceId={currentWorkspaceId}
open={openWorkspacesModal || workspaces.length === 0}
onClose={useCallback(() => {
setOpenWorkspacesModal(false);
}, [setOpenWorkspacesModal])}
onMoveWorkspace={useCallback(
(activeId, overId) => {
const oldIndex = workspaces.findIndex(w => w.id === activeId);
const newIndex = workspaces.findIndex(w => w.id === overId);
transition(() =>
setWorkspaces(workspaces =>
arrayMove(workspaces, oldIndex, newIndex)
)
);
},
[setWorkspaces, workspaces]
)}
onClickWorkspace={useCallback(
workspace => {
<Suspense>
<WorkspaceListModal
disabled={transitioning}
user={user}
workspaces={workspaces}
currentWorkspaceId={currentWorkspaceId}
open={openWorkspacesModal || workspaces.length === 0}
onClose={useCallback(() => {
setOpenWorkspacesModal(false);
setCurrentWorkspace(workspace.id);
jumpToSubPath(workspace.id, WorkspaceSubPath.ALL);
},
[jumpToSubPath, setCurrentWorkspace, setOpenWorkspacesModal]
)}
onClickWorkspaceSetting={useCallback(
workspace => {
setOpenWorkspacesModal(false);
setCurrentWorkspace(workspace.id);
jumpToSubPath(workspace.id, WorkspaceSubPath.SETTING);
},
[jumpToSubPath, setCurrentWorkspace, setOpenWorkspacesModal]
)}
onClickLogin={useAffineLogIn()}
onClickLogout={useAffineLogOut()}
onCreateWorkspace={useCallback(() => {
setOpenCreateWorkspaceModal(true);
}, [setOpenCreateWorkspaceModal])}
/>
<CreateWorkspaceModal
open={openCreateWorkspaceModal}
onClose={useCallback(() => {
setOpenCreateWorkspaceModal(false);
}, [setOpenCreateWorkspaceModal])}
onCreate={useCallback(
async name => {
const id = await createLocalWorkspace(name);
}, [setOpenWorkspacesModal])}
onMoveWorkspace={useCallback(
(activeId, overId) => {
const oldIndex = workspaces.findIndex(w => w.id === activeId);
const newIndex = workspaces.findIndex(w => w.id === overId);
transition(() =>
setWorkspaces(workspaces =>
arrayMove(workspaces, oldIndex, newIndex)
)
);
},
[setWorkspaces, workspaces]
)}
onClickWorkspace={useCallback(
workspace => {
setOpenWorkspacesModal(false);
setCurrentWorkspace(workspace.id);
jumpToSubPath(workspace.id, WorkspaceSubPath.ALL);
},
[jumpToSubPath, setCurrentWorkspace, setOpenWorkspacesModal]
)}
onClickWorkspaceSetting={useCallback(
workspace => {
setOpenWorkspacesModal(false);
setCurrentWorkspace(workspace.id);
jumpToSubPath(workspace.id, WorkspaceSubPath.SETTING);
},
[jumpToSubPath, setCurrentWorkspace, setOpenWorkspacesModal]
)}
onClickLogin={useAffineLogIn()}
onClickLogout={useAffineLogOut()}
onCreateWorkspace={useCallback(() => {
setOpenCreateWorkspaceModal(true);
}, [setOpenCreateWorkspaceModal])}
/>
</Suspense>
<Suspense>
<CreateWorkspaceModal
open={openCreateWorkspaceModal}
onClose={useCallback(() => {
setOpenCreateWorkspaceModal(false);
setOpenWorkspacesModal(false);
setCurrentWorkspace(id);
return jumpToSubPath(id, WorkspaceSubPath.ALL);
},
[
createLocalWorkspace,
jumpToSubPath,
setCurrentWorkspace,
setOpenCreateWorkspaceModal,
setOpenWorkspacesModal,
]
)}
/>
}, [setOpenCreateWorkspaceModal])}
onCreate={useCallback(
async name => {
const id = await createLocalWorkspace(name);
setOpenCreateWorkspaceModal(false);
setOpenWorkspacesModal(false);
setCurrentWorkspace(id);
return jumpToSubPath(id, WorkspaceSubPath.ALL);
},
[
createLocalWorkspace,
jumpToSubPath,
setCurrentWorkspace,
setOpenCreateWorkspaceModal,
setOpenWorkspacesModal,
]
)}
/>
</Suspense>
</>
);
}