Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(attributes): delete the confirmed rows after a reload or note change
Claudec58e9d787992
diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsxindex 1e9aa894d0..bf81a4394d 100644--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx@@ -972,6 +972,44 @@ describe("AttributeList", () => {expect(selectedNames()).toEqual([ "cssClass", "template", "inheritedLabel" ]);});+ it("deletes the picked rows by attributeId once confirmed, whatever a reload did meanwhile", async () => {+ const note = noteWithAttributes();+ renderPanel(note, true);+ pick(0, { ctrlKey: true });+ pick(1, { ctrlKey: true });++ // A reload rebuilds the row objects while the confirmation is open.+ let answer: (confirmed: boolean) => void = () => {};+ confirm.mockImplementationOnce(() => new Promise<boolean>((resolve) => { answer = resolve; }));+ rightPress(1);+ const deletion = menuItem("bx bx-trash")?.handler?.();+ act(() => fireEntitiesReloaded(() => affectingRows(note)));+ await act(async () => {+ answer(true);+ await deletion;+ });++ expect(put).toHaveBeenCalledOnce();+ const [ url, saved ] = put.mock.calls[0] as [ string, Attribute[] ];+ expect(url).toBe("notes/subject/attributes");+ expect(saved.map((attribute) => attribute.name)).toEqual([ "template", "label:priority" ]);++ // Confirmed after reading another note, the deletion is dropped instead of saving that note's+ // attributes over the one the rows were picked on.+ put.mockClear();+ pick(0, { ctrlKey: true });+ confirm.mockImplementationOnce(() => new Promise<boolean>((resolve) => { answer = resolve; }));+ rightPress(0);+ const lateDeletion = menuItem("bx bx-trash")?.handler?.();+ showNote(buildNote({ id: "elsewhere", title: "Elsewhere", "#other": "x" }));+ await act(async () => {+ answer(true);+ await lateDeletion;+ });+ expect(put).not.toHaveBeenCalled();+ expect(namesIn(container)).toEqual([ "other" ]);+ });+it("keeps a press on a picked row from whatever it lands on within it", () => {renderPanel(noteWithAttributes());diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsxindex f1929e2f5e..e0dcc3aaf3 100644--- a/apps/client/src/widgets/sidebar/AttributeList.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx@@ -306,17 +306,37 @@ export default function AttributeList() {*/async function deleteAttribute(attribute: Attribute) {const name = getDisplayName(attribute, getAttributeKind(attribute));+ const noteId = note?.noteId ?? null;if (!await dialog.confirm(t("attribute_list_panel.delete_confirm", { name }))) {return;}- owned.current = owned.current.filter((candidate) => candidate !== attribute);+ if (!removeOwned(noteId, [ attribute ])) {+ return;+ }setDetail(null);rerender();await save();}+ /**+ * Drops the given attributes from the draft, matched by `attributeId` when they have one since a+ * reload during the confirmation rebuilds the row objects. Returns `false`, changing nothing, once+ * the panel shows another note than `noteId`.+ */+ function removeOwned(noteId: string | null, attributes: Attribute[]) {+ if (shownNoteId.current !== noteId) {+ return false;+ }++ const ids = new Set(attributes.flatMap((attribute) => attribute.attributeId ?? []));+ owned.current = owned.current.filter((candidate) => candidate.attributeId+ ? !ids.has(candidate.attributeId)+ : !attributes.includes(candidate));+ return true;+ }+const sections = splitIntoSections(owned.current, inherited.current);const internalRows = internal.current.map((attribute) => toEntry(attribute, true));@@ -593,11 +613,14 @@ export default function AttributeList() {return;}+ const noteId = note?.noteId ?? null;if (!await dialog.confirm(t("attribute_list_panel.delete_selection_confirm", { count: picked.length }))) {return;}- owned.current = owned.current.filter((candidate) => !picked.includes(candidate));+ if (!removeOwned(noteId, picked)) {+ return;+ }clearSelection();setDetail(null);rerender();