Skip to content

Build your personal knowledge base with Trilium Notes

test(client): multiple notes in split not loading on refresh

Elian Doranb76ae96b629b
diff --git a/apps/client/src/widgets/NoteDetail.spec.ts b/apps/client/src/widgets/NoteDetail.spec.ts
new file mode 100644
index 0000000000..63e6a9d295
--- /dev/null
+++ b/apps/client/src/widgets/NoteDetail.spec.ts
@@ -0,0 +1,50 @@
+import { describe, expect, it, vi } from "vitest";
+
+import NoteContext from "../components/note_context";
+
+// NoteDetail pulls in note_tree, which loads jquery.fancytree at import time and needs a `jQuery`
+// 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 { isContextInActiveTab } from "./NoteDetail";
+
+/**
+ * Builds a tab as a list of note contexts: the first is the main context (the tab itself),
+ * the rest are its splits. Every context resolves its main context to the first one — the
+ * same topology `NoteContext.getMainContext()` produces, without needing a real tab manager.
+ */
+function buildTab(ntxIds: string[]): NoteContext[] {
+ const contexts = ntxIds.map((id, i) => new NoteContext(id, "root", i === 0 ? null : ntxIds[0]));
+ const mainContext = contexts[0];
+ for (const context of contexts) {
+ context.getMainContext = () => mainContext;
+ }
+ return contexts;
+}
+
+describe("isContextInActiveTab", () => {
+ it("treats every split of the active tab as active, not just the focused one", () => {
+ const [ mainContext, secondarySplit ] = buildTab([ "A", "B" ]);
+
+ // The active tab is "A". Both the main split and the secondary (non-focused) split belong to
+ // it, so both must load eagerly. The secondary split is the regression case: keying deferral on
+ // the focused split alone (its own ntxId "B") would leave it blank until clicked.
+ expect(isContextInActiveTab(mainContext, "A")).toBe(true);
+ expect(isContextInActiveTab(secondarySplit, "A")).toBe(true);
+ });
+
+ it("defers splits that belong to a different (background) tab", () => {
+ const [ backgroundMain, backgroundSplit ] = buildTab([ "C", "D" ]);
+
+ expect(isContextInActiveTab(backgroundMain, "A")).toBe(false);
+ expect(isContextInActiveTab(backgroundSplit, "A")).toBe(false);
+ });
+
+ it("returns false when the context is missing or no tab is active yet", () => {
+ const [ , split ] = buildTab([ "A", "B" ]);
+
+ expect(isContextInActiveTab(undefined, "A")).toBe(false);
+ expect(isContextInActiveTab(split, null)).toBe(false);
+ expect(isContextInActiveTab(split, undefined)).toBe(false);
+ });
+});
diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsx
index 46b0e18781..199d1a9132 100644
--- a/apps/client/src/widgets/NoteDetail.tsx
+++ b/apps/client/src/widgets/NoteDetail.tsx
@@ -47,14 +47,16 @@ export default function NoteDetail() {
// active split would leave the non-focused split of the active tab blank until it's clicked.
// Special contexts (ntxId starting with "_", e.g. popup editor) are always considered active.
const isSpecialContext = ntxId?.startsWith("_") ?? false;
- const [ hasTabBeenActive, setHasTabBeenActive ] = useState(() => isSpecialContext || isContextInActiveTab(noteContext));
+ const isInActiveTab = () =>
+ isContextInActiveTab(noteContext, appContext.tabManager.getActiveMainContext()?.ntxId);
+ const [ hasTabBeenActive, setHasTabBeenActive ] = useState(() => isSpecialContext || isInActiveTab());
useEffect(() => {
- if (!hasTabBeenActive && isContextInActiveTab(noteContext)) {
+ if (!hasTabBeenActive && isInActiveTab()) {
setHasTabBeenActive(true);
}
- }, [ noteContext, hasTabBeenActive ]);
+ }, [ noteContext, hasTabBeenActive ]); // eslint-disable-line react-hooks/exhaustive-deps
useTriliumEvent("activeNoteChanged", () => {
- if (!hasTabBeenActive && isContextInActiveTab(noteContext)) {
+ if (!hasTabBeenActive && isInActiveTab()) {
setHasTabBeenActive(true);
}
});
@@ -237,17 +239,17 @@ export default function NoteDetail() {
}
/**
- * True when the given context belongs to the currently active tab. A tab can hold several splits, but
- * only one of them is the "active" context at a time; every split of the active tab is visible and must
- * load eagerly, so the deferral check is keyed on the tab (the main context), not the individual split.
+ * True when the given context belongs to the active tab, identified by `activeMainNtxId` (the ntxId of
+ * the active tab's main context). A tab can hold several splits, but only one of them is the "active"
+ * context at a time; every split of the active tab is visible and must load eagerly, so the deferral
+ * check is keyed on the tab (the main context), not the individual split.
*/
-function isContextInActiveTab(noteContext: NoteContext | undefined): boolean {
- if (!noteContext) {
+export function isContextInActiveTab(noteContext: NoteContext | undefined, activeMainNtxId: string | null | undefined): boolean {
+ if (!noteContext || activeMainNtxId == null) {
return false;
}
- const activeMainNtxId = appContext.tabManager.getActiveMainContext()?.ntxId;
- return activeMainNtxId != null && activeMainNtxId === noteContext.getMainContext().ntxId;
+ return activeMainNtxId === noteContext.getMainContext().ntxId;
}
function FixedTree({ noteContext }: { noteContext: NoteContext }) {