Skip to content

Build your personal knowledge base with Trilium Notes

fix(collections): don't re-run the filter on content saves

Elian Doranc4ee28a9013e
diff --git a/apps/client/src/widgets/collections/collection_filter.spec.tsx b/apps/client/src/widgets/collections/collection_filter.spec.tsx
index f01e8e4fe8..a5cd82a72e 100644
--- a/apps/client/src/widgets/collections/collection_filter.spec.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.spec.tsx
@@ -67,6 +67,15 @@ function branchEntitiesReloaded(parentNoteId: string, childNoteId: string) {
return loadResults;
}
+function noteEntitiesReloaded(noteId: string, { contentChanged }: { contentChanged: boolean }) {
+ const loadResults = new LoadResults([]);
+ loadResults.addNote(noteId, "comp-1");
+ if (contentChanged) {
+ loadResults.addNoteContent(noteId, "comp-1");
+ }
+ return loadResults;
+}
+
async function flushMicrotasks() {
await Promise.resolve();
await Promise.resolve();
@@ -363,6 +372,34 @@ describe("useCollectionFilter", () => {
expect(searchInSubtree).toHaveBeenCalledTimes(2);
});
+ it("re-runs for a note's own change but not for a save of its content", async () => {
+ vi.useFakeTimers();
+ searchInSubtree.mockResolvedValue(matchResponse([ "n1" ]));
+ await mount({ collectionNoteIds: [ "n1", "n2" ] });
+
+ await act(async () => {
+ currentFilter?.setQuery("#done");
+ await vi.advanceTimersByTimeAsync(0);
+ });
+ expect(searchInSubtree).toHaveBeenCalledTimes(1);
+
+ await act(async () => {
+ await parent.handleEvent("entitiesReloaded", {
+ loadResults: noteEntitiesReloaded("n1", { contentChanged: true })
+ });
+ await vi.advanceTimersByTimeAsync(400);
+ });
+ expect(searchInSubtree).toHaveBeenCalledTimes(1);
+
+ await act(async () => {
+ await parent.handleEvent("entitiesReloaded", {
+ loadResults: noteEntitiesReloaded("n2", { contentChanged: false })
+ });
+ await vi.advanceTimersByTimeAsync(400);
+ });
+ expect(searchInSubtree).toHaveBeenCalledTimes(2);
+ });
+
it("does not re-run while no filter is active", async () => {
const note = await mount({ collectionNoteIds: [ "n1" ] });
vi.useFakeTimers();
diff --git a/apps/client/src/widgets/collections/collection_filter.tsx b/apps/client/src/widgets/collections/collection_filter.tsx
index febda90e9e..261a2c42b5 100644
--- a/apps/client/src/widgets/collections/collection_filter.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.tsx
@@ -273,14 +273,19 @@ export function CollectionFilterInput({ filter, placeholder }: {
);
}
-/** Whether any changed note, attribute or branch belongs to the collection's subtree. */
+/**
+ * Whether any changed note, attribute or branch belongs to the collection's subtree. A content save
+ * does not count: an editor saves about once a second while the user types, and each re-run is a
+ * full search on the server.
+ */
function touchesCollection(
loadResults: LoadResults, collectionNoteIds: Set<string>, collectionNoteId: string
) {
const inCollection = (noteId: string | null | undefined) =>
!!noteId && (noteId === collectionNoteId || collectionNoteIds.has(noteId));
- return loadResults.getNoteIds().some(inCollection)
+ return loadResults.getNoteIds().some(noteId =>
+ inCollection(noteId) && !loadResults.isNoteContentReloaded(noteId))
|| loadResults.getAttributeRows().some(attr => inCollection(attr.noteId))
|| loadResults.getBranchRows().some(branch =>
inCollection(branch.noteId) || inCollection(branch.parentNoteId));