fix: reference page crash for deleted items (#3835)

This commit is contained in:
Peng Xiao 2023-08-19 02:52:09 +08:00 committed by GitHub
parent ba676eb937
commit bd826bb7f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -29,7 +29,11 @@ export const ReferencePage = ({
const icon = setting?.mode === 'edgeless' ? <EdgelessIcon /> : <PageIcon />;
const references = useBlockSuitePageReferences(workspace, pageId);
const referencesToShow = useMemo(() => {
return [...new Set(references.filter(ref => !metaMapping[ref]?.trash))];
return [
...new Set(
references.filter(ref => metaMapping[ref] && !metaMapping[ref]?.trash)
),
];
}, [references, metaMapping]);
const [collapsed, setCollapsed] = useState(true);
const collapsible = referencesToShow.length > 0;

View File

@ -1,4 +1,3 @@
import { assertExists } from '@blocksuite/global/utils';
import type { Page, Workspace } from '@blocksuite/store';
import { type Atom, atom, useAtomValue } from 'jotai';
@ -14,7 +13,11 @@ function getPageReferences(page: Page): string[] {
.filter(Boolean);
}
const getPageReferencesAtom = (page: Page) => {
const getPageReferencesAtom = (page: Page | null) => {
if (!page) {
return atom([]);
}
if (!weakMap.has(page)) {
const baseAtom = atom<string[]>(getPageReferences(page));
baseAtom.onMount = set => {
@ -35,6 +38,5 @@ export function useBlockSuitePageReferences(
pageId: string
): string[] {
const page = useBlockSuiteWorkspacePage(blockSuiteWorkspace, pageId);
assertExists(page);
return useAtomValue(getPageReferencesAtom(page));
}