mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix(core): possible crash issues (#4783)
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import type { Workspace } from '@blocksuite/store';
|
||||
import { useBlockSuitePagePreview } from '@toeverything/hooks/use-block-suite-page-preview';
|
||||
import { useBlockSuiteWorkspacePage } from '@toeverything/hooks/use-block-suite-workspace-page';
|
||||
@@ -12,7 +11,6 @@ interface PagePreviewInnerProps {
|
||||
|
||||
const PagePreviewInner = ({ workspace, pageId }: PagePreviewInnerProps) => {
|
||||
const page = useBlockSuiteWorkspacePage(workspace, pageId);
|
||||
assertExists(page);
|
||||
const previewAtom = useBlockSuitePagePreview(page);
|
||||
const preview = useAtomValue(previewAtom);
|
||||
return preview ? preview : null;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import type { AffineOfficialWorkspace } from '@affine/env/workspace';
|
||||
import {
|
||||
type AffineOfficialWorkspace,
|
||||
WorkspaceFlavour,
|
||||
} from '@affine/env/workspace';
|
||||
import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import type { Workspace } from '@blocksuite/store';
|
||||
import { getBlockSuiteWorkspaceAtom } from '@toeverything/infra/__internal__/workspace';
|
||||
import type { Atom } from 'jotai';
|
||||
@@ -24,12 +26,21 @@ export function useWorkspace(workspaceId: string): AffineOfficialWorkspace {
|
||||
const workspace = useAtomValue(workspaceAtom);
|
||||
if (!workspaceWeakMap.has(workspace)) {
|
||||
const baseAtom = atom(async get => {
|
||||
const metadata = await get(rootWorkspacesMetadataAtom);
|
||||
const flavour = metadata.find(({ id }) => id === workspaceId)?.flavour;
|
||||
assertExists(flavour, 'workspace flavour not found');
|
||||
const metadataList = await get(rootWorkspacesMetadataAtom);
|
||||
const flavour = metadataList.find(({ id }) => id === workspaceId)
|
||||
?.flavour;
|
||||
|
||||
if (!flavour) {
|
||||
// when last workspace is removed, we may encounter this warning. it should be fine
|
||||
console.warn(
|
||||
'workspace not found in rootWorkspacesMetadataAtom, maybe it is removed',
|
||||
workspaceId
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
id: workspaceId,
|
||||
flavour,
|
||||
flavour: flavour ?? WorkspaceFlavour.LOCAL,
|
||||
blockSuiteWorkspace: workspace,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -17,8 +17,12 @@ export const getPagePreviewText = (page: Page) => {
|
||||
return text.slice(0, 300);
|
||||
};
|
||||
|
||||
export function useBlockSuitePagePreview(page: Page): Atom<string> {
|
||||
if (weakMap.has(page)) {
|
||||
const emptyAtom = atom<string>('');
|
||||
|
||||
export function useBlockSuitePagePreview(page: Page | null): Atom<string> {
|
||||
if (page === null) {
|
||||
return emptyAtom;
|
||||
} else if (weakMap.has(page)) {
|
||||
return weakMap.get(page) as Atom<string>;
|
||||
} else {
|
||||
const baseAtom = atom<string>(getPagePreviewText(page));
|
||||
|
||||
Reference in New Issue
Block a user