Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix some issues

Adorian Doran10fee5bfb483
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 054b1cd01f..f0e5a074df 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -10,10 +10,10 @@ import dialog from "../../../services/dialog";
import froca from "../../../services/froca";
import { t } from "../../../services/i18n";
import note_create from "../../../services/note_create";
+import { type NoteTypeOption, resolveNoteTypeOptions } from "../../../services/note_types";
import server from "../../../services/server";
import ws from "../../../services/ws";
import toast from "../../../services/toast";
-import { type NoteTypeOption, resolveNoteTypeOptions } from "../../../services/note_types";
import { BoardColumnData, BoardViewData } from ".";
import { currentCardTemplate, DEFAULT_CARD_TEMPLATES } from "./card_templates";
import {
diff --git a/apps/client/src/widgets/collections/board/card_template_pill.tsx b/apps/client/src/widgets/collections/board/card_template_pill.tsx
index f42ce21c77..914a0e2587 100644
--- a/apps/client/src/widgets/collections/board/card_template_pill.tsx
+++ b/apps/client/src/widgets/collections/board/card_template_pill.tsx
@@ -1,10 +1,10 @@
import { useMemo, useState } from "preact/hooks";
import { t } from "../../../services/i18n";
+import { type NoteTypeOption } from "../../../services/note_types";
import Dropdown from "../../react/Dropdown";
import { FormListItem } from "../../react/FormList";
import Icon from "../../react/Icon";
-import { type NoteTypeOption } from "../../../services/note_types";
/**
* What a new card will be made from, standing inside the editor it is named in.
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 6a392f303d..deababe892 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -31,10 +31,10 @@ const FOOTER_QUIET_MS = 5000;
const EXPAND_MS = 200;
import NoteLink from "../../react/NoteLink";
import { BoardActionsContext, BoardDragStateContext, TitleEditor } from ".";
-import CardTemplatePill from "./card_template_pill";
-import { type CardTemplates } from "./card_templates";
import BoardApi from "./api";
import Card from "./card";
+import CardTemplatePill from "./card_template_pill";
+import { type CardTemplates } from "./card_templates";
import { DEFAULT_CARD_ICON, DEFAULT_COLUMN_ICON, INBOX_COLUMN } from "./columns";
import { openColumnContextMenu, openCreateCardMenu } from "./context_menu";
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 11ed8a17ae..b927e73688 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -17,6 +17,7 @@ import FBranch from "../../../entities/fbranch";
import froca from "../../../services/froca";
import attributes from "../../../services/attributes";
import { executeBulkActions } from "../../../services/bulk_action";
+import LoadResults from "../../../services/load_results";
import { buildNote } from "../../../test/easy-froca";
import { ParentComponent } from "../../react/react_utils";
import BoardView, { BoardViewData } from ".";
@@ -42,13 +43,19 @@ vi.mock("../../../services/i18n", () => ({
// What a card can be made from. The listing itself is the app's and is tested there; the board is
// handed a short list of it, the four it offers by default and one it does not.
vi.mock("../../../services/note_types", () => ({
- getNoteTypeOptions: async () => NOTE_TYPE_OPTIONS,
+ getNoteTypeOptions: async () => {
+ templateReads++;
+ return NOTE_TYPE_OPTIONS;
+ },
resolveNoteTypeOptions: (ids: string[], available: { id: string }[]) =>
ids.flatMap(id => available.filter(option => option.id === id)),
noteTypeOptionGroupTitle: (group: string) => `group:${group}`,
default: {}
}));
+/** How many times the board has asked what a card can be made from. */
+let templateReads = 0;
+
const NOTE_TYPE_OPTIONS = [
[ "type:text:text/html", "Text", "text", "text/html" ],
[ "type:code:text/x-markdown", "Markdown", "code", "text/x-markdown" ],
@@ -972,9 +979,10 @@ describe("Board column rename", () => {
container = mountPoint;
document.body.appendChild(mountPoint);
+ const host = new Component();
await act(async () => {
render(
- <ParentComponent.Provider value={new Component()}>
+ <ParentComponent.Provider value={host}>
<Harness
note={note}
noteIds={[ ...note.getChildNoteIds() ]}
@@ -986,7 +994,8 @@ describe("Board column rename", () => {
});
await act(async () => { await flush(); });
- return { note, container: mountPoint };
+ // The host is what the board's own event handlers hang off, for a test that sends one.
+ return { note, container: mountPoint, host };
}
/**
@@ -2300,6 +2309,40 @@ describe("Board column rename", () => {
expect(document.activeElement).toBe(editor);
});
+ /**
+ * A board outlives the templates it read: one made while it stands would never be offered, and
+ * one deleted would go on being offered. A template is a note carrying `#template`, so a change
+ * to one of those attributes is what sends the board back for the list.
+ */
+ it("reads the templates again when one is made or taken away", async () => {
+ const { host } = await setup();
+ const before = templateReads;
+
+ /** An attribute change as the server reports one: the row is tracked as well as carried. */
+ const attributeChanged = (name: string) => {
+ const results = new LoadResults([ {
+ entityName: "attributes",
+ entityId: "attr1",
+ entity: { attributeId: "attr1", noteId: "someNote", type: "label", name }
+ } as never ]);
+ results.addAttribute("attr1", "other");
+ return results;
+ };
+
+ // A change to something else leaves the list where it is.
+ await act(async () => {
+ await host.handleEvent("entitiesReloaded", { loadResults: attributeChanged("status") });
+ await flush();
+ });
+ expect(templateReads).toBe(before);
+
+ await act(async () => {
+ await host.handleEvent("entitiesReloaded", { loadResults: attributeChanged("template") });
+ await flush();
+ });
+ expect(templateReads).toBe(before + 1);
+ });
+
/** The pill's last entry opens the dialog that decides what the board offers. */
it("opens the templates dialog from the pill, and stores what is ticked", async () => {
const { container } = await setup();
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 16470aa62b..3d2a4fe51e 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -17,11 +17,10 @@ import { isIMEComposing } from "../../../services/shortcuts";
import type { ShortcutHintDefinition } from "../../../services/shortcut_hints";
import toast from "../../../services/toast";
import { escapeHtml, isMobile } from "../../../services/utils";
-import CollectionProperties from "../../note_bars/CollectionProperties";
import {
getNoteTypeOptions, type NoteTypeOption, resolveNoteTypeOptions
} from "../../../services/note_types";
-import NoteTypeSelectorDialog from "../../react/NoteTypeSelectorDialog";
+import CollectionProperties from "../../note_bars/CollectionProperties";
import FormTextArea from "../../react/FormTextArea";
import FormTextBox from "../../react/FormTextBox";
import {
@@ -30,6 +29,7 @@ import {
} from "../../react/hooks";
import Icon from "../../react/Icon";
import NoteAutocomplete from "../../react/NoteAutocomplete";
+import NoteTypeSelectorDialog from "../../react/NoteTypeSelectorDialog";
import ShortcutHintButton from "../../shortcut_hints/shortcut_hint_button";
import { onWheelHorizontalScroll } from "../../widget_utils";
import ActionButton from "../../react/ActionButton";
@@ -271,7 +271,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
const [ availableTemplates, setAvailableTemplates ] = useState<NoteTypeOption[]>([]);
const [ isPickingTemplates, setIsPickingTemplates ] = useState(false);
- useEffect(() => {
+ const readTemplates = useCallback(() => {
let listening = true;
getNoteTypeOptions().then((templates) => {
if (listening) {
@@ -281,6 +281,23 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
return () => { listening = false; };
}, []);
+
+ useEffect(readTemplates, [ readTemplates ]);
+
+ // A board outlives the templates it read: one made while it stands would never be offered, and
+ // one deleted would go on being offered until the board was drawn afresh. A template is a note
+ // carrying `#template`, and its title and icon are what the picker shows.
+ useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
+ const templated = loadResults.getAttributeRows()
+ .some((attribute) => attribute.name === "template");
+ const renamed = availableTemplates.some((option) =>
+ option.options.templateNoteId
+ && loadResults.isNoteReloaded(option.options.templateNoteId));
+
+ if (templated || renamed) {
+ readTemplates();
+ }
+ });
const selectColumn = useCallback<Dispatch<StateUpdater<string | undefined>>>((column) => {
setIsPeekingAll(false);
setActiveColumn(column);