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
perf(client): lazy-load bulk actions dialog via LazyDialog wrapper
Elian Doranffac1966c7f8
diff --git a/apps/client/src/layouts/layout_commons.tsx b/apps/client/src/layouts/layout_commons.tsxindex 212d6ffc68..1c275a4fb5 100644--- a/apps/client/src/layouts/layout_commons.tsx+++ b/apps/client/src/layouts/layout_commons.tsx@@ -1,8 +1,11 @@+import { ComponentType } from "preact";+import { useContext, useEffect, useState } from "preact/hooks";++import type { EventData, EventNames } from "../components/app_context.js";import type RootContainer from "../widgets/containers/root_container.js";import AboutDialog from "../widgets/dialogs/about.js";import AddLinkDialog from "../widgets/dialogs/add_link.js";import BranchPrefixDialog from "../widgets/dialogs/branch_prefix.js";-import BulkActionsDialog from "../widgets/dialogs/bulk_actions.js";import CallToActionDialog from "../widgets/dialogs/call_to_action.jsx";import CloneToDialog from "../widgets/dialogs/clone_to.js";import ConfirmDialog from "../widgets/dialogs/confirm.js";@@ -28,11 +31,13 @@ import RecentChangesDialog from "../widgets/dialogs/recent_changes.js";import RevisionsDialog from "../widgets/dialogs/revisions.js";import SortChildNotesDialog from "../widgets/dialogs/sort_child_notes.js";import UploadAttachmentsDialog from "../widgets/dialogs/upload_attachments.js";+import { useTriliumEvents } from "../widgets/react/hooks.jsx";+import { ParentComponent } from "../widgets/react/react_utils.jsx";import ToastContainer from "../widgets/Toast.jsx";export function applyModals(rootContainer: RootContainer) {rootContainer- .child(<BulkActionsDialog />)+ .child(<LazyDialog triggerEvents={["openBulkActionsDialog"]} loader={() => import("../widgets/dialogs/bulk_actions.js")} />).child(<AboutDialog />).child(<HelpDialog />).child(<RecentChangesDialog />)@@ -63,3 +68,46 @@ export function applyModals(rootContainer: RootContainer) {.child(<UploadAttachmentsDialog />).child(<ToastContainer />);}++interface LazyDialogProps {+ /** Loader returning the module whose default export is the dialog component, e.g. `() => import("../widgets/dialogs/about.js")`. */+ loader: () => Promise<{ default: ComponentType }>;+ /** The events that summon this dialog; the first one to fire loads and mounts it. */+ triggerEvents: EventNames[];+}++/**+ * Mounts a dialog on demand: dialogs are summoned via events (with any results reported through+ * callbacks in the event data), so until the first summons there is no reason to have the dialog+ * or its module graph around. Once loaded, the dialog stays mounted and handles further events+ * itself.+ *+ * The buffered first event is re-delivered through the subtree's host component, which only+ * notifies handlers registered within this subtree (the wrapper and the dialog), not the rest of+ * the application. The re-delivery happens in an effect because parent effects run after the+ * children's, guaranteeing that the dialog's own event handlers are registered by then.+ *+ * Limitation: a second summons arriving while the dialog module is still being fetched replaces+ * the buffered event rather than queueing behind it.+ */+function LazyDialog({ loader, triggerEvents }: LazyDialogProps) {+ const parentComponent = useContext(ParentComponent);+ const [ Component, setComponent ] = useState<ComponentType>();+ const [ pendingEvent, setPendingEvent ] = useState<{ name: EventNames, data: unknown }>();++ useTriliumEvents(triggerEvents, async (data, name) => {+ // Once mounted, the dialog receives events directly through the shared host component.+ if (Component) return;+ const module = await loader();+ setComponent(() => module.default);+ setPendingEvent({ name, data });+ });++ useEffect(() => {+ if (!Component || !pendingEvent) return;+ void parentComponent?.handleEvent(pendingEvent.name, pendingEvent.data as EventData<EventNames>);+ setPendingEvent(undefined);+ }, [ Component, pendingEvent, parentComponent ]);++ return <div className="lazy-component">{Component && <Component />}</div>;+}