Skip to content

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

fix: prevent editor outline update loop

tianma-ifbae2b269e3be
diff --git a/apps/web/src/components/EditorOutline.tsx b/apps/web/src/components/EditorOutline.tsx
index a12a250e..43a876cc 100644
--- a/apps/web/src/components/EditorOutline.tsx
+++ b/apps/web/src/components/EditorOutline.tsx
@@ -44,6 +44,12 @@ const getOutlineItems = (editor: Editor): OutlineItem[] => {
return items;
};
+const sameOutlineItems = (left: OutlineItem[], right: OutlineItem[]) =>
+ left.length === right.length && left.every((item, index) => {
+ const other = right[index];
+ return other?.level === item.level && other.pos === item.pos && other.text === item.text;
+ });
+
export const EditorOutline = ({ editor, scrollContainer, collapsed, onCollapsedChange }: EditorOutlineProps) => {
const { t } = useTranslation();
const [items, setItems] = useState<OutlineItem[]>([]);
@@ -55,7 +61,8 @@ export const EditorOutline = ({ editor, scrollContainer, collapsed, onCollapsedC
return;
}
- setItems(getOutlineItems(editor));
+ const nextItems = getOutlineItems(editor);
+ setItems((currentItems) => (sameOutlineItems(currentItems, nextItems) ? currentItems : nextItems));
}, [editor]);
const updateActiveItem = useCallback(() => {
@@ -69,7 +76,8 @@ export const EditorOutline = ({ editor, scrollContainer, collapsed, onCollapsedC
item.pos <= selectionPos ? item : current
), null);
- setActivePos(activeItem?.pos ?? items[0]?.pos ?? null);
+ const nextActivePos = activeItem?.pos ?? items[0]?.pos ?? null;
+ setActivePos((currentActivePos) => (currentActivePos === nextActivePos ? currentActivePos : nextActivePos));
}, [editor, items]);
useEffect(() => {
@@ -107,7 +115,8 @@ export const EditorOutline = ({ editor, scrollContainer, collapsed, onCollapsedC
}
if (activeItem) {
- setActivePos(activeItem.pos);
+ const nextActivePos = activeItem.pos;
+ setActivePos((currentActivePos) => (currentActivePos === nextActivePos ? currentActivePos : nextActivePos));
}
};
diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsx
index bc10a9af..53cf2bac 100644
--- a/apps/web/src/components/WorkspaceApp.tsx
+++ b/apps/web/src/components/WorkspaceApp.tsx
@@ -1625,6 +1625,15 @@ export const WorkspaceApp = ({
const previousActivePane = activePane;
const previousSelectedMemoId = selectedMemoId;
+ // Leave the editor before cancelling/refetching its detail query. On a
+ // desktop layout the editor remains mounted even when the memo pane is
+ // visible, so keeping a trashed memo selected while its query is being
+ // invalidated can render a deleted detail and repeatedly re-select it.
+ setEmptyTrashConfirmationOpen(false);
+ clearMemoSelection();
+ setSelectedMemoId(null);
+ setActivePane("memos");
+
await Promise.all([
queryClient.cancelQueries({ queryKey: ["memos"] }),
queryClient.cancelQueries({ queryKey: ["memo"] }),
@@ -1638,10 +1647,6 @@ export const WorkspaceApp = ({
// memo detail query here can make the editor render with a missing memo
// during the same React update and blank the whole workspace.
clearTrashMemoLists(queryClient);
- setEmptyTrashConfirmationOpen(false);
- clearMemoSelection();
- setSelectedMemoId(null);
- setActivePane("memos");
return { previousMemoLists, previousMemoDetails, previousActivePane, previousSelectedMemoId };
},
@@ -1659,6 +1664,20 @@ export const WorkspaceApp = ({
description: t("workspaceDialogs.emptyTrashFailedDescription"),
});
},
+ onSuccess: () => {
+ // The trash detail queries are no longer valid after a successful
+ // permanent delete. Remove them before invalidating the remaining
+ // active queries so an editor cannot briefly observe a 404 detail.
+ queryClient.removeQueries({
+ queryKey: ["memo"],
+ predicate: (query) => {
+ const data = query.state.data as { memo?: { isDeleted?: boolean } } | undefined;
+ return data?.memo?.isDeleted === true;
+ },
+ });
+ setSelectedMemoId(null);
+ setActivePane("memos");
+ },
onSettled: () => {
void Promise.all([
queryClient.invalidateQueries({ queryKey: ["memos"], refetchType: "active" }),