Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: improve performance

Adorian Doranf5b7f1f7efae
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsx
index aa9d4a6bda..0e715258fb 100644
--- a/apps/client/src/widgets/react/hooks.spec.tsx
+++ b/apps/client/src/widgets/react/hooks.spec.tsx
@@ -4,7 +4,8 @@ import { useRef } from "preact/hooks";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { type DelayedVisibilityPhase, useDelayedVisibility, useImperativeSearchHighlighlighting, useStaticTooltip, useTooltip } from "./hooks";
+import { buildNote } from "../../test/easy-froca";
+import { type DelayedVisibilityPhase, useDelayedVisibility, useImperativeSearchHighlighlighting, useNoteLabelBoolean, useStaticTooltip, useTooltip } from "./hooks";
/**
* mark.js delegating to the real implementation, so marking still works, while recording the calls
@@ -467,3 +468,38 @@ describe("useImperativeSearchHighlighlighting", () => {
target.remove();
});
});
+
+describe("useNoteLabelBoolean", () => {
+ let container: HTMLElement | undefined;
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ /**
+ * The render that mounts a consumer has already read the label, so forcing another one after it
+ * draws everything twice for a value that has not changed. A board draws this once a card.
+ */
+ it("draws a consumer once for a label that has not changed", async () => {
+ const note = buildNote({ title: "Card", "#archived": "true" });
+ const draws: boolean[] = [];
+
+ function Consumer() {
+ const [ archived ] = useNoteLabelBoolean(note, "archived");
+ draws.push(archived);
+ return <span>{String(archived)}</span>;
+ }
+
+ container = document.body.appendChild(document.createElement("div"));
+ await act(async () => {
+ render(<Consumer />, container as HTMLElement);
+ await new Promise((resolve) => setTimeout(resolve));
+ });
+
+ expect(draws).toEqual([ true ]);
+ });
+});
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 6c56113e4c..cd180aecb0 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -738,8 +738,15 @@ export function useNoteLabelWithDefault(note: FNote | undefined | null, labelNam
export function useNoteLabelBoolean(note: FNote | undefined | null, labelName: FilterLabelsByType<boolean>): [ boolean, (newValue: boolean) => void] {
const [, forceRender] = useState({});
+ // Not on the first run: the render that mounted the component has already read the label, so
+ // forcing another draws every consumer twice for a value that has not changed. 72 components
+ // use this hook, and a board draws it once a card.
+ const seenNote = useRef<FNote | undefined | null>(undefined);
useEffect(() => {
- forceRender({});
+ if (seenNote.current !== undefined) {
+ forceRender({});
+ }
+ seenNote.current = note;
}, [ note ]);
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {