Skip to content

Build your personal knowledge base with Trilium Notes

fix(board): keep a deleted grouping's key and wait for its deletion (closes #12064)

Elian Doran25ea5ba7ae19
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index 4ea051409a..2961ef8267 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -2171,7 +2171,7 @@ describe("deleting the attribute of a grouping", () => {
* Left in `board.json`, the columns of a deleted grouping are written back into a new
* definition by `syncColumnsToDefinition` once the board groups by it again.
*/
- it("drops the stored columns of the grouping once deleted, leaving the rest", async () => {
+ it("empties the stored columns of the grouping once deleted, leaving the rest", async () => {
const { api, saved } = createApi({
columns: [ { value: "To Do" }, { value: "Done" } ],
testViewColumns: [ { value: "A" } ],
@@ -2182,7 +2182,9 @@ describe("deleting the attribute of a grouping", () => {
await api.deleteGrouping("status", async () => { savedDuringDeletion.push(saved.length); });
expect(savedDuringDeletion).toEqual([ 0 ]);
- expect(saved).toEqual([ { testViewColumns: [ { value: "A" } ], filterQuery: "#urgent" } ]);
+ expect(saved).toEqual([
+ { columns: [], testViewColumns: [ { value: "A" } ], filterQuery: "#urgent" }
+ ]);
});
/**
@@ -2199,7 +2201,7 @@ describe("deleting the attribute of a grouping", () => {
});
expect(inFlightDuringDeletion).toBe(1);
- expect(saved).toEqual([ {} ]);
+ expect(saved).toEqual([ { columns: [] } ]);
expect(pending.inFlight).toBe(0);
});
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 842e20fb8d..f25695c67f 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -30,7 +30,7 @@ import {
DEFAULT_CARD_TEMPLATES, DEFAULT_COLUMN_ICON, DEFAULT_COLUMN_WIDTH, DEFAULT_GROUP_BY, INBOX_COLUMN,
INBOX_COLUMN_ICON, parseColumnWidth
} from "./columns";
-import { readColumns, removeColumns, writeColumns } from "./column_storage";
+import { readColumns, writeColumns } from "./column_storage";
import { cardNotes, ColumnItem, ColumnMap } from "./data";
import {
cardReference, ColumnReferenceLabel, columnReference, newColumnId, readColumnId
@@ -1040,9 +1040,10 @@ export default class BoardApi {
}
/**
- * Runs `deletion`, which deletes the attribute a grouping is made from, then drops the columns
+ * Runs `deletion`, which deletes the attribute a grouping is made from, then empties the columns
* stored for that grouping so that `syncColumnsToDefinition()` cannot write them back into a
- * new definition.
+ * new definition. The empty list keeps the grouping's key, without which
+ * `adoptLegacyColumns()` reads the default grouping's `columns` as this grouping's.
*
* Counted in `pending.inFlight` while `deletion` runs. A refresh that resolves later still
* holds the old config, and `hasStoredSince()` keeps it from writing.
@@ -1052,10 +1053,11 @@ export default class BoardApi {
try {
await deletion();
- const removed = removeColumns(this.viewConfig, groupBy);
- if (removed) {
- this.viewConfig = removed;
- this.saveConfig(removed);
+ // A board that still reads the deleted definition fills an empty list from its options.
+ await ws.waitForMaxKnownEntityChangeId();
+ if (readColumns(this.viewConfig, groupBy)) {
+ this.viewConfig = writeColumns(this.viewConfig, groupBy, []);
+ this.saveConfig(this.viewConfig);
}
} finally {
this.pending.inFlight--;
diff --git a/apps/client/src/widgets/collections/board/column_storage.spec.ts b/apps/client/src/widgets/collections/board/column_storage.spec.ts
index 89bcb05d8d..71bd88b429 100644
--- a/apps/client/src/widgets/collections/board/column_storage.spec.ts
+++ b/apps/client/src/widgets/collections/board/column_storage.spec.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import type { BoardViewData } from ".";
-import { adoptLegacyColumns, readColumns, removeColumns, writeColumns } from "./column_storage";
+import { adoptLegacyColumns, readColumns, writeColumns } from "./column_storage";
const CONFIG: BoardViewData = {
columns: [ { value: "To Do" }, { value: "Done", icon: "bx bx-check" } ],
@@ -55,42 +55,6 @@ describe("writeColumns", () => {
});
});
-describe("removeColumns", () => {
- it("drops one grouping's columns without touching another's or the rest of the config", () => {
- const removed = removeColumns(CONFIG, "priority");
-
- expect(removed).not.toHaveProperty("priorityViewColumns");
- expect(removed?.columns).toBe(CONFIG.columns);
- expect(removed?.templates).toBe(CONFIG.templates);
- expect(CONFIG.priorityViewColumns).toEqual([ { value: "High", color: "#f00" } ]);
- });
-
- it("drops the default grouping's columns from the key every board already uses", () => {
- for (const groupBy of [ "status", "#status" ]) {
- const removed = removeColumns(CONFIG, groupBy);
-
- expect(removed, groupBy).not.toHaveProperty("columns");
- expect(removed?.priorityViewColumns, groupBy).toBe(CONFIG.priorityViewColumns);
- }
- });
-
- it("keeps a relation apart from the label of the same name", () => {
- const config: BoardViewData = {
- "priorityViewColumns": [ { value: "High" } ],
- "~priorityViewColumns": [ { value: "someNoteId" } ]
- };
-
- expect(removeColumns(config, "~priority")).toEqual({
- priorityViewColumns: [ { value: "High" } ]
- });
- });
-
- it("has nothing to write where the grouping stores no columns", () => {
- expect(removeColumns(CONFIG, "severity")).toBeUndefined();
- expect(removeColumns(undefined, "status")).toBeUndefined();
- });
-});
-
describe("adoptLegacyColumns", () => {
/** What a board grouping by something other than the default stored before this key existed. */
const legacy: BoardViewData = {
diff --git a/apps/client/src/widgets/collections/board/column_storage.ts b/apps/client/src/widgets/collections/board/column_storage.ts
index 5e14772ad0..9c6101e10f 100644
--- a/apps/client/src/widgets/collections/board/column_storage.ts
+++ b/apps/client/src/widgets/collections/board/column_storage.ts
@@ -19,26 +19,6 @@ export function writeColumns(
return { ...config, [boardColumnsKey(groupBy)]: columns };
}
-/**
- * The config without the columns of one grouping, or nothing where it stores none for it.
- *
- * Used when the attribute a grouping is made from is deleted: `syncColumnsToDefinition` would
- * otherwise write the stored columns into a new definition once the board groups by it again.
- */
-export function removeColumns(
- config: BoardViewData | undefined,
- groupBy: string
-): BoardViewData | undefined {
- const key = boardColumnsKey(groupBy);
- if (!config || !(key in config)) {
- return undefined;
- }
-
- const removed: BoardViewData = { ...config };
- delete removed[key];
- return removed;
-}
-
/**
* The config with a pre-switching `columns` list moved under the grouping it belongs to, or nothing
* where there is none to move.
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index ee0ec0794c..76e635b150 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -16,6 +16,7 @@ import contextMenu from "../../../menus/context_menu";
import dialog from "../../../services/dialog";
import server from "../../../services/server";
import toast from "../../../services/toast";
+import ws from "../../../services/ws";
import FBranch from "../../../entities/fbranch";
import froca from "../../../services/froca";
import attributes from "../../../services/attributes";
@@ -4533,6 +4534,109 @@ describe("Switchable board grouping", () => {
expect(columnTitles(mountPoint)).toEqual([ "Low", "High" ]);
expect(columnIcons(mountPoint)).toEqual([ "bx bx-down-arrow", DEFAULT_COLUMN_ICON ]);
});
+
+ /**
+ * `adoptLegacyColumns()` reads a config with `columns` and no `*ViewColumns` key as one written
+ * before the grouping could be switched. Deleting the last keyed list must not leave that shape.
+ */
+ it("keeps the default grouping's columns off a deleted grouping it was opened on", async () => {
+ delete noteAttributeCache.attributes["deleteBoard"];
+ const note = buildNote({
+ id: "deleteBoard",
+ title: "Board",
+ "#collection": "",
+ "#viewType": "board",
+ "#board:groupBy": "priority",
+ "#label:priority(inheritable)": "promoted,alias=Priority,single,select,options=High;Low",
+ children: [
+ { title: "First", "#status": "To Do", "#priority": "High" },
+ { title: "Second", "#status": "Done", "#priority": "Low" }
+ ]
+ });
+ const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);
+ // Every refresh asks the api this, which is where the test takes hold of it.
+ let api: BoardApi | undefined;
+ const hasStoredSince = BoardApi.prototype.hasStoredSince;
+ vi.spyOn(BoardApi.prototype, "hasStoredSince").mockImplementation(
+ function (this: BoardApi, config) {
+ api = this;
+ return hasStoredSince.call(this, config);
+ });
+
+ const mountPoint = document.createElement("div");
+ container = mountPoint;
+ document.body.appendChild(mountPoint);
+ const host = new Component();
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={host}>
+ <Harness
+ note={note}
+ noteIds={[ ...note.getChildNoteIds() ]}
+ initialConfig={CONFIG}
+ />
+ </ParentComponent.Provider>,
+ mountPoint
+ );
+ });
+ await act(async () => { await flush(); });
+ await act(async () => { await flush(); });
+ expect(columnTitles(mountPoint)).toEqual([ "High", "Low" ]);
+ setLabel.mockClear();
+
+ // What `PromotedAttributesCard` deletes: the definition and every card's value. The
+ // deletion reaches froca once `ws.waitForMaxKnownEntityChangeId()` is awaited, or only
+ // after the request has returned where nothing waits for it.
+ const deleted = Object.values(froca.attributes).filter(attribute =>
+ attribute.name === "label:priority" || attribute.name === "priority");
+ let deletionArrived = false;
+ async function applyDeletion() {
+ if (deletionArrived) {
+ return;
+ }
+ deletionArrived = true;
+ for (const attribute of deleted) {
+ delete froca.attributes[attribute.attributeId];
+ for (const owner of [ note, ...note.getChildNoteIds().map(id => froca.notes[id]) ]) {
+ owner.attributes = owner.attributes
+ .filter(attributeId => attributeId !== attribute.attributeId);
+ noteAttributeCache.attributes[owner.noteId] = noteAttributeCache
+ .attributes[owner.noteId]?.filter(cached => cached !== attribute) ?? [];
+ }
+ }
+ const results = new LoadResults(deleted.map(attribute => ({
+ entityName: "attributes",
+ entityId: attribute.attributeId,
+ entity: {
+ attributeId: attribute.attributeId, noteId: attribute.noteId, type: "label",
+ name: attribute.name, isDeleted: true
+ }
+ }) as never));
+ for (const attribute of deleted) {
+ results.addAttribute(attribute.attributeId, "other");
+ }
+ await host.handleEvent("entitiesReloaded", { loadResults: results });
+ }
+ vi.spyOn(ws, "waitForMaxKnownEntityChangeId").mockImplementation(applyDeletion);
+
+ await act(async () => {
+ await api?.deleteGrouping("priority", async () => {});
+ await flush();
+ });
+ await act(async () => {
+ await applyDeletion();
+ await flush();
+ });
+ await act(async () => { await flush(); });
+ await act(async () => { await flush(); });
+
+ expect(api).toBeDefined();
+ expect(saved.at(-1)?.priorityViewColumns ?? []).toEqual([]);
+ expect(saved.at(-1)?.columns).toEqual(CONFIG.columns);
+ expect(setLabel).not.toHaveBeenCalledWith(
+ "deleteBoard", "label:priority", expect.anything(), expect.anything());
+ vi.restoreAllMocks();
+ });
});
describe("Board properties from the note menu", () => {