Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board/switchable card grouping: fix some issues

Adorian Doran8e4f66972474
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index e1df1a873a..d695d3e2c9 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3519,6 +3519,7 @@
"status-alias": "Status",
"filter-placeholder": "Filter board...",
"group-by": "Group cards by",
+ "grouping-already-defined": "\"{{- name}}\" is already defined on this board. Pick it from the list to group by it.",
"card-outside-filter": "This card is outside of the current filtering scope",
"sort": "Sort",
"sort-manually": "Manually",
diff --git a/apps/client/src/widgets/collections/board/group_by.spec.tsx b/apps/client/src/widgets/collections/board/group_by.spec.tsx
index c1de241334..a08ca08e8d 100644
--- a/apps/client/src/widgets/collections/board/group_by.spec.tsx
+++ b/apps/client/src/widgets/collections/board/group_by.spec.tsx
@@ -21,6 +21,7 @@ vi.mock("../../../services/i18n", () => ({
const mocks = vi.hoisted(() => ({
setLabel: vi.fn(async () => {}),
+ showError: vi.fn(),
/** The editor the dropdown opens, which the test drives through the callbacks it was given. */
detail: { opts: null as AttributeDetailOpts | null, callbacks: {} as Record<string, Function> }
}));
@@ -29,6 +30,8 @@ vi.mock("../../../services/attributes", () => ({
default: { setLabel: mocks.setLabel, isAffecting: () => true }
}));
+vi.mock("../../../services/toast", () => ({ default: { showError: mocks.showError } }));
+
// The editor is tested where it lives; what the dropdown answers for is what it hands over and what
// it writes once the editor reports a definition.
vi.mock("../../attribute_widgets/attribute_detail", () => ({
@@ -64,7 +67,9 @@ const DEFINED = [
function board(defined = DEFINED) {
return {
noteId: "board1",
- getAttributeDefinitions: () => defined
+ getAttributeDefinitions: () => defined,
+ // Every definition here is the board's own, which is the case the write has to refuse.
+ getOwnedLabels: (name: string) => defined.filter(attribute => attribute.name === name)
} as unknown as FNote;
}
@@ -133,6 +138,7 @@ describe("BoardGroupBy", () => {
afterEach(() => {
mocks.setLabel.mockClear();
+ mocks.showError.mockClear();
mocks.detail.opts = null;
if (container) {
render(null, container);
@@ -235,6 +241,35 @@ describe("BoardGroupBy", () => {
expect(mocks.detail.opts).toBeNull();
});
+ /**
+ * `set-attribute` replaces the value of a definition the board already owns, so a name already
+ * taken would cost that grouping its alias and every column it offers.
+ */
+ it("refuses a name the board already defines, keeping the editor open", async () => {
+ const { mountPoint, onSelect } = await setup();
+
+ await act(async () => {
+ mountPoint.querySelector<HTMLElement>(".board-group-by-create")?.click();
+ });
+
+ await act(async () => {
+ mocks.detail.callbacks.onAttributesChanged([ {
+ type: "label",
+ name: "label:status",
+ value: "promoted,single,select,options=Something else",
+ isInheritable: true
+ } ]);
+ await mocks.detail.callbacks.onSaveAndClose();
+ });
+
+ expect(mocks.setLabel).not.toHaveBeenCalled();
+ expect(onSelect).not.toHaveBeenCalled();
+ expect(mocks.showError).toHaveBeenCalledWith(
+ 'board_view.grouping-already-defined:{"name":"status"}');
+ // Left standing on the name, so what was typed is not lost to a rename.
+ expect(mocks.detail.opts).not.toBeNull();
+ });
+
it("writes nothing for an editor closed without a name", async () => {
const { mountPoint, onSelect } = await setup();
diff --git a/apps/client/src/widgets/collections/board/group_by.tsx b/apps/client/src/widgets/collections/board/group_by.tsx
index 62c07b9599..8051a49768 100644
--- a/apps/client/src/widgets/collections/board/group_by.tsx
+++ b/apps/client/src/widgets/collections/board/group_by.tsx
@@ -7,6 +7,7 @@ import type FNote from "../../../entities/fnote";
import type { Attribute } from "../../../services/attribute_parser";
import attributes from "../../../services/attributes";
import { t } from "../../../services/i18n";
+import toast from "../../../services/toast";
import {
AttributeDetail, type AttributeDetailOpts
} from "../../attribute_widgets/attribute_detail";
@@ -74,13 +75,22 @@ export default function BoardGroupBy({ note, options, current, onSelect }: {
/** Writes the definition the editor was left holding, and groups by it. */
const save = useCallback(async () => {
const definition = edited.current;
- setDetail(null);
-
const [ , name ] = definition?.name.split(":", 2) ?? [];
if (!definition || !name) {
+ setDetail(null);
+ return;
+ }
+
+ // `set-attribute` replaces the value of a definition the board already owns, which would
+ // take the alias and the columns of a grouping the list above already offers. The editor
+ // stays open for the reader to rename it. Owned only: a definition the board inherits is
+ // not matched, so a definition of its own shadowing that one is safe.
+ if (note.getOwnedLabels(definition.name).length) {
+ toast.showError(t("board_view.grouping-already-defined", { name }));
return;
}
+ setDetail(null);
await attributes.setLabel(
note.noteId, definition.name, definition.value, definition.isInheritable);
onSelect(name);