Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

perf: lazy load sidebar file tree

nash_su6e65fbfbcd84
diff --git a/src/components/layout/app-layout.tsx b/src/components/layout/app-layout.tsx
index 3cee5456..7dcb6945 100644
--- a/src/components/layout/app-layout.tsx
+++ b/src/components/layout/app-layout.tsx
@@ -16,6 +16,25 @@ interface AppLayoutProps {
onSwitchProject: () => void
}
+async function loadDirectoryWithRetry(
+ path: string,
+ options: Parameters<typeof listDirectory>[1],
+ attempts = 2,
+): Promise<Awaited<ReturnType<typeof listDirectory>>> {
+ let lastError: unknown
+ for (let i = 0; i < attempts; i += 1) {
+ try {
+ return await listDirectory(path, options)
+ } catch (err) {
+ lastError = err
+ if (i < attempts - 1) {
+ await new Promise((resolve) => setTimeout(resolve, 250))
+ }
+ }
+ }
+ throw lastError
+}
+
export function AppLayout({ onSwitchProject }: AppLayoutProps) {
const project = useWikiStore((s) => s.project)
const activeView = useWikiStore((s) => s.activeView)
@@ -32,15 +51,16 @@ export function AppLayout({ onSwitchProject }: AppLayoutProps) {
if (!project) return
const projectId = project.id
const projectPath = normalizePath(project.path)
+ setFileTree([], { syncPathIndex: false })
try {
const tree = await listDirectory(projectPath, { maxDepth: 2 })
if (useWikiStore.getState().project?.id !== projectId) return
- setFileTree(tree, { syncPathIndex: false })
+ setFileTree(tree)
} catch (err) {
console.error("Failed to load file tree:", err)
}
- listDirectory(projectPath)
+ loadDirectoryWithRetry(projectPath, undefined, 3)
.then((tree) => {
if (useWikiStore.getState().project?.id !== projectId) return
setProjectPathIndexFromTree(tree)
diff --git a/src/components/layout/file-tree-utils.test.ts b/src/components/layout/file-tree-utils.test.ts
new file mode 100644
index 00000000..14314fd8
--- /dev/null
+++ b/src/components/layout/file-tree-utils.test.ts
@@ -0,0 +1,51 @@
+import { describe, expect, it } from "vitest"
+import type { FileNode } from "@/types/wiki"
+import { replaceNodeChildren } from "./file-tree-utils"
+
+function file(path: string): FileNode {
+ return { name: path.split("/").pop()!, path, is_dir: false }
+}
+
+function dir(path: string, children?: FileNode[]): FileNode {
+ return { name: path.split("/").pop()!, path, is_dir: true, children }
+}
+
+describe("replaceNodeChildren", () => {
+ it("replaces children for a top-level directory", () => {
+ const tree = [dir("/p/wiki"), file("/p/schema.md")]
+ const children = [file("/p/wiki/page.md")]
+
+ const result = replaceNodeChildren(tree, "/p/wiki", children)
+
+ expect(result.matched).toBe(true)
+ expect(result.nodes[0]).toEqual({ ...dir("/p/wiki"), children })
+ expect(result.nodes[1]).toBe(tree[1])
+ })
+
+ it("replaces children for a nested directory without touching siblings", () => {
+ const sibling = file("/p/wiki/a.md")
+ const tree = [
+ dir("/p/wiki", [
+ sibling,
+ dir("/p/wiki/nested"),
+ ]),
+ ]
+ const children = [file("/p/wiki/nested/b.md")]
+
+ const result = replaceNodeChildren(tree, "/p/wiki/nested", children)
+
+ expect(result.matched).toBe(true)
+ expect(result.nodes[0]).not.toBe(tree[0])
+ expect(result.nodes[0].children?.[0]).toBe(sibling)
+ expect(result.nodes[0].children?.[1]).toEqual({ ...dir("/p/wiki/nested"), children })
+ })
+
+ it("returns the original tree when the path is not found", () => {
+ const tree = [dir("/p/wiki")]
+
+ const result = replaceNodeChildren(tree, "/p/missing", [file("/p/missing/a.md")])
+
+ expect(result.matched).toBe(false)
+ expect(result.nodes).toBe(tree)
+ })
+})
diff --git a/src/components/layout/file-tree-utils.ts b/src/components/layout/file-tree-utils.ts
new file mode 100644
index 00000000..6c7bf73b
--- /dev/null
+++ b/src/components/layout/file-tree-utils.ts
@@ -0,0 +1,29 @@
+import type { FileNode } from "@/types/wiki"
+
+export interface ReplaceNodeChildrenResult {
+ nodes: FileNode[]
+ matched: boolean
+}
+
+export function replaceNodeChildren(
+ nodes: FileNode[],
+ path: string,
+ children: FileNode[],
+): ReplaceNodeChildrenResult {
+ let matched = false
+ const next = nodes.map((node) => {
+ if (node.path === path) {
+ matched = true
+ return { ...node, children }
+ }
+ if (node.children) {
+ const result = replaceNodeChildren(node.children, path, children)
+ if (result.matched) {
+ matched = true
+ return { ...node, children: result.nodes }
+ }
+ }
+ return node
+ })
+ return { nodes: matched ? next : nodes, matched }
+}
diff --git a/src/components/layout/file-tree.tsx b/src/components/layout/file-tree.tsx
index 33b84fc7..6c3034d1 100644
--- a/src/components/layout/file-tree.tsx
+++ b/src/components/layout/file-tree.tsx
@@ -6,16 +6,7 @@ import { useWikiStore } from "@/stores/wiki-store"
import type { FileNode } from "@/types/wiki"
import { useTranslation } from "react-i18next"
import { listDirectory, openProjectFolder } from "@/commands/fs"
-
-function replaceNodeChildren(nodes: FileNode[], path: string, children: FileNode[]): FileNode[] {
- return nodes.map((node) => {
- if (node.path === path) return { ...node, children }
- if (node.children) {
- return { ...node, children: replaceNodeChildren(node.children, path, children) }
- }
- return node
- })
-}
+import { replaceNodeChildren } from "./file-tree-utils"
function TreeNode({
node,
@@ -105,7 +96,7 @@ export function FileTree() {
useEffect(() => {
loadedPaths.current.clear()
- }, [project?.id])
+ }, [project?.id, fileTree])
const handleOpenProjectFolder = async () => {
if (!project) return
@@ -135,9 +126,11 @@ export function FileTree() {
try {
const children = await listDirectory(node.path, { maxDepth: 1 })
if (useWikiStore.getState().project?.id !== projectId) return
- loadedPaths.current.add(node.path)
const currentTree = useWikiStore.getState().fileTree
- setFileTree(replaceNodeChildren(currentTree, node.path, children), {
+ const result = replaceNodeChildren(currentTree, node.path, children)
+ if (!result.matched) return
+ loadedPaths.current.add(node.path)
+ setFileTree(result.nodes, {
syncPathIndex: false,
})
} catch (err) {
diff --git a/src/stores/wiki-store.test.ts b/src/stores/wiki-store.test.ts
index 94f51111..0ddff635 100644
--- a/src/stores/wiki-store.test.ts
+++ b/src/stores/wiki-store.test.ts
@@ -84,6 +84,23 @@ describe("wiki preview store actions", () => {
expect(state.projectPathIndex.filesByName.get("display-only.md")).toBeUndefined()
})
+ it("can rebuild only the project path index from a full tree", () => {
+ useWikiStore.getState().setFileTree([
+ { name: "display.md", path: "/project/wiki/display.md", is_dir: false },
+ ])
+
+ useWikiStore.getState().setProjectPathIndexFromTree([
+ { name: "full.md", path: "/project/wiki/deep/full.md", is_dir: false },
+ ])
+
+ const state = useWikiStore.getState()
+ expect(state.fileTree[0]?.name).toBe("display.md")
+ expect(state.projectPathIndex.filesByName.get("display.md")).toBeUndefined()
+ expect(state.projectPathIndex.filesByName.get("full.md")?.[0]?.path).toBe(
+ "/project/wiki/deep/full.md",
+ )
+ })
+
it("opens a path in the wiki preview and clears external previews", () => {
useWikiStore.setState({
activeView: "chat",