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_sub5c59966a671
diff --git a/src/components/layout/app-layout.tsx b/src/components/layout/app-layout.tsx
index b84168f7..3cee5456 100644
--- a/src/components/layout/app-layout.tsx
+++ b/src/components/layout/app-layout.tsx
@@ -21,6 +21,7 @@ export function AppLayout({ onSwitchProject }: AppLayoutProps) {
const activeView = useWikiStore((s) => s.activeView)
const researchPanelOpen = useResearchStore((s) => s.panelOpen)
const setFileTree = useWikiStore((s) => s.setFileTree)
+ const setProjectPathIndexFromTree = useWikiStore((s) => s.setProjectPathIndexFromTree)
const [leftWidth, setLeftWidth] = useState(220)
const [rightWidth, setRightWidth] = useState(400)
const isDraggingLeft = useRef(false)
@@ -29,13 +30,25 @@ export function AppLayout({ onSwitchProject }: AppLayoutProps) {
const loadFileTree = useCallback(async () => {
if (!project) return
+ const projectId = project.id
+ const projectPath = normalizePath(project.path)
try {
- const tree = await listDirectory(normalizePath(project.path))
- setFileTree(tree)
+ const tree = await listDirectory(projectPath, { maxDepth: 2 })
+ if (useWikiStore.getState().project?.id !== projectId) return
+ setFileTree(tree, { syncPathIndex: false })
} catch (err) {
console.error("Failed to load file tree:", err)
}
- }, [project, setFileTree])
+
+ listDirectory(projectPath)
+ .then((tree) => {
+ if (useWikiStore.getState().project?.id !== projectId) return
+ setProjectPathIndexFromTree(tree)
+ })
+ .catch((err) => {
+ console.error("Failed to build project path index:", err)
+ })
+ }, [project, setFileTree, setProjectPathIndexFromTree])
useEffect(() => {
loadFileTree()
diff --git a/src/components/layout/file-tree.tsx b/src/components/layout/file-tree.tsx
index 22887e1f..33b84fc7 100644
--- a/src/components/layout/file-tree.tsx
+++ b/src/components/layout/file-tree.tsx
@@ -1,14 +1,34 @@
-import { useState } from "react"
+import { useEffect, useRef, useState } from "react"
import { ChevronRight, ChevronDown, File, Folder, FolderOpen } from "lucide-react"
import { message } from "@tauri-apps/plugin-dialog"
import { ScrollArea } from "@/components/ui/scroll-area"
import { useWikiStore } from "@/stores/wiki-store"
import type { FileNode } from "@/types/wiki"
import { useTranslation } from "react-i18next"
-import { openProjectFolder } from "@/commands/fs"
+import { listDirectory, openProjectFolder } from "@/commands/fs"
-function TreeNode({ node, depth }: { node: FileNode; depth: number }) {
+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
+ })
+}
+
+function TreeNode({
+ node,
+ depth,
+ onLoadChildren,
+}: {
+ node: FileNode
+ depth: number
+ onLoadChildren: (node: FileNode) => Promise<void>
+}) {
+ const { t } = useTranslation()
const [expanded, setExpanded] = useState(depth < 1)
+ const [loadingChildren, setLoadingChildren] = useState(false)
const selectedFile = useWikiStore((s) => s.selectedFile)
const openPathInPreview = useWikiStore((s) => s.openPathInPreview)
@@ -16,10 +36,22 @@ function TreeNode({ node, depth }: { node: FileNode; depth: number }) {
const paddingLeft = 12 + depth * 16
if (node.is_dir) {
+ const handleToggle = async () => {
+ const nextExpanded = !expanded
+ setExpanded(nextExpanded)
+ if (!nextExpanded || node.children) return
+ setLoadingChildren(true)
+ try {
+ await onLoadChildren(node)
+ } finally {
+ setLoadingChildren(false)
+ }
+ }
+
return (
<div>
<button
- onClick={() => setExpanded(!expanded)}
+ onClick={() => void handleToggle()}
className="flex w-full items-center gap-1 py-1 text-sm text-muted-foreground hover:bg-accent/50 hover:text-accent-foreground"
style={{ paddingLeft }}
>
@@ -30,9 +62,19 @@ function TreeNode({ node, depth }: { node: FileNode; depth: number }) {
)}
<Folder className="h-3.5 w-3.5 shrink-0 text-blue-400" />
<span className="truncate">{node.name}</span>
+ {loadingChildren && (
+ <span className="ml-auto pr-2 text-[10px] text-muted-foreground">
+ {t("common.loading", { defaultValue: "Loading..." })}
+ </span>
+ )}
</button>
{expanded && node.children?.map((child) => (
- <TreeNode key={child.path} node={child} depth={depth + 1} />
+ <TreeNode
+ key={child.path}
+ node={child}
+ depth={depth + 1}
+ onLoadChildren={onLoadChildren}
+ />
))}
</div>
)
@@ -57,7 +99,13 @@ function TreeNode({ node, depth }: { node: FileNode; depth: number }) {
export function FileTree() {
const { t } = useTranslation()
const fileTree = useWikiStore((s) => s.fileTree)
+ const setFileTree = useWikiStore((s) => s.setFileTree)
const project = useWikiStore((s) => s.project)
+ const loadedPaths = useRef(new Set<string>())
+
+ useEffect(() => {
+ loadedPaths.current.clear()
+ }, [project?.id])
const handleOpenProjectFolder = async () => {
if (!project) return
@@ -80,6 +128,23 @@ export function FileTree() {
}
}
+ const handleLoadChildren = async (node: FileNode) => {
+ if (!project) return
+ if (loadedPaths.current.has(node.path)) return
+ const projectId = project.id
+ 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), {
+ syncPathIndex: false,
+ })
+ } catch (err) {
+ console.error("[FileTree] load children failed:", err)
+ }
+ }
+
if (!project) {
return (
<div className="flex h-full items-center justify-center p-4 text-sm text-muted-foreground">
@@ -96,7 +161,12 @@ export function FileTree() {
{project.name}
</div>
{fileTree.map((node) => (
- <TreeNode key={node.path} node={node} depth={0} />
+ <TreeNode
+ key={node.path}
+ node={node}
+ depth={0}
+ onLoadChildren={handleLoadChildren}
+ />
))}
</div>
</ScrollArea>
diff --git a/src/stores/wiki-store.test.ts b/src/stores/wiki-store.test.ts
index 1213868e..94f51111 100644
--- a/src/stores/wiki-store.test.ts
+++ b/src/stores/wiki-store.test.ts
@@ -67,6 +67,23 @@ describe("wiki preview store actions", () => {
expect(index.filesByName.get("new.md")?.[0]?.path).toBe("/project/wiki/new.md")
})
+ it("can update the display tree without replacing the project path index", () => {
+ useWikiStore.getState().setFileTree([
+ { name: "indexed.md", path: "/project/wiki/indexed.md", is_dir: false },
+ ])
+
+ useWikiStore.getState().setFileTree([
+ { name: "display-only.md", path: "/project/wiki/display-only.md", is_dir: false },
+ ], { syncPathIndex: false })
+
+ const state = useWikiStore.getState()
+ expect(state.fileTree[0]?.name).toBe("display-only.md")
+ expect(state.projectPathIndex.filesByName.get("indexed.md")?.[0]?.path).toBe(
+ "/project/wiki/indexed.md",
+ )
+ expect(state.projectPathIndex.filesByName.get("display-only.md")).toBeUndefined()
+ })
+
it("opens a path in the wiki preview and clears external previews", () => {
useWikiStore.setState({
activeView: "chat",
diff --git a/src/stores/wiki-store.ts b/src/stores/wiki-store.ts
index ef88c933..b9b09a1b 100644
--- a/src/stores/wiki-store.ts
+++ b/src/stores/wiki-store.ts
@@ -358,7 +358,8 @@ interface WikiState {
dataVersion: number
setProject: (project: WikiProject | null) => void
- setFileTree: (tree: FileNode[]) => void
+ setFileTree: (tree: FileNode[], options?: { syncPathIndex?: boolean }) => void
+ setProjectPathIndexFromTree: (tree: FileNode[]) => void
setSelectedFile: (path: string | null) => void
setFileContent: (content: string) => void
openPathInPreview: (path: string) => void
@@ -409,8 +410,15 @@ export const useWikiStore = create<WikiState>((set) => ({
dataVersion: 0,
setProject: (project) => set({ project }),
- setFileTree: (fileTree) =>
- set({ fileTree, projectPathIndex: buildProjectPathIndexFromTree(fileTree) }),
+ setFileTree: (fileTree, options) => {
+ if (options?.syncPathIndex === false) {
+ set({ fileTree })
+ return
+ }
+ set({ fileTree, projectPathIndex: buildProjectPathIndexFromTree(fileTree) })
+ },
+ setProjectPathIndexFromTree: (tree) =>
+ set({ projectPathIndex: buildProjectPathIndexFromTree(tree) }),
setSelectedFile: (selectedFile) =>
set({ selectedFile, previewContentPath: null, externalPreview: null }),
setFileContent: (fileContent) => set({ fileContent }),