fix(core): properties adapter reactivitiy issue (#5661)

This commit is contained in:
Peng Xiao
2024-01-23 01:41:44 +00:00
parent 03b60a63cd
commit ecdb5b3407
6 changed files with 51 additions and 34 deletions

View File

@@ -13,9 +13,9 @@ import { createAffineCloudBlobStorage } from '@affine/workspace-impl';
import { assertEquals } from '@blocksuite/global/utils';
import { Workspace } from '@blocksuite/store';
import { revertUpdate } from '@toeverything/y-indexeddb';
import { useMemo } from 'react';
import { useEffect, useMemo } from 'react';
import useSWRImmutable from 'swr/immutable';
import { applyUpdate } from 'yjs';
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
import {
useMutateQueryResource,
@@ -105,7 +105,7 @@ const snapshotFetcher = async (
const workspaceMap = new Map<string, Workspace>();
// assume the workspace is a cloud workspace since the history feature is only enabled for cloud workspace
const getOrCreateWorkspace = (workspaceId: string) => {
const getOrCreateShellWorkspace = (workspaceId: string) => {
let workspace = workspaceMap.get(workspaceId);
if (!workspace) {
const blobStorage = createAffineCloudBlobStorage(workspaceId);
@@ -120,6 +120,7 @@ const getOrCreateWorkspace = (workspaceId: string) => {
schema: globalBlockSuiteSchema,
});
workspaceMap.set(workspaceId, workspace);
workspace.doc.emit('sync', []);
}
return workspace;
};
@@ -143,17 +144,17 @@ export const usePageHistory = (
// workspace id + page id + timestamp + snapshot -> Page (to be used for rendering in blocksuite editor)
export const useSnapshotPage = (
workspaceId: string,
workspace: Workspace,
pageDocId: string,
ts?: string
) => {
const snapshot = usePageHistory(workspaceId, pageDocId, ts);
const snapshot = usePageHistory(workspace.id, pageDocId, ts);
const page = useMemo(() => {
if (!ts) {
return;
}
const pageId = pageDocId + '-' + ts;
const historyShellWorkspace = getOrCreateWorkspace(workspaceId);
const historyShellWorkspace = getOrCreateShellWorkspace(workspace.id);
let page = historyShellWorkspace.getPage(pageId);
if (!page && snapshot) {
page = historyShellWorkspace.createPage({
@@ -169,7 +170,15 @@ export const useSnapshotPage = (
.catch(console.error); // must load before applyUpdate
}
return page ?? undefined;
}, [pageDocId, snapshot, ts, workspaceId]);
}, [pageDocId, snapshot, ts, workspace]);
useEffect(() => {
const historyShellWorkspace = getOrCreateShellWorkspace(workspace.id);
// apply the rootdoc's update to the current workspace
// this makes sure the page reference links are not deleted ones in the preview
const update = encodeStateAsUpdate(workspace.doc);
applyUpdate(historyShellWorkspace.doc, update);
}, [workspace]);
return page;
};

View File

@@ -5,6 +5,7 @@ import { ConfirmModal, Modal } from '@affine/component/ui/modal';
import { openSettingModalAtom, type PageMode } from '@affine/core/atoms';
import { useIsWorkspaceOwner } from '@affine/core/hooks/affine/use-is-workspace-owner';
import { useAsyncCallback } from '@affine/core/hooks/affine-async-hooks';
import { useBlockSuiteWorkspacePageTitle } from '@affine/core/hooks/use-block-suite-workspace-page-title';
import { useUserSubscription } from '@affine/core/hooks/use-subscription';
import { waitForCurrentWorkspaceAtom } from '@affine/core/modules/workspace';
import { timestampToLocalTime } from '@affine/core/utils';
@@ -418,7 +419,7 @@ const PageHistoryManager = ({
return workspace.getPage(pageId)?.spaceDoc.guid ?? pageId;
}, [pageId, workspace]);
const snapshotPage = useSnapshotPage(workspaceId, pageDocId, activeVersion);
const snapshotPage = useSnapshotPage(workspace, pageDocId, activeVersion);
const t = useAFFiNEI18N();
@@ -440,10 +441,7 @@ const PageHistoryManager = ({
const defaultPreviewPageMode = useAtomValue(currentModeAtom);
const [mode, setMode] = useState<PageMode>(defaultPreviewPageMode);
const title = useMemo(
() => workspace.getPage(pageId)?.meta.title || t['Untitled'](),
[pageId, t, workspace]
);
const title = useBlockSuiteWorkspacePageTitle(workspace, pageId);
const [showRestoreConfirmModal, setShowRestoreConfirmModal] = useState(false);