diff --git a/packages/common/infra/src/livedata/react.ts b/packages/common/infra/src/livedata/react.ts index 1b7ccd93b4..b6aa5a50a8 100644 --- a/packages/common/infra/src/livedata/react.ts +++ b/packages/common/infra/src/livedata/react.ts @@ -7,10 +7,14 @@ function noopSubscribe() { return () => {}; } -function noopGetSnapshot() { +function nullGetSnapshot() { return null; } +function undefinedGetSnapshot() { + return undefined; +} + /** * subscribe LiveData and return the value. */ @@ -25,7 +29,11 @@ export function useLiveData | null | undefined>( : never { return useSyncExternalStore( liveData ? liveData.reactSubscribe : noopSubscribe, - liveData ? liveData.reactGetSnapshot : noopGetSnapshot + liveData + ? liveData.reactGetSnapshot + : liveData === undefined + ? undefinedGetSnapshot + : nullGetSnapshot ); } diff --git a/packages/frontend/core/src/layouts/workspace-layout.tsx b/packages/frontend/core/src/layouts/workspace-layout.tsx index 7716cf4e45..595c1aa2af 100644 --- a/packages/frontend/core/src/layouts/workspace-layout.tsx +++ b/packages/frontend/core/src/layouts/workspace-layout.tsx @@ -1,4 +1,3 @@ -import { useBlockSuiteDocMeta } from '@affine/core/hooks/use-block-suite-page-meta'; import { useWorkspaceStatus } from '@affine/core/hooks/use-workspace-status'; import { assertExists } from '@blocksuite/global/utils'; import { @@ -10,12 +9,12 @@ import { useSensor, useSensors, } from '@dnd-kit/core'; -import { Workspace } from '@toeverything/infra'; +import { PageRecordList, useLiveData, Workspace } from '@toeverything/infra'; import { useService } from '@toeverything/infra/di'; import { useAtom, useAtomValue, useSetAtom } from 'jotai'; import type { PropsWithChildren, ReactNode } from 'react'; import { lazy, Suspense, useCallback, useEffect, useState } from 'react'; -import { useParams } from 'react-router-dom'; +import { matchPath, useParams } from 'react-router-dom'; import { Map as YMap } from 'yjs'; import { openQuickSearchModalAtom, openSettingModalAtom } from '../atoms'; @@ -37,6 +36,7 @@ import { useAppSettingHelper } from '../hooks/affine/use-app-setting-helper'; import { useSidebarDrag } from '../hooks/affine/use-sidebar-drag'; import { useNavigateHelper } from '../hooks/use-navigate-helper'; import { useRegisterWorkspaceCommands } from '../hooks/use-register-workspace-commands'; +import { Workbench } from '../modules/workbench'; import { AllWorkspaceModals, CurrentWorkspaceModals, @@ -55,16 +55,15 @@ export const QuickSearch = () => { openQuickSearchModalAtom ); - const currentWorkspace = useService(Workspace); - const { pageId } = useParams(); - const blockSuiteWorkspace = currentWorkspace.blockSuiteWorkspace; - const pageMeta = useBlockSuiteDocMeta( - currentWorkspace.blockSuiteWorkspace - ).find(meta => meta.id === pageId); - - if (!blockSuiteWorkspace) { - return null; - } + const workbench = useService(Workbench); + const currentPath = useLiveData(workbench.location.map(l => l.pathname)); + const pageRecordList = useService(PageRecordList); + const currentPathId = matchPath('/:pageId', currentPath)?.params.pageId; + // TODO: getting pageid from route is fragile, get current page from context + const currentPage = useLiveData( + currentPathId ? pageRecordList.record(currentPathId) : null + ); + const pageMeta = useLiveData(currentPage?.meta); return (