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
fix(client): don't latch the note list visible while the content is loading
Claude4fb03264ad2d
diff --git a/apps/client/src/widgets/collections/NoteList.spec.tsx b/apps/client/src/widgets/collections/NoteList.spec.tsxindex 8dd5bc5605..deaac1d631 100644--- a/apps/client/src/widgets/collections/NoteList.spec.tsx+++ b/apps/client/src/widgets/collections/NoteList.spec.tsx@@ -273,4 +273,54 @@ describe("CustomNoteList visibility latch", () => {expect(observer.disconnect).toHaveBeenCalled();expect(mountPoint.querySelector(".note-list-widget-content")).not.toBeNull();});++ it("waits for the note content before observing, and drops the latch when a new load starts", async () => {+ const note = buildNote({ title: "Parent", type: "text" });+ note.getChildNoteIdsWithArchiveFiltering = vi.fn(async () => [ "child-a" ]);+ note.getAttachmentsByRole = vi.fn(async () => []);++ const parent = new Component();+ const mountPoint = document.createElement("div");+ container = mountPoint;+ document.body.appendChild(mountPoint);++ const renderList = async (contentReady: boolean) => {+ await act(async () => {+ render(+ <ParentComponent.Provider value={parent}>+ <CustomNoteList+ note={note}+ viewType="grid"+ isEnabled={true}+ contentReady={contentReady}+ notePath={`root/${note.noteId}`}+ ntxId="ntx-1"+ media="screen"+ />+ </ParentComponent.Provider>,+ mountPoint+ );+ });+ // Let the 10ms observe delay (Firefox race workaround) elapse.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 25));+ });+ };++ // While the content is loading the widget still sits inside the viewport, so observing+ // now would latch it visible on effectively every note.+ await renderList(false);+ expect(MockIntersectionObserver.instances.every((o) => o.observe.mock.calls.length === 0)).toBe(true);++ // Content loaded: observe, and render once actually visible.+ await renderList(true);+ const observer = MockIntersectionObserver.instances.find((o) => o.observe.mock.calls.length > 0);+ if (!observer) throw new Error("expected the note list to observe once the content loaded");+ await act(async () => observer.fire(true));+ expect(mountPoint.querySelector(".note-list-widget-content")).not.toBeNull();++ // A new load (e.g. a note switch) resets the latch instead of keeping the list mounted.+ await renderList(false);+ expect(mountPoint.querySelector(".note-list-widget-content")).toBeNull();+ });});diff --git a/apps/client/src/widgets/collections/NoteList.tsx b/apps/client/src/widgets/collections/NoteList.tsxindex dab80c2086..5de699b83e 100644--- a/apps/client/src/widgets/collections/NoteList.tsx+++ b/apps/client/src/widgets/collections/NoteList.tsx@@ -9,7 +9,7 @@ import FNote from "../../entities/fnote";import type { PrintReport } from "../../print";import froca from "../../services/froca";import { subscribeToMessages, unsubscribeToMessage as unsubscribeFromMessage } from "../../services/ws";-import { useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteProperty, useTriliumEvent } from "../react/hooks";+import { useGetContextDataFrom, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteProperty, useTriliumEvent } from "../react/hooks";import { allViewTypes, ViewModeMedia, ViewModeProps, ViewTypeOptions } from "./interface";import ViewModeStorage, { type ViewModeStorageType } from "./view_mode_storage";@@ -20,6 +20,12 @@ interface NoteListProps {/** if set to `true` then only collection-type views are displayed such as geo-map and the calendar. The original book types grid and list will be ignored. */displayOnlyCollections?: boolean;isEnabled: boolean;+ /**+ * When false, the note's content is still being loaded and the legacy list/grid views hold off+ * their visibility check: without content the widget sits inside the viewport, so observing it+ * now would latch it visible on effectively every note. Defaults to true.+ */+ contentReady?: boolean;ntxId: string | null | undefined;media: ViewModeMedia;viewType: ViewTypeOptions | undefined;@@ -62,11 +68,13 @@ export default function NoteList(props: Pick<NoteListProps, "displayOnlyCollecticonst { note, noteContext, notePath, ntxId, viewScope } = useNoteContext();const viewType = useNoteViewType(note);const noteType = useNoteProperty(note, "type");+ const contentLoad = useGetContextDataFrom(noteContext, "contentLoad");const [ enabled, setEnabled ] = useState(noteContext?.hasNoteList());useEffect(() => {setEnabled(noteContext?.hasNoteList());}, [ note, noteContext, viewType, viewScope?.viewMode, noteType ]);- return <CustomNoteList viewType={viewType} note={note} isEnabled={!!enabled} notePath={notePath} ntxId={ntxId} {...props} />;+ return <CustomNoteList viewType={viewType} note={note} isEnabled={!!enabled} notePath={notePath} ntxId={ntxId}+ contentReady={contentLoad?.state !== "loading"} {...props} />;}export function SearchNoteList(props: Omit<NoteListProps, "isEnabled" | "viewType">) {@@ -83,7 +91,7 @@ export function EmbeddedNoteList(props: Omit<NoteListProps, "isEnabled" | "viewTreturn <CustomNoteList {...props} isEnabled={true} viewType={viewType} />;}-export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, notePath, highlightedTokens, displayOnlyCollections, ntxId, onReady, onProgressChanged, ...restProps }: NoteListProps) {+export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, contentReady = true, notePath, highlightedTokens, displayOnlyCollections, ntxId, onReady, onProgressChanged, ...restProps }: NoteListProps) {const widgetRef = useRef<HTMLDivElement>(null);const noteIds = useNoteIds(shouldEnable ? note : null, viewType, ntxId);const isFullHeight = (viewType && viewType !== "list" && viewType !== "grid");@@ -97,6 +105,13 @@ export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, notePareturn;}+ if (!contentReady) {+ // Drop any previous latch and wait for the load to finish before observing — a note+ // switch reports "loading" again, so this also resets the latch across note switches.+ setIsIntersecting(false);+ return;+ }+const observer = new IntersectionObserver((entries) => {// The first callback fires immediately after observe() and reports false while the@@ -117,7 +132,7 @@ export function CustomNoteList({ note, viewType, isEnabled: shouldEnable, notePa// (intersection is false). https://github.com/zadam/trilium/issues/4165setTimeout(() => widgetRef.current && observer.observe(widgetRef.current), 10);return () => observer.disconnect();- }, [ widgetRef, isFullHeight, displayOnlyCollections, note ]);+ }, [ widgetRef, isFullHeight, displayOnlyCollections, note, contentReady ]);// Preload the configuration.let props: ViewModeProps<any> | undefined | null = null;