Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(board): stop redrawing the board for a note's content
Elian Doranf3c3f0ec2df6
diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsxindex 7c0e38abed..efaa6f8937 100644--- a/apps/client/src/widgets/collections/board/card.tsx+++ b/apps/client/src/widgets/collections/board/card.tsx@@ -9,7 +9,7 @@ import { openNoteContextMenu } from "./context_menu";import { perfCount } from "../../../services/debug_perf";import { t } from "../../../services/i18n";import UserAttributesDisplay from "../../attribute_widgets/UserAttributesList";-import { useTriliumEvent } from "../../react/hooks";+import { useNoteIcon, useTriliumEvent } from "../../react/hooks";export const CARD_CLIPBOARD_TYPE = "trilium/board-card";@@ -49,7 +49,11 @@ function Card({const isArchived = note.isArchived;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.+ const icon = useNoteIcon(note);+ // A card owns its own title: the board does not redraw for a note-row change. Setting the value+ // already held is a no-op, so a save that left the title alone re-renders nothing.useTriliumEvent("entitiesReloaded", ({ loadResults }) => {const row = loadResults.getEntityRow("notes", note.noteId);if (row) {@@ -118,7 +122,7 @@ function Card({{!isEditing ? (<><span className="title">- <span class={`icon ${note.getIcon()}`} />+ <span class={`icon ${icon}`} />{title}</span><spandiff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex b89c45bfa0..734bb74522 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -141,8 +141,6 @@ export default function Column({draggedCard?.noteId !== note.noteId;return (- // Keyed on the fragment, since the fragment is what this map contributes to- // the child array; on the <Card> inside it the key identifies nothing.<Fragment key={note.noteId}>{showIndicatorBefore && (<div className="board-drop-placeholder show" />diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex f2ba2da6ec..ee1525efff 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -285,22 +285,21 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC/*** Names the first change in `loadResults` that the board has to redraw for, or null if none does.*- * The checks are the same ones, in the same short-circuit order, that used to be `||`-chained- * inline; naming the winner is what lets the profiler attribute a redraw to a cause instead of- * reporting only that one happened.+ * A plain note-row change is deliberately not one of them. `getNoteIds()` reports every note in the+ * change set whatever changed about it, so it cannot distinguish a card's title from its content,+ * which no card displays. Cards keep their own title and icon in step instead, and nothing else the+ * board derives comes off the note row: membership is branches, grouping is the status attribute,+ * and `#archived` is a label.+ *+ * Naming the winning check, rather than returning a boolean, is what lets the profiler attribute a+ * redraw to a cause.*/-function findRefreshReason(loadResults: LoadResults, statusAttribute: string, noteIds: string[], parentNoteId: string): string | null {+export function findRefreshReason(loadResults: LoadResults, statusAttribute: string, noteIds: string[], parentNoteId: string): string | null {// A card moved between columns.if (loadResults.getAttributeRows().some(attr => attr.name === statusAttribute && noteIds.includes(attr.noteId ?? ""))) {return "status-attribute";}- // Intended as "a card's title changed", but getNoteIds() reports every note in the change set,- // so a plain content save of a card lands here too.- if (loadResults.getNoteIds().some(noteId => noteIds.includes(noteId))) {- return "note-row";- }-// Subchildren moved, added or removed.if (loadResults.getBranchRows().some(branch => noteIds.includes(branch.noteId ?? ""))) {return "branch";diff --git a/apps/client/src/widgets/collections/board/refresh_reason.spec.ts b/apps/client/src/widgets/collections/board/refresh_reason.spec.tsnew file mode 100644index 0000000000..a39f017ec3--- /dev/null+++ b/apps/client/src/widgets/collections/board/refresh_reason.spec.ts@@ -0,0 +1,91 @@+import { describe, expect, it } from "vitest";++import LoadResults from "../../../services/load_results";+import type { EntityChange, EntityType } from "../../../server_types";+import { findRefreshReason } from ".";++const BOARD_ID = "boardNote";+const STATUS = "status";+/** The notes the board has cards for. */+const CARDS = [ "cardA", "cardB" ];+const COMPONENT_ID = "componentId";++describe("findRefreshReason", () => {+ it("ignores a note-row change, which is what an autosave of a card's content produces", () => {+ // The regression this exists for: getNoteIds() reports every note in the change set whatever+ // changed about it, so typing in a card rebuilt the whole board on every autosave. Nothing a+ // card renders comes off the note row -- a card keeps its own title and icon in step.+ expect(reasonFor({ notes: [ "cardA" ] })).toBeNull();+ });++ it("redraws when a card's status attribute changes, which is a card moving column", () => {+ expect(reasonFor({ attributes: [ [ STATUS, "cardA" ] ] })).toBe("status-attribute");+ });++ it("redraws for a branch change, which is how cards are added, removed and reordered", () => {+ expect(reasonFor({ branches: [ "cardB" ] })).toBe("branch");+ });++ it("redraws for an icon or colour change on a card", () => {+ expect(reasonFor({ attributes: [ [ "iconClass", "cardA" ] ] })).toBe("icon-or-color");+ expect(reasonFor({ attributes: [ [ "color", "cardA" ] ] })).toBe("icon-or-color");+ });++ it("redraws when the board's own grouping changes", () => {+ expect(reasonFor({ attributes: [ [ "board:groupBy", BOARD_ID ] ] })).toBe("group-by");+ });++ it("ignores changes to notes and branches outside the board", () => {+ expect(reasonFor({+ notes: [ "strangerNote" ],+ attributes: [ [ STATUS, "strangerNote" ] ],+ branches: [ "strangerNote" ]+ })).toBeNull();+ });++ it("ignores a groupBy change on some other note, which is another board's business", () => {+ expect(reasonFor({ attributes: [ [ "board:groupBy", "otherBoard" ] ] })).toBeNull();+ });++ it("reports the status attribute ahead of a branch when a move produces both", () => {+ // A card dragged to another column writes its status and may reorder its branch. The winner+ // only decides what gets logged, so this pins the documented order rather than a behaviour.+ expect(reasonFor({ attributes: [ [ STATUS, "cardA" ] ], branches: [ "cardA" ] }))+ .toBe("status-attribute");+ });+});++/**+ * Builds the `LoadResults` a websocket message would produce and asks it for a reason.+ *+ * Attributes and branches have to be registered twice over, because `getAttributeRows()` and+ * `getBranchRows()` merge a tracked row with the entity behind it and drop rows missing either half.+ * The entity goes in through the constructor, the row through `add*()`.+ */+function reasonFor({ notes = [], attributes = [], branches = [] }: {+ notes?: string[];+ /** `[ name, noteId ]` pairs. */+ attributes?: [ string, string ][];+ /** The note each changed branch points at. */+ branches?: string[];+}) {+ const changes: EntityChange[] = [+ ...attributes.map(([ name, noteId ], index) => entityChange("attributes", `attr${index}`, {+ attributeId: `attr${index}`, name, noteId+ })),+ ...branches.map((noteId, index) => entityChange("branches", `branch${index}`, {+ branchId: `branch${index}`, noteId+ }))+ ];++ const results = new LoadResults(changes);+ notes.forEach((noteId) => results.addNote(noteId, COMPONENT_ID));+ attributes.forEach((_, index) => results.addAttribute(`attr${index}`, COMPONENT_ID));+ branches.forEach((_, index) => results.addBranch(`branch${index}`, COMPONENT_ID));++ return findRefreshReason(results, STATUS, CARDS, BOARD_ID);+}++function entityChange(entityName: EntityType, entityId: string, entity: object): EntityChange {+ return { entityName, entityId, entity, hash: "", isSynced: true, isErased: false };+}