Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(document): render the preview when opening a document while another is active (#21944)

LiJian16e0cbf80eed
diff --git a/src/store/document/slices/document/action.test.ts b/src/store/document/slices/document/action.test.ts
index 36006c5d62a..24e7d939e11 100644
--- a/src/store/document/slices/document/action.test.ts
+++ b/src/store/document/slices/document/action.test.ts
@@ -9,7 +9,7 @@
import { randomUUID } from 'node:crypto';
import { act, render, renderHook, waitFor } from '@testing-library/react';
-import { createElement } from 'react';
+import { createElement, useEffect } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cacheScope, replicaSWRDriver } from '@/libs/replica';
@@ -377,6 +377,41 @@ describe('document detail replica', () => {
expect(useDocumentStore.getState().documents['doc-1']?.content).toBe('# Cached');
});
+ it('adopts an in-memory document once it becomes active after another document was open', async () => {
+ vi.mocked(documentService.getDocumentById).mockImplementation(
+ async (id: string) => documentRow({ id, content: `# ${id}` }) as any,
+ );
+ // Open doc-1, then leave it without closing: `activeDocumentId` stays on it,
+ // as it does when the portal is closed.
+ const first = renderDocument('doc-1', createEditor());
+ await waitFor(() => expect(useDocumentStore.getState().documents['doc-1']).toBeDefined());
+ first.unmount();
+ expect(useDocumentStore.getState().activeDocumentId).toBe('doc-1');
+
+ // The portal body reads doc-2 before the editor mounts, so its entry is
+ // already in memory and will not be written again.
+ await act(async () => {
+ await useDocumentStore.getState().prefetchDocument('doc-2');
+ });
+ vi.mocked(documentService.getDocumentById).mockImplementation(pending as any);
+
+ // Mirror `DocumentIdMode`: the hook adopts in a layout effect, the component
+ // claims `activeDocumentId` in a later passive effect.
+ const editor = createEditor();
+ const Probe = () => {
+ useDocumentStore((s) => s.useFetchDocument)('doc-2', { editor, sourceType: 'notebook' });
+ useEffect(() => {
+ useDocumentStore.setState({ activeDocumentId: 'doc-2' });
+ }, []);
+ return null;
+ };
+ render(createElement(Probe), { wrapper });
+
+ await waitFor(() =>
+ expect(useDocumentStore.getState().documents['doc-2']?.content).toBe('# doc-2'),
+ );
+ });
+
it('drops the previous identity’s document, projection and editor state included', async () => {
vi.mocked(documentService.getDocumentById).mockResolvedValue(documentRow() as any);
const editor = createEditor();
diff --git a/src/store/document/slices/document/action.ts b/src/store/document/slices/document/action.ts
index fa114fb14c4..ce35f9e7852 100644
--- a/src/store/document/slices/document/action.ts
+++ b/src/store/document/slices/document/action.ts
@@ -492,8 +492,18 @@ export class DocumentActionImpl {
return;
}
// Only react to *this* entry changing; every other store write (typing
- // in the editor included) is none of our business.
- if (previous && previous.documentDetailMap[documentId] === next) return;
+ // in the editor included) is none of our business — except this
+ // document becoming active while still unadopted. `#adoptDocument`
+ // skips an entry while another document is active, and the caller
+ // (`DocumentIdMode`) only claims `activeDocumentId` in a passive effect,
+ // after this layout effect ran. An entry already in memory never writes
+ // again, so without this retry the editor stays on its skeleton until
+ // the document is reopened.
+ if (previous && previous.documentDetailMap[documentId] === next) {
+ const becameActive =
+ state.activeDocumentId === documentId && previous.activeDocumentId !== documentId;
+ if (!becameActive || state.documents[documentId]) return;
+ }
// Entries are scope-owned by the engine; never adopt one that belongs to
// another identity (this hook can stay mounted across an identity switch).
const scope = state.documentDetailReplica?.scope;