fix: first workspace not found (#3258)
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
import { DebugLogger } from '@affine/debug';
|
import { DebugLogger } from '@affine/debug';
|
||||||
|
import { initEmptyPage, initPageWithPreloading } from '@affine/env/blocksuite';
|
||||||
|
import { DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX } from '@affine/env/constant';
|
||||||
import { WorkspaceFlavour, WorkspaceVersion } from '@affine/env/workspace';
|
import { WorkspaceFlavour, WorkspaceVersion } from '@affine/env/workspace';
|
||||||
import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
|
import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
|
||||||
import { saveWorkspaceToLocalStorage } from '@affine/workspace/local/crud';
|
import { saveWorkspaceToLocalStorage } from '@affine/workspace/local/crud';
|
||||||
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
||||||
|
import { assertEquals } from '@blocksuite/global/utils';
|
||||||
import { nanoid } from '@blocksuite/store';
|
import { nanoid } from '@blocksuite/store';
|
||||||
import { getWorkspace } from '@toeverything/plugin-infra/__internal__/workspace';
|
import { getWorkspace } from '@toeverything/plugin-infra/__internal__/workspace';
|
||||||
import { useAtomValue, useSetAtom } from 'jotai';
|
import { useAtomValue, useSetAtom } from 'jotai';
|
||||||
@@ -45,6 +48,30 @@ export function useAppHelper() {
|
|||||||
);
|
);
|
||||||
blockSuiteWorkspace.meta.setName(name);
|
blockSuiteWorkspace.meta.setName(name);
|
||||||
const id = await LocalAdapter.CRUD.create(blockSuiteWorkspace);
|
const id = await LocalAdapter.CRUD.create(blockSuiteWorkspace);
|
||||||
|
{
|
||||||
|
// this is hack, because CRUD doesn't return the workspace
|
||||||
|
const blockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||||
|
id,
|
||||||
|
WorkspaceFlavour.LOCAL
|
||||||
|
);
|
||||||
|
const pageId = `${blockSuiteWorkspace.id}-${DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX}`;
|
||||||
|
const page = blockSuiteWorkspace.createPage({
|
||||||
|
id: pageId,
|
||||||
|
});
|
||||||
|
assertEquals(page.id, pageId);
|
||||||
|
if (runtimeConfig.enablePreloading) {
|
||||||
|
await initPageWithPreloading(page).catch(error => {
|
||||||
|
console.error('import error:', error);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
await initEmptyPage(page).catch(error => {
|
||||||
|
console.error('init empty page error', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
blockSuiteWorkspace.setPageMeta(page.id, {
|
||||||
|
jumpOnce: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
await set(workspaces => [
|
await set(workspaces => [
|
||||||
...workspaces,
|
...workspaces,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import {
|
|||||||
ToolContainer,
|
ToolContainer,
|
||||||
WorkspaceFallback,
|
WorkspaceFallback,
|
||||||
} from '@affine/component/workspace';
|
} from '@affine/component/workspace';
|
||||||
import { initEmptyPage, initPageWithPreloading } from '@affine/env/blocksuite';
|
|
||||||
import {
|
import {
|
||||||
DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX,
|
DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX,
|
||||||
isDesktop,
|
isDesktop,
|
||||||
@@ -20,7 +19,7 @@ import {
|
|||||||
rootBlockHubAtom,
|
rootBlockHubAtom,
|
||||||
rootWorkspacesMetadataAtom,
|
rootWorkspacesMetadataAtom,
|
||||||
} from '@affine/workspace/atom';
|
} from '@affine/workspace/atom';
|
||||||
import { assertEquals, assertExists } from '@blocksuite/global/utils';
|
import { assertExists } from '@blocksuite/global/utils';
|
||||||
import { nanoid } from '@blocksuite/store';
|
import { nanoid } from '@blocksuite/store';
|
||||||
import type { DragEndEvent } from '@dnd-kit/core';
|
import type { DragEndEvent } from '@dnd-kit/core';
|
||||||
import {
|
import {
|
||||||
@@ -187,32 +186,6 @@ export const WorkspaceLayoutInner: FC<PropsWithChildren> = ({ children }) => {
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { jumpToPage } = useRouterHelper(router);
|
const { jumpToPage } = useRouterHelper(router);
|
||||||
|
|
||||||
//#region init workspace
|
|
||||||
if (currentWorkspace.blockSuiteWorkspace.isEmpty) {
|
|
||||||
// this is a new workspace, so we should redirect to the new page
|
|
||||||
const pageId = `${currentWorkspace.blockSuiteWorkspace.id}-${DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX}`;
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (!router.query.pageId) {
|
|
||||||
setCurrentPageId(pageId);
|
|
||||||
jumpToPage(currentWorkspace.id, pageId).catch(console.error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//#endregion
|
|
||||||
|
|
||||||
usePassiveWorkspaceEffect(currentWorkspace.blockSuiteWorkspace);
|
usePassiveWorkspaceEffect(currentWorkspace.blockSuiteWorkspace);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -220,12 +193,9 @@ export const WorkspaceLayoutInner: FC<PropsWithChildren> = ({ children }) => {
|
|||||||
`${currentWorkspace.blockSuiteWorkspace.id}-${DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX}`
|
`${currentWorkspace.blockSuiteWorkspace.id}-${DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX}`
|
||||||
);
|
);
|
||||||
if (page && page.meta.jumpOnce) {
|
if (page && page.meta.jumpOnce) {
|
||||||
currentWorkspace.blockSuiteWorkspace.meta.setPageMeta(
|
currentWorkspace.blockSuiteWorkspace.meta.setPageMeta(page.id, {
|
||||||
`${currentWorkspace.blockSuiteWorkspace.id}-${DEFAULT_HELLO_WORLD_PAGE_ID_SUFFIX}`,
|
jumpOnce: false,
|
||||||
{
|
});
|
||||||
jumpOnce: false,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
setCurrentPageId(currentPageId);
|
setCurrentPageId(currentPageId);
|
||||||
jumpToPage(currentWorkspace.id, page.id).catch(err => {
|
jumpToPage(currentWorkspace.id, page.id).catch(err => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
Reference in New Issue
Block a user