Skip to content

Build your personal knowledge base with Trilium Notes

fix(board): dim a card as soon as its note is archived

Elian Doran92a20ff19af6
diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsx
new file mode 100644
index 0000000000..613fe53929
--- /dev/null
+++ b/apps/client/src/widgets/collections/board/card.spec.tsx
@@ -0,0 +1,138 @@
+/**
+ * A card renders state that no board redraw is triggered for, so each of those has to reach the card
+ * through a subscription of its own. These check that the card actually receives them.
+ */
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, describe, expect, it } from "vitest";
+
+import Component from "../../../components/component";
+import FAttribute from "../../../entities/fattribute";
+import froca from "../../../services/froca";
+import LoadResults from "../../../services/load_results";
+import noteAttributeCache from "../../../services/note_attribute_cache";
+import utils from "../../../services/utils";
+import { buildNote } from "../../../test/easy-froca";
+import { ParentComponent } from "../../react/react_utils";
+import BoardView from ".";
+
+describe("Board card", () => {
+ let container: HTMLElement | undefined;
+ /** froca is module-level, so ids are kept distinct rather than reset between tests. */
+ let idSeed = 0;
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ it("dims a card when its note is archived, without the board being redrawn", async () => {
+ // Archiving reaches the board two ways, and only one of them fires here. With
+ // #includeArchived off the note leaves the collection's id list, which re-runs the board's
+ // refresh; with it on the list is unchanged, so nothing above the card reacts at all.
+ const { component, first, second } = await renderBoard();
+
+ expect(cardClasses(first)).not.toContain("archived");
+
+ await addLabel(component, first, "archived");
+
+ expect(cardClasses(first)).toContain("archived");
+ expect(cardClasses(second)).not.toContain("archived");
+ });
+
+ it("follows its note's icon, which the quick edit popup sets as a label", async () => {
+ const { component, first } = await renderBoard();
+
+ expect(cardIcon(first)).not.toContain("bx-bug");
+
+ await addLabel(component, first, "iconClass", "bx bx-bug");
+
+ expect(cardIcon(first)).toContain("bx-bug");
+ });
+
+ /** Renders a board of two cards and hands back the component their subscriptions register on. */
+ async function renderBoard() {
+ const first = `card${idSeed++}`;
+ const second = `card${idSeed++}`;
+ const note = buildNote({
+ title: "Board",
+ "#collection": "",
+ "#viewType": "board",
+ children: [
+ { id: first, title: "First", "#status": "To Do" },
+ { id: second, title: "Second", "#status": "To Do" }
+ ]
+ });
+
+ const component = new Component();
+ const mountPoint = document.createElement("div");
+ container = mountPoint;
+ document.body.appendChild(mountPoint);
+
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={component}>
+ <BoardView
+ note={note}
+ notePath={`root/${note.noteId}`}
+ noteIds={[ first, second ]}
+ highlightedTokens={null}
+ viewConfig={{ columns: [ { value: "To Do" } ] }}
+ saveConfig={() => {}}
+ media="screen"
+ onReady={() => {}}
+ />
+ </ParentComponent.Provider>,
+ mountPoint
+ );
+ });
+ await settle();
+
+ return { note, component, first, second };
+ }
+
+ /** Adds a label to a note already in froca and announces it the way a websocket message would. */
+ async function addLabel(component: Component, noteId: string, name: string, value = "") {
+ const attributeId = utils.randomString(12);
+ const attribute = new FAttribute(froca, {
+ noteId, attributeId, type: "label", name, value, position: 0, isInheritable: false
+ });
+
+ froca.attributes[attributeId] = attribute;
+ froca.notes[noteId].attributes.push(attributeId);
+ noteAttributeCache.attributes[noteId] = [ ...(noteAttributeCache.attributes[noteId] ?? []), attribute ];
+
+ const entity = { attributeId, noteId, type: "label", name, value, isDeleted: false };
+ const loadResults = new LoadResults([ {
+ entityName: "attributes", entityId: attributeId, entity, hash: "", isSynced: true, isErased: false
+ } ]);
+ loadResults.addAttribute(attributeId, "someOtherComponent");
+
+ await act(async () => {
+ await component.handleEvent("entitiesReloaded", { loadResults });
+ });
+ await settle();
+ }
+
+ function card(noteId: string) {
+ const title = froca.notes[noteId]?.title;
+ const element = [ ...(container?.querySelectorAll(".board-note") ?? []) ]
+ .find((el) => el.querySelector(".title")?.textContent?.includes(title));
+ if (!element) throw new Error(`no card rendered for ${noteId} ("${title}")`);
+
+ return element;
+ }
+
+ const cardClasses = (noteId: string) => card(noteId).className;
+ const cardIcon = (noteId: string) => card(noteId).querySelector(".title .icon")?.className ?? "";
+});
+
+/** Drains the async chain inside the board's `refresh()` plus any re-render it queues. */
+async function settle() {
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve));
+ });
+}
diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsx
index 655b9d2e8a..63caacff78 100644
--- a/apps/client/src/widgets/collections/board/card.tsx
+++ b/apps/client/src/widgets/collections/board/card.tsx
@@ -8,7 +8,7 @@ import { ContextMenuEvent } from "../../../menus/context_menu";
import { openNoteContextMenu } from "./context_menu";
import { t } from "../../../services/i18n";
import UserAttributesDisplay from "../../attribute_widgets/UserAttributesList";
-import { useNoteIcon, useTriliumEvent } from "../../react/hooks";
+import { useNoteIcon, useNoteLabelBoolean, useTriliumEvent } from "../../react/hooks";
export const CARD_CLIPBOARD_TYPE = "trilium/board-card";
@@ -43,7 +43,7 @@ function Card({
const { setBranchIdToEdit, setDraggedCard } = useContext(BoardActionsContext);
const colorClass = note.getColorClass() || '';
const editorRef = useRef<HTMLInputElement>(null);
- const isArchived = note.isArchived;
+ const [ isArchived ] = useNoteLabelBoolean(note, "archived");
const [ isVisible, setVisible ] = useState(true);
const [ title, setTitle ] = useState(note.title);
// Tracks the `iconClass` label, which an attribute change carries and the note row never does.