Skip to content

Build your personal knowledge base with Trilium Notes

fix(collections): refresh note list when froca children array mutates in place

Elian Doran6613a6400d71
diff --git a/apps/client/src/widgets/collections/NoteList.spec.tsx b/apps/client/src/widgets/collections/NoteList.spec.tsx
index f49d02d5c6..557b6f74de 100644
--- a/apps/client/src/widgets/collections/NoteList.spec.tsx
+++ b/apps/client/src/widgets/collections/NoteList.spec.tsx
@@ -139,4 +139,48 @@ describe("useNoteIds refresh race", () => {
expect(currentNoteIds).toEqual(["child-a", "child-b"]);
});
+
+ it("hands React a fresh array when froca's live children array is mutated in place", async () => {
+ // The `includeArchived`/hidden/search paths of getChildNoteIdsWithArchiveFiltering return
+ // froca's *live* `note.children` array, which `addChild` mutates in place on a clone. If the
+ // hook re-published that same reference, React's Object.is bail-out would skip the update and
+ // downstream effects (note resolution, grid reconcile) would never see the new child.
+ const note = buildNote({ title: "Dashboard", type: "book" });
+ const liveChildren = [ "child-a", "child-b" ];
+ note.getChildNoteIdsWithArchiveFiltering = vi.fn(async () => liveChildren);
+
+ const parent = new Component();
+ container = document.createElement("div");
+ document.body.appendChild(container);
+
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={parent}>
+ <Harness note={note} />
+ </ParentComponent.Provider>,
+ container
+ );
+ });
+ // Drain the mount refresh's async chain (effect → getNoteIds → setNoteIds).
+ await act(async () => {
+ await flushMicrotasks();
+ });
+ expect(currentNoteIds).toEqual([ "child-a", "child-b" ]);
+ // Must be a copy, not froca's internal array.
+ expect(currentNoteIds).not.toBe(liveChildren);
+ const afterMount = currentNoteIds;
+
+ // Simulate the clone: addChild pushes into the same array, then an entitiesReloaded arrives.
+ liveChildren.push("child-new");
+ await act(async () => {
+ await parent.handleEvent("entitiesReloaded", {
+ loadResults: branchEntitiesReloaded(note.noteId, "child-new")
+ });
+ await flushMicrotasks();
+ });
+
+ expect(currentNoteIds).toEqual([ "child-a", "child-b", "child-new" ]);
+ // A new reference is what makes React/`useEffect([noteIds])` downstream actually re-run.
+ expect(currentNoteIds).not.toBe(afterMount);
+ });
});
diff --git a/apps/client/src/widgets/collections/NoteList.tsx b/apps/client/src/widgets/collections/NoteList.tsx
index 73b4324d78..a0bd213472 100644
--- a/apps/client/src/widgets/collections/NoteList.tsx
+++ b/apps/client/src/widgets/collections/NoteList.tsx
@@ -170,7 +170,10 @@ export function useNoteIds(note: FNote | null | undefined, viewType: ViewTypeOpt
// A newer refresh was issued while we were awaiting; let it win.
return;
}
- setNoteIds(result);
+ // getNoteIds may hand back froca's live children array, which is mutated in place when notes
+ // are cloned/removed. Setting that same reference would be a no-op for React (Object.is), so
+ // downstream effects never re-run. Copy it, and skip the update only when nothing changed.
+ setNoteIds(prev => sameNoteIds(prev, result) ? prev : [ ...result ]);
}
async function getNoteIds(note: FNote) {
@@ -229,6 +232,11 @@ export function useNoteIds(note: FNote | null | undefined, viewType: ViewTypeOpt
return noteIds;
}
+/** Order-sensitive equality for note-id lists, used to skip no-op `noteIds` updates. */
+function sameNoteIds(a: string[], b: string[]) {
+ return a.length === b.length && a.every((id, index) => id === b[index]);
+}
+
export function useViewModeConfig<T extends object>(note: FNote | null | undefined, viewType: ViewModeStorageType | undefined) {
const [ viewConfig, setViewConfig ] = useState<{
config: T | undefined;