Skip to content

Build your personal knowledge base with Trilium Notes

client: prevent unnecessary note reloads

Adorian Dorana8d32d6dce0e
diff --git a/apps/client/src/widgets/NoteDetail.spec.ts b/apps/client/src/widgets/NoteDetail.spec.ts
index cf7ee218ce..e5cf41140c 100644
--- a/apps/client/src/widgets/NoteDetail.spec.ts
+++ b/apps/client/src/widgets/NoteDetail.spec.ts
@@ -13,9 +13,13 @@ import froca from "../services/froca";
// global the test env doesn't provide. The pure function under test never touches the tree, so stub it.
vi.mock("./note_tree", () => ({ default: class {} }));
-import { getExtendedWidgetType, NoteDetailWrapper, useDeferredDetailFocus } from "./NoteDetail";
+import {
+ getExtendedWidgetType, NoteDetailWrapper, useDeferredDetailFocus, useNoteInfo
+} from "./NoteDetail";
import { isContextInActiveTab, useLegacyImperativeHandlers, useTriliumEvent } from "./react/hooks";
-import { ContainerVisibilityContext, ParentComponent } from "./react/react_utils";
+import {
+ ContainerVisibilityContext, NoteContextContext, ParentComponent
+} from "./react/react_utils";
import type { TypeWidgetProps } from "./type_widgets/type_widget";
const FAKE_NOTE_ID = "blob-stub-note";
@@ -149,6 +153,62 @@ describe("useDeferredDetailFocus", () => {
});
});
+describe("useNoteInfo", () => {
+ const host = document.createElement("div");
+
+ function Harness() {
+ useNoteInfo();
+ return null;
+ }
+
+ afterEach(() => {
+ render(null, host);
+ });
+
+ it("reloads on a type or MIME change of its own note only", async () => {
+ const note = {
+ noteId: "shown-note",
+ type: "text",
+ isProtected: false,
+ isTriliumSqlite: () => false,
+ isMarkdown: () => false,
+ isIconPack: () => false
+ } as unknown as FNote;
+ const setContextData = vi.fn();
+ const noteContext = {
+ note,
+ notePath: "root/shown-note",
+ hoistedNoteId: "root",
+ viewScope: {},
+ isReadOnly: async () => false,
+ setContextData
+ } as unknown as NoteContext;
+ const parentComponent = new Component();
+ await act(async () => render(
+ h(ParentComponent.Provider, { value: parentComponent },
+ h(NoteContextContext.Provider, { value: noteContext }, h(Harness, {}))),
+ host
+ ));
+ const loadStates = () => setContextData.mock.calls
+ .map(([ key, { state } ]) => `${key}:${state}`);
+ await vi.waitFor(() => expect(loadStates()).toEqual(
+ [ "contentLoad:loading", "contentLoad:loaded" ]));
+ setContextData.mockClear();
+
+ const changeNoteType = (noteId: string) => act(async () => {
+ await parentComponent.handleEventInChildren("noteTypeMimeChanged", { noteId });
+ });
+
+ // A `NoteDetail` that shows the changed note triggers the event for the whole app.
+ await changeNoteType("other-note");
+ expect(setContextData).not.toHaveBeenCalled();
+
+ await changeNoteType("shown-note");
+ await vi.waitFor(() => expect(loadStates()).toEqual(
+ [ "contentLoad:loading", "contentLoad:loaded" ]));
+ });
+});
+
describe("NoteDetailWrapper", () => {
const host = document.createElement("div");
const received: string[] = [];
diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsx
index 87a2c09409..ff952007f1 100644
--- a/apps/client/src/widgets/NoteDetail.tsx
+++ b/apps/client/src/widgets/NoteDetail.tsx
@@ -411,8 +411,12 @@ function NoteDetailLoadingOverlay({ noteContext }: { noteContext: NoteContext |
);
}
-/** Manages both note changes and changes to the widget type, which are asynchronous. */
-function useNoteInfo() {
+/**
+ * Manages both note changes and changes to the widget type, which are asynchronous.
+ *
+ * Exported for testing.
+ */
+export function useNoteInfo() {
const { note: actualNote, noteContext, parentComponent } = useNoteContext();
const [ note, setNote ] = useState<FNote | null | undefined>();
const [ type, setType ] = useState<ExtendedNoteType>();
@@ -445,7 +449,9 @@ function useNoteInfo() {
if (eventNoteContext?.ntxId !== noteContext?.ntxId) return;
refresh();
});
- useTriliumEvent("noteTypeMimeChanged", refresh);
+ useTriliumEvent("noteTypeMimeChanged", ({ noteId }) => {
+ if (noteId === actualNote?.noteId) refresh();
+ });
return { note, type, mime, noteContext, parentComponent };
}