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): address review feedback on lazy-loading effects
Elian Dorana2b762e80d57
diff --git a/apps/client/src/layouts/layout_commons.tsx b/apps/client/src/layouts/layout_commons.tsxindex be48fe5b85..b7757f47c8 100644--- a/apps/client/src/layouts/layout_commons.tsx+++ b/apps/client/src/layouts/layout_commons.tsx@@ -1,5 +1,5 @@import { ComponentType } from "preact";-import { useContext, useEffect, useState } from "preact/hooks";+import { useContext, useEffect, useRef, useState } from "preact/hooks";import type { EventData, EventNames } from "../components/app_context.js";import type RootContainer from "../widgets/containers/root_container.js";@@ -72,10 +72,14 @@ function LazyDialog({ loader, triggerEvents }: LazyDialogProps) {const parentComponent = useContext(ParentComponent);const [ Component, setComponent ] = useState<ComponentType>();const [ pendingEvent, setPendingEvent ] = useState<{ name: EventNames, data: unknown }>();+ // Guards against a second summons that arrives before the import resolves (the `Component`+ // state is still undefined then, so it cannot be used to short-circuit the load).+ const loadStarted = useRef(false);useTriliumEvents(triggerEvents, async (data, name) => {- // Once mounted, the dialog receives events directly through the shared host component.- if (Component) return;+ // Once loaded, the dialog receives events directly through the shared host component.+ if (loadStarted.current) return;+ loadStarted.current = true;const module = await loader();setComponent(() => module.default);setPendingEvent({ name, data });diff --git a/apps/client/src/widgets/react/LazyComponent.tsx b/apps/client/src/widgets/react/LazyComponent.tsxindex 0fff1ed1e8..a6c1c0c83c 100644--- a/apps/client/src/widgets/react/LazyComponent.tsx+++ b/apps/client/src/widgets/react/LazyComponent.tsx@@ -30,7 +30,10 @@ export default function LazyComponent({ loader }: LazyComponentProps) {return () => {cancelled = true;};- }, [ loader ]);+ // Loads once on mount; the loader is typically an inline arrow that changes identity each+ // render, so depending on it would re-run the import on every render.+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, []);return (<div className="lazy-component">diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsxindex e2c713c6ce..263f3daeaa 100644--- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx+++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx@@ -113,7 +113,16 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI// of the startup module graph.const [ editorClass, setEditorClass ] = useState<typeof CKEditorAttributeEditor>();useEffect(() => {- void import("@triliumnext/ckeditor5").then((ckeditor) => setEditorClass(() => ckeditor.AttributeEditor));+ let active = true;+ void import("@triliumnext/ckeditor5").then((ckeditor) => {+ // Avoid a state update if the editor unmounted before the bundle finished loading.+ if (active) {+ setEditorClass(() => ckeditor.AttributeEditor);+ }+ });+ return () => {+ active = false;+ };}, []);// Stable config so `useTooltip`'s effect doesn't dispose/recreate the tooltipdiff --git a/apps/client/src/widgets/ribbon/components/StandaloneRibbonAdapter.tsx b/apps/client/src/widgets/ribbon/components/StandaloneRibbonAdapter.tsxindex e5fed5220d..62158c3c90 100644--- a/apps/client/src/widgets/ribbon/components/StandaloneRibbonAdapter.tsx+++ b/apps/client/src/widgets/ribbon/components/StandaloneRibbonAdapter.tsx@@ -20,8 +20,17 @@ export default function StandaloneRibbonAdapter({ component, show }: StandaloneRconst [ shown, setShown ] = useState<boolean | null | undefined>(false);useEffect(() => {- void shouldShowTab(show, { note, noteContext }).then(setShown);- }, [ note ]);+ let active = true;+ void shouldShowTab(show, { note, noteContext }).then((result) => {+ // Ignore a stale resolution if the note changed while the predicate was pending.+ if (active) {+ setShown(result);+ }+ });+ return () => {+ active = false;+ };+ }, [ note, noteContext, show ]);return (<Componentdiff --git a/apps/client/src/widgets/type_widgets/options/code_mime_types_list.tsx b/apps/client/src/widgets/type_widgets/options/code_mime_types_list.tsxindex 381c9fa5d0..2f5e78c37f 100644--- a/apps/client/src/widgets/type_widgets/options/code_mime_types_list.tsx+++ b/apps/client/src/widgets/type_widgets/options/code_mime_types_list.tsx@@ -63,7 +63,9 @@ export function CodeMimeTypesList() {result[initial].push(mimeType);}return result;- }, [ codeNotesMimeTypes ]);+ // The available mime types are static; the memo does not read the user's selection+ // (`codeNotesMimeTypes`), so it must not re-group on every checkbox toggle.+ }, []);return (<ul class="options-mime-types" ref={containerRef}>