refactor(core): remove jump once (#6733)

Previously, we marked `jumpOnce: true` on `doc.meta` to open a specific doc after creating a new workspace. This pr removes `jumpOnce` and directly jumps to the specific doc URL.

This PR also fixes an error when opening the all-page page, because the all-page page scans the jumpOnce attribute of all docs, and the code in it will fail on damaged data.
This commit is contained in:
EYHN 2024-04-29 08:51:31 +00:00
parent 5e8fe28326
commit 6202ba5ada
No known key found for this signature in database
GPG Key ID: 46C9E26A75AB276C
7 changed files with 50 additions and 78 deletions

View File

@ -24,31 +24,18 @@ export async function buildShowcaseWorkspace(
const docsService = workspace.scope.get(DocsService);
// todo: find better way to do the following
// perhaps put them into middleware?
{
// the "Write, Draw, Plan all at Once." page should be set to edgeless mode
const edgelessPage1 = docsService.list.docs$.value.find(
p => p.title$.value === 'Write, Draw, Plan all at Once.'
);
// should jump to "Write, Draw, Plan all at Once." in edgeless by default
const defaultDoc = docsService.list.docs$.value.find(p =>
p.title$.value.startsWith('Write, Draw, Plan all at Once.')
);
if (edgelessPage1) {
edgelessPage1.setMode('edgeless');
}
// should jump to "Write, Draw, Plan all at Once." by default
const defaultPage = docsService.list.docs$.value.find(p =>
p.title$.value.startsWith('Write, Draw, Plan all at Once.')
);
if (defaultPage) {
defaultPage.setMeta({
jumpOnce: true,
});
}
if (defaultDoc) {
defaultDoc.setMode('edgeless');
}
dispose();
return meta;
return { meta, defaultDocId: defaultDoc?.id };
}
const logger = new DebugLogger('createFirstAppData');
@ -59,26 +46,25 @@ export async function createFirstAppData(workspacesService: WorkspacesService) {
}
localStorage.setItem('is-first-open', 'false');
if (runtimeConfig.enablePreloading) {
const workspaceMetadata = await buildShowcaseWorkspace(
const { meta, defaultDocId } = await buildShowcaseWorkspace(
workspacesService,
WorkspaceFlavour.LOCAL,
DEFAULT_WORKSPACE_NAME
);
logger.info('create first workspace', workspaceMetadata);
return workspaceMetadata;
logger.info('create first workspace', defaultDocId);
return { meta, defaultPageId: defaultDocId };
} else {
let defaultPageId: string | undefined = undefined;
const workspaceMetadata = await workspacesService.create(
WorkspaceFlavour.LOCAL,
async workspace => {
workspace.meta.setName(DEFAULT_WORKSPACE_NAME);
const page = workspace.createDoc();
workspace.setDocMeta(page.id, {
jumpOnce: true,
});
defaultPageId = page.id;
initEmptyPage(page);
}
);
logger.info('create first workspace', workspaceMetadata);
return workspaceMetadata;
return { meta: workspaceMetadata, defaultPageId };
}
}

View File

@ -36,7 +36,7 @@ const logger = new DebugLogger('CreateWorkspaceModal');
interface ModalProps {
mode: CreateWorkspaceMode; // false means not open
onClose: () => void;
onCreate: (id: string) => void;
onCreate: (id: string, defaultDocId?: string) => void;
}
interface NameWorkspaceContentProps extends ConfirmModalProps {
@ -236,25 +236,24 @@ export const CreateWorkspaceModal = ({
// this will be the last step for web for now
// fix me later
if (runtimeConfig.enablePreloading) {
const { id } = await buildShowcaseWorkspace(
const { meta, defaultDocId } = await buildShowcaseWorkspace(
workspacesService,
workspaceFlavour,
name
);
onCreate(id);
onCreate(meta.id, defaultDocId);
} else {
let defaultDocId: string | undefined = undefined;
const { id } = await workspacesService.create(
workspaceFlavour,
async workspace => {
workspace.meta.setName(name);
const page = workspace.createDoc();
workspace.setDocMeta(page.id, {
jumpOnce: true,
});
defaultDocId = page.id;
initEmptyPage(page);
}
);
onCreate(id);
onCreate(id, defaultDocId);
}
setLoading(false);

View File

@ -48,7 +48,7 @@ export const Component = () => {
const list = useLiveData(workspacesService.list.workspaces$);
const listIsLoading = useLiveData(workspacesService.list.isLoading$);
const { openPage } = useNavigateHelper();
const { openPage, jumpToPage } = useNavigateHelper();
const [searchParams] = useSearchParams();
const createOnceRef = useRef(false);
@ -61,9 +61,15 @@ export const Component = () => {
WorkspaceFlavour.AFFINE_CLOUD,
'AFFiNE Cloud'
)
.then(workspace => openPage(workspace.id, WorkspaceSubPath.ALL))
.then(({ meta, defaultDocId }) => {
if (defaultDocId) {
jumpToPage(meta.id, defaultDocId);
} else {
openPage(meta.id, WorkspaceSubPath.ALL);
}
})
.catch(err => console.error('Failed to create cloud workspace', err));
}, [openPage, workspacesService]);
}, [jumpToPage, openPage, workspacesService]);
useLayoutEffect(() => {
if (!navigating) {
@ -114,9 +120,16 @@ export const Component = () => {
useEffect(() => {
setCreating(true);
createFirstAppData(workspacesService)
.then(workspaceMeta => {
if (workspaceMeta) {
openPage(workspaceMeta.id, WorkspaceSubPath.ALL);
.then(createdWorkspace => {
if (createdWorkspace) {
if (createdWorkspace.defaultPageId) {
jumpToPage(
createdWorkspace.meta.id,
createdWorkspace.defaultPageId
);
} else {
openPage(createdWorkspace.meta.id, WorkspaceSubPath.ALL);
}
}
})
.catch(err => {
@ -125,7 +138,7 @@ export const Component = () => {
.finally(() => {
setCreating(false);
});
}, [openPage, workspacesService]);
}, [jumpToPage, openPage, workspacesService]);
if (navigating || creating) {
return <WorkspaceFallback></WorkspaceFallback>;

View File

@ -4,11 +4,10 @@ import {
VirtualizedPageList,
} from '@affine/core/components/page-list';
import { useBlockSuiteDocMeta } from '@affine/core/hooks/use-block-suite-page-meta';
import { useNavigateHelper } from '@affine/core/hooks/use-navigate-helper';
import { performanceRenderLogger } from '@affine/core/shared';
import type { Filter } from '@affine/env/filter';
import { useService, WorkspaceService } from '@toeverything/infra';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { ViewBodyIsland, ViewHeaderIsland } from '../../../modules/workbench';
import { EmptyPageList } from '../page-list-empty';
@ -59,25 +58,5 @@ export const AllPage = () => {
export const Component = () => {
performanceRenderLogger.info('AllPage');
const currentWorkspace = useService(WorkspaceService).workspace;
const navigateHelper = useNavigateHelper();
useEffect(() => {
function checkJumpOnce() {
for (const [pageId] of currentWorkspace.docCollection.docs) {
const page = currentWorkspace.docCollection.getDoc(pageId);
if (page && page.meta?.jumpOnce) {
currentWorkspace.docCollection.meta.setDocMeta(page.id, {
jumpOnce: false,
});
navigateHelper.jumpToPage(currentWorkspace.id, pageId);
}
}
}
checkJumpOnce();
return currentWorkspace.docCollection.slots.docUpdated.on(checkJumpOnce)
.dispose;
}, [currentWorkspace.docCollection, currentWorkspace.id, navigateHelper]);
return <AllPage />;
};

View File

@ -325,14 +325,6 @@ export const DetailPage = ({ pageId }: { pageId: string }): ReactElement => {
};
}, [currentWorkspace, pageId]);
const jumpOnce = useLiveData(doc?.meta$.map(meta => meta.jumpOnce));
useEffect(() => {
if (jumpOnce) {
doc?.record.setMeta({ jumpOnce: false });
}
}, [doc?.record, jumpOnce]);
const isInTrash = useLiveData(doc?.meta$.map(meta => meta.trash));
useEffect(() => {

View File

@ -259,7 +259,7 @@ export const AllWorkspaceModals = (): ReactElement => {
openCreateWorkspaceModalAtom
);
const { jumpToSubPath } = useNavigateHelper();
const { jumpToSubPath, jumpToPage } = useNavigateHelper();
return (
<>
@ -270,15 +270,19 @@ export const AllWorkspaceModals = (): ReactElement => {
setOpenCreateWorkspaceModal(false);
}, [setOpenCreateWorkspaceModal])}
onCreate={useCallback(
id => {
(id, defaultDocId) => {
setOpenCreateWorkspaceModal(false);
// if jumping immediately, the page may stuck in loading state
// not sure why yet .. here is a workaround
setTimeout(() => {
jumpToSubPath(id, WorkspaceSubPath.ALL);
if (!defaultDocId) {
jumpToSubPath(id, WorkspaceSubPath.ALL);
} else {
jumpToPage(id, defaultDocId);
}
});
},
[jumpToSubPath, setOpenCreateWorkspaceModal]
[jumpToPage, jumpToSubPath, setOpenCreateWorkspaceModal]
)}
/>
</Suspense>

View File

@ -30,7 +30,6 @@ declare module '@blocksuite/store' {
trashDate?: number;
updatedDate?: number;
mode?: 'page' | 'edgeless';
jumpOnce?: boolean;
// todo: support `number` in the future
isPublic?: boolean;
}