Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix an issue

Adorian Doran000ce08cd670
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index d72b2a39cf..86e6f4ab90 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -1,6 +1,7 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import FAttribute from "../../../entities/fattribute";
+import { executeBulkActions } from "../../../services/bulk_action";
import FNote from "../../../entities/fnote";
import froca from "../../../services/froca";
import noteAttributeCache from "../../../services/note_attribute_cache";
@@ -14,6 +15,11 @@ vi.mock("../../../services/bulk_action", () => ({
executeBulkActions: vi.fn(async () => {})
}));
+/** Makes the next bulk action fail, as an offline or rejecting server does. */
+function failNextBulkAction() {
+ vi.mocked(executeBulkActions).mockRejectedValueOnce(new Error("offline"));
+}
+
vi.mock("../../../services/i18n", () => ({
// i18next is never initialised under test, so the real `t` yields nothing and the alias would
// silently vanish from the definitions asserted below rather than being checked.
@@ -151,6 +157,38 @@ describe("BoardApi column mutations", () => {
expect([ ...pendingRenames ]).toEqual([ [ "Done", "Shipped" ] ]);
});
+ /**
+ * A record left behind by a write that never landed goes on being applied: the column would show
+ * under a name nothing carries, or stay hidden though it is still there.
+ */
+ it("keeps no record of a rename or a deletion the server refused", async () => {
+ const { api, saved, pendingRenames } = createApi(
+ { columns: [ { value: "To Do" }, { value: "Done" } ] },
+ [ "To Do", "Done" ]
+ );
+
+ failNextBulkAction();
+ await expect(api.renameColumn("Done", "Shipped")).rejects.toThrow("offline");
+
+ failNextBulkAction();
+ await expect(api.removeColumn("To Do")).rejects.toThrow("offline");
+
+ expect([ ...pendingRenames ]).toEqual([]);
+ expect(saved).toHaveLength(0);
+ });
+
+ it("restores the record of an earlier rename when a later one is refused", async () => {
+ const { api, pendingRenames } = createApi({ columns: [ { value: "Done" } ] }, [ "Done" ]);
+
+ await api.renameColumn("Done", "Shipped");
+
+ // The refused rename re-pointed the first one on its way in, which has to be put back.
+ failNextBulkAction();
+ await expect(api.renameColumn("Shipped", "Delivered")).rejects.toThrow("offline");
+
+ expect([ ...pendingRenames ]).toEqual([ [ "Done", "Shipped" ] ]);
+ });
+
it("follows a rename through when the one before it has not landed yet", async () => {
const { api, pendingRenames } = createApi({ columns: [ { value: "Done" } ] }, [ "Done" ]);
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 246ef878e7..8268892c1b 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -78,28 +78,27 @@ export default class BoardApi {
}
async removeColumn(column: string) {
- this.retireColumn(column);
-
// Remove the value from the notes.
const noteIds = this.byColumn?.get(column)?.map(item => item.note.noteId) || [];
const action: BulkAction = this.isRelationMode
? { name: "deleteRelation", relationName: this.statusAttribute }
: { name: "deleteLabel", labelName: this.statusAttribute };
- await executeBulkActions(noteIds, [ action ], { silent: true });
+ await this.retiredWhile(column, undefined,
+ () => executeBulkActions(noteIds, [ action ], { silent: true }));
+
this.storeColumns((this.viewConfig?.columns ?? []).filter(col => col.value !== column));
}
async renameColumn(oldValue: string, newValue: string) {
- this.retireColumn(oldValue, newValue);
-
const noteIds = this.byColumn?.get(oldValue)?.map(item => item.note.noteId) || [];
// Change the value in the notes.
const action: BulkAction = this.isRelationMode
? { name: "updateRelationTarget", relationName: this.statusAttribute, targetNoteId: newValue }
: { name: "updateLabelValue", labelName: this.statusAttribute, labelValue: newValue };
- await executeBulkActions(noteIds, [ action ], { silent: true });
+ await this.retiredWhile(oldValue, newValue,
+ () => executeBulkActions(noteIds, [ action ], { silent: true }));
// Rename the column in the persisted data.
this.storeColumns((this.viewConfig?.columns ?? [])
@@ -168,6 +167,30 @@ export default class BoardApi {
return newColumns;
}
+ /**
+ * Runs the write that moves a column, with the record of it in place from the start so that a
+ * refresh in the middle reads the sources as they are about to be.
+ *
+ * Taken back out if the write does not land: left in, a rename nothing carries would keep the
+ * column under the name it failed to take, and a deletion that failed would keep hiding a column
+ * that is still there, cards and all. The record is restored rather than merely dropped, since
+ * {@link retireColumn} may have re-pointed an earlier one that has not landed either.
+ */
+ private async retiredWhile<T>(oldValue: string, newValue: string | undefined, write: () => Promise<T>) {
+ const restore = new Map(this.pendingRenames);
+ this.retireColumn(oldValue, newValue);
+
+ try {
+ return await write();
+ } catch (e) {
+ this.pendingRenames.clear();
+ for (const [ from, to ] of restore) {
+ this.pendingRenames.set(from, to);
+ }
+ throw e;
+ }
+ }
+
/**
* Records what a column became, so that `resolveBoardColumns` reads whichever of the notes, the
* view config and the definition has not been written yet as though it already were. That must