Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix ghost cells after delete/rename when multiple instances of the board are open in the same time

Adorian Doranb446fe45190c
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index 2ae3927460..03bc972deb 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -12,7 +12,7 @@ import noteAttributeCache from "../../../services/note_attribute_cache";
import server from "../../../services/server";
import { buildNote } from "../../../test/easy-froca";
import { BoardViewData } from ".";
-import BoardApi, { PendingColumnWrites } from "./api";
+import BoardApi, { getPendingWrites, PendingColumnWrites, releasePendingWrites } from "./api";
import { ColumnMap } from "./data";
import { BOARD_TEMPLATE_ID, getStatusDefinition } from "./columns";
@@ -861,3 +861,30 @@ function buildBoard(
return board;
}
+
+describe("pending writes shared between the views of a board", () => {
+ /**
+ * Two tabs on one board read the same notes, definition and attachment, so a write in flight on
+ * one has to be a write in flight on the other. A view with a record of its own would resolve a
+ * column being deleted elsewhere out of the sources that still carry it, and write it back.
+ */
+ it("hands every view of one board the same record, and another board its own", () => {
+ const writes = getPendingWrites("board1|status");
+
+ expect(getPendingWrites("board1|status")).toBe(writes);
+ expect(getPendingWrites("board1|priority")).not.toBe(writes);
+ expect(getPendingWrites("board2|status")).not.toBe(writes);
+ });
+
+ it("keeps a board's record while a write is on it, and forgets it once none is", () => {
+ const writes = getPendingWrites("board3|status");
+ writes.renames.set("Done", undefined);
+
+ releasePendingWrites("board3|status");
+ expect(getPendingWrites("board3|status")).toBe(writes);
+
+ writes.renames.delete("Done");
+ releasePendingWrites("board3|status");
+ expect(getPendingWrites("board3|status")).not.toBe(writes);
+ });
+});
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 0f0a0eb3b0..9ac82cd57b 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -46,6 +46,41 @@ export function settleColumn(pending: PendingColumnWrites, column: string) {
pending.claims.delete(column);
}
+/**
+ * The writes in flight on each board, by the note it is and the label it groups by.
+ *
+ * Shared by every view of one board rather than held per view, because what a record stands in for
+ * is shared too: the notes, the definition and the attachment. A second tab open on the same board
+ * refreshes on the same entity changes, and one that knew nothing of a deletion under way would
+ * resolve the column from whichever source still carries it and write it back.
+ */
+const pendingWritesByBoard = new Map<string, PendingColumnWrites>();
+
+/** The record every view of one board writes into, made on first use. */
+export function getPendingWrites(board: string) {
+ const existing = pendingWritesByBoard.get(board);
+ if (existing) {
+ return existing;
+ }
+
+ const writes: PendingColumnWrites = { renames: new Map(), claims: new Map() };
+ pendingWritesByBoard.set(board, writes);
+ return writes;
+}
+
+/**
+ * Forgets a board no write is left on, an empty record saying the same as none at all.
+ *
+ * Kept while anything is still claimed: a write in flight holds the record it made itself in, and
+ * one dropped here would leave its undo writing into an object nothing reads.
+ */
+export function releasePendingWrites(board: string) {
+ const writes = pendingWritesByBoard.get(board);
+ if (writes && !writes.renames.size && !writes.claims.size) {
+ pendingWritesByBoard.delete(board);
+ }
+}
+
export default class BoardApi {
private isRelationMode: boolean;
diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts
index 582e8d3e23..113dcd64c8 100644
--- a/apps/client/src/widgets/collections/board/context_menu.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.ts
@@ -8,6 +8,7 @@ import branches from "../../../services/branches";
import dialog from "../../../services/dialog";
import { getArchiveMenuItem } from "../../../menus/context_menu_utils";
import { t } from "../../../services/i18n";
+import toast from "../../../services/toast";
import ColorPicker from "../../react/ColorPicker";
import Api from "./api";
@@ -88,7 +89,12 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:
return;
}
- await api.removeColumn(column.value);
+ try {
+ await api.removeColumn(column.value);
+ } catch (e) {
+ console.error("Failed to delete the board column:", e);
+ toast.showError(t("board_view.save-error"));
+ }
}
},
{ kind: "separator" },
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 74456d9de9..76c07f2859 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -18,6 +18,7 @@ import { executeBulkActions } from "../../../services/bulk_action";
import { buildNote } from "../../../test/easy-froca";
import { ParentComponent } from "../../react/react_utils";
import BoardView, { BoardViewData } from ".";
+import { getPendingWrites } from "./api";
import { DEFAULT_COLUMN_ICON } from "./columns";
// Stands in for the server: by the time the bulk action resolves, the notes carry the new value,
@@ -245,6 +246,37 @@ describe("Board column rename", () => {
return { note, container: mountPoint };
}
+ /**
+ * What a second tab on the same board sees. The record is shared, so the column being
+ * deleted is resolved away here too; persisting that answer would put a deletion on disk
+ * the notes have not given yet, and the tab making the write persists it once it lands.
+ */
+ it("leaves the stored columns alone while a write on the board is still settling", async () => {
+ const { note } = await setup();
+ saved.length = 0;
+
+ getPendingWrites(`${note.noteId}|status`).renames.set("Done", undefined);
+
+ const second = document.createElement("div");
+ document.body.appendChild(second);
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={new Component()}>
+ <Harness
+ note={note}
+ noteIds={[ ...note.getChildNoteIds() ]}
+ initialConfig={DEFAULT_CONFIG}
+ />
+ </ParentComponent.Provider>,
+ second
+ );
+ });
+ await act(async () => { await flush(); });
+
+ expect(saved).toEqual([]);
+ second.remove();
+ });
+
/** Renames the middle column, so a slot that is not the last one has to survive. */
async function renameSecondColumn(container: HTMLElement, newName: string) {
return renameColumnAt(container, 1, newName);
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index fd70b80596..41e7dd0eed 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -16,7 +16,9 @@ import Icon from "../../react/Icon";
import NoteAutocomplete from "../../react/NoteAutocomplete";
import { onWheelHorizontalScroll } from "../../widget_utils";
import { ViewModeProps } from "../interface";
-import Api, { PendingColumnWrites, settleColumn } from "./api";
+import Api, {
+ getPendingWrites, PendingColumnWrites, releasePendingWrites, settleColumn
+} from "./api";
import BoardApi from "./api";
import { DEFAULT_GROUP_BY, getStatusDefinition } from "./columns";
import Column from "./column";
@@ -128,15 +130,15 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
const refreshSeqRef = useRef(0);
// A pending rename belongs to the board and the grouping it was made on, and `NoteList` renders
- // the view unkeyed, so moving to another board reuses this instance. The map is replaced rather
- // than emptied: a write still in flight holds the map it recorded itself in, and undoing into
- // one nobody reads is what keeps it off a board that has since recorded the very same rename.
- // Done while rendering, so the `api` built below is handed the map the refresh will read.
+ // the view unkeyed, so moving to another board reuses this instance. Looked up rather than made
+ // here, so that another view of the same board reads the same record; moving to another board
+ // takes up that board's own, leaving a write still in flight to undo into the one it recorded
+ // itself in. Done while rendering, so the `api` below is handed the map the refresh reads.
const boardIdentity = `${parentNote.noteId}|${statusAttributeWithPrefix}`;
if (pendingRenamesRef.current.board !== boardIdentity) {
pendingRenamesRef.current = {
board: boardIdentity,
- writes: { renames: new Map(), claims: new Map() }
+ writes: getPendingWrites(boardIdentity)
};
refreshSeqRef.current++;
}
@@ -201,11 +203,19 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
for (const settled of settledRenames) {
settleColumn(pendingRenamesRef.current.writes, settled);
}
+ releasePendingWrites(pendingRenamesRef.current.board);
setByColumn(byColumn);
setIsRelationMode(isInRelationMode);
setColumns(columns);
+ // A column a write is still settling has already been taken out of `columns`, and
+ // the two writes below would put that answer on disk before the notes have given
+ // it. The write that made the record persists the outcome itself once it lands.
+ if (pendingRenamesRef.current.writes.renames.size) {
+ return;
+ }
+
if (newPersistedData) {
viewConfig = { ...newPersistedData };
saveConfig(newPersistedData);