Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix: restore all notes after external notebook deletion

tianma-ifcce5f7ec34c2
diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsx
index 69988cba..14fbb791 100644
--- a/apps/web/src/components/WorkspaceApp.tsx
+++ b/apps/web/src/components/WorkspaceApp.tsx
@@ -118,6 +118,7 @@ import { useWorkspaceRoute } from "@/hooks/useWorkspaceRoute";
import { useNoteProse } from "@/hooks/useNoteProse";
import { useWorkspacePreferences } from "@/hooks/useWorkspacePreferences";
import { useWorkspaceSelection } from "@/hooks/useWorkspaceSelection";
+import { useNotebookSelectionReconciliation } from "@/hooks/useNotebookSelectionReconciliation";
import { useWorkspaceQueuedSync } from "@/hooks/useWorkspaceQueuedSync";
import { useWorkspaceMemoRemoval } from "@/hooks/useWorkspaceMemoRemoval";
import { EdgeEverPluginHost, type RegisteredPluginPanel } from "@/lib/plugins/plugin-host";
@@ -1230,6 +1231,16 @@ export const WorkspaceApp = ({
},
});
+ useNotebookSelectionReconciliation({
+ notebooks: notebooksQuery.isSuccess && !notebooksQuery.isFetching && !createNotebookMutation.isPending
+ ? notebooksQuery.data.notebooks
+ : undefined,
+ selectedNotebookId,
+ setSelectedNotebookId,
+ setSelectedMemoId,
+ clearMemoSelection,
+ });
+
const revealCreatedMemo = (memo: MemoDetail) => {
const targetNotebookId = memo.notebookId;
const isStructuredNote = Boolean(parseDiagramDocument(memo.contentMarkdown) || parseTableDocument(memo.contentMarkdown) || parseInfographicDocument(memo.contentMarkdown) || hasPosterDocumentMarker(memo.contentMarkdown));
diff --git a/apps/web/src/hooks/useNotebookSelectionReconciliation.test.mjs b/apps/web/src/hooks/useNotebookSelectionReconciliation.test.mjs
new file mode 100644
index 00000000..bd9cdb1a
--- /dev/null
+++ b/apps/web/src/hooks/useNotebookSelectionReconciliation.test.mjs
@@ -0,0 +1,76 @@
+import { afterEach, expect, test } from "bun:test";
+import { act, createElement } from "react";
+import { createRoot } from "react-dom/client";
+import { parseHTML } from "linkedom";
+import { useWorkspaceSelection } from "./useWorkspaceSelection.ts";
+import { useNotebookSelectionReconciliation } from "./useNotebookSelectionReconciliation.ts";
+
+const globals = ["window", "document", "IS_REACT_ACT_ENVIRONMENT"];
+const originals = new Map(globals.map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]));
+let root;
+
+afterEach(async () => {
+ if (root) await act(async () => root.unmount());
+ root = undefined;
+ for (const [key, descriptor] of originals) {
+ if (descriptor) Object.defineProperty(globalThis, key, descriptor);
+ else delete globalThis[key];
+ }
+});
+
+const mount = async (initialNotebookId = "test") => {
+ const { window } = parseHTML("<html><body><div id='root'></div></body></html>");
+ globalThis.window = window;
+ globalThis.document = window.document;
+ globalThis.IS_REACT_ACT_ENVIRONMENT = true;
+ root = createRoot(window.document.getElementById("root"));
+ let selection;
+ const Harness = ({ notebooks }) => {
+ selection = useWorkspaceSelection({ selectedNotebookId: initialNotebookId, selectedMemoId: "memo" });
+ useNotebookSelectionReconciliation({ notebooks, ...selection });
+ return createElement("div", null, selection.selectedNotebookId ?? "all");
+ };
+ const render = async (notebooks) => {
+ await act(async () => root.render(createElement(Harness, { notebooks })));
+ return selection;
+ };
+ return { render, selection: () => selection, text: () => window.document.body.textContent };
+};
+
+test("external deletion returns to all notes and clears stale single and bulk selections", async () => {
+ const h = await mount();
+ await h.render([{ id: "test" }, { id: "inbox" }]);
+ await act(async () => h.selection().replaceMemoSelection(["memo"]));
+ await h.render([{ id: "inbox" }]);
+ expect(h.text()).toBe("all");
+ expect(h.selection().selectedMemoId).toBeNull();
+ expect(h.selection().selectedMemoIds.size).toBe(0);
+ expect(h.selection().memoSelectionMode).toBe(false);
+});
+
+test("loading or an unsettled catalog preserves selection, including newly created notebooks", async () => {
+ const h = await mount();
+ await h.render(undefined);
+ expect(h.selection().selectedNotebookId).toBe("test");
+ await h.render([{ id: "test" }]);
+ await h.render(undefined);
+ await act(async () => h.selection().setSelectedNotebookId("new"));
+ expect(h.selection().selectedNotebookId).toBe("new");
+ await h.render([{ id: "test" }, { id: "new" }]);
+ expect(h.selection().selectedNotebookId).toBe("new");
+});
+
+test("deleting another notebook preserves current selection", async () => {
+ const h = await mount();
+ await h.render([{ id: "test" }, { id: "other" }]);
+ await h.render([{ id: "test" }]);
+ expect(h.selection().selectedNotebookId).toBe("test");
+ expect(h.selection().selectedMemoId).toBe("memo");
+});
+
+test("a settled empty catalog clears a restored missing notebook", async () => {
+ const h = await mount();
+ await h.render(undefined);
+ await h.render([]);
+ expect(h.selection().selectedNotebookId).toBeNull();
+});
diff --git a/apps/web/src/hooks/useNotebookSelectionReconciliation.ts b/apps/web/src/hooks/useNotebookSelectionReconciliation.ts
new file mode 100644
index 00000000..03fb2656
--- /dev/null
+++ b/apps/web/src/hooks/useNotebookSelectionReconciliation.ts
@@ -0,0 +1,24 @@
+import { useEffect } from "react";
+
+// An external deletion can remove the selected notebook without going through
+// the UI mutation. Wait for a settled catalog before returning to all notes.
+export const useNotebookSelectionReconciliation = ({
+ notebooks,
+ selectedNotebookId,
+ setSelectedNotebookId,
+ setSelectedMemoId,
+ clearMemoSelection,
+}: {
+ notebooks: ReadonlyArray<{ id: string }> | undefined;
+ selectedNotebookId: string | null;
+ setSelectedNotebookId: (id: string | null) => void;
+ setSelectedMemoId: (id: string | null) => void;
+ clearMemoSelection: () => void;
+}) => {
+ useEffect(() => {
+ if (!notebooks || !selectedNotebookId || notebooks.some((notebook) => notebook.id === selectedNotebookId)) return;
+ setSelectedNotebookId(null);
+ setSelectedMemoId(null);
+ clearMemoSelection();
+ }, [notebooks, selectedNotebookId, setSelectedNotebookId, setSelectedMemoId, clearMemoSelection]);
+};