From 16a9caa555a2d63b7ff8af0731fbfd3231d6225c Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Sun, 15 Sep 2024 18:05:02 -0700 Subject: [PATCH] perf: eagerly compute explorer nodes to avoid re-render in memoized value --- quartz/components/Explorer.tsx | 76 ++++++++++++++++-------------- quartz/components/ExplorerNode.tsx | 13 ++--- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/quartz/components/Explorer.tsx b/quartz/components/Explorer.tsx index ec7c48e..df876ab 100644 --- a/quartz/components/Explorer.tsx +++ b/quartz/components/Explorer.tsx @@ -7,6 +7,7 @@ import { ExplorerNode, FileNode, Options } from "./ExplorerNode" import { QuartzPluginData } from "../plugins/vfile" import { classNames } from "../util/lang" import { i18n } from "../i18n" +import { VNode } from "preact" // Options interface defined in `ExplorerNode` to avoid circular dependency const defaultOptions = { @@ -44,6 +45,7 @@ export default ((userOpts?: Partial) => { // memoized let fileTree: FileNode let jsonTree: string + let component: VNode let lastBuildId: string = "" function constructFileTree(allFiles: QuartzPluginData[]) { @@ -82,44 +84,46 @@ export default ((userOpts?: Partial) => { if (ctx.buildId !== lastBuildId) { lastBuildId = ctx.buildId constructFileTree(allFiles) + const tree = ExplorerNode({ node: fileTree, opts, fileData }) + component = ( +
+ +
+
    + {tree} +
  • +
+
+
+ ) } - return ( -
- -
-
    - -
  • -
-
-
- ) + return component } Explorer.css = explorerStyle diff --git a/quartz/components/ExplorerNode.tsx b/quartz/components/ExplorerNode.tsx index e57d677..3137a3f 100644 --- a/quartz/components/ExplorerNode.tsx +++ b/quartz/components/ExplorerNode.tsx @@ -224,15 +224,10 @@ export function ExplorerNode({ node, opts, fullPath, fileData }: ExplorerNodePro class="content" data-folderul={folderPath} > - {node.children.map((childNode, i) => ( - - ))} + {node.children.map((childNode) => + // eagerly render children so we can memoize properly + ExplorerNode({ node: childNode, opts, fileData, fullPath: folderPath }), + )}