Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(explorer): expanded directories persist across drawer close and app restart

enesaf27d0b5304d
diff --git a/src/renderer/components/ExplorerPanel.tsx b/src/renderer/components/ExplorerPanel.tsx
index 171799515..4658f8276 100644
--- a/src/renderer/components/ExplorerPanel.tsx
+++ b/src/renderer/components/ExplorerPanel.tsx
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import type { DirEntry, FsApi } from '@shared/types'
import { useProjects } from '../state/projects'
+import { useExplorer } from '../state/explorer'
import { sshFs } from '../terminal/ssh-fs'
interface ExplorerPanelProps {
@@ -36,9 +37,8 @@ function TreeEntry({
path,
depth,
fs,
+ projectId,
selected,
- forcedOpen,
- revealNonce,
onContext,
onOpenFile,
onSelect
@@ -47,36 +47,32 @@ function TreeEntry({
path: string
depth: number
fs: FsApi
+ projectId: string
selected: string | null
- forcedOpen: Set<string>
- revealNonce: number | undefined
onContext: ContextFn
onOpenFile: OpenFn
onSelect: SelectFn
}) {
- const [open, setOpen] = useState(false)
+ const open = useExplorer((s) => (s.expandedByProject[projectId] ?? []).includes(path))
const [children, setChildren] = useState<DirEntry[] | null>(null)
const rowRef = useRef<HTMLDivElement>(null)
- // The last reveal nonce this row force-opened for, so we honor each reveal edge exactly once
- // and don't re-assert open afterwards (otherwise a manual collapse wouldn't stick).
- const lastHonoredRef = useRef<number | undefined>(undefined)
- const expandDir = useCallback(async () => {
- const next = !open
- setOpen(next)
- if (next && children === null) setChildren(await fs.list(path))
- }, [open, children, path, fs])
+ const toggleDir = useCallback(() => {
+ useExplorer.getState().setExpanded(projectId, path, !open)
+ }, [projectId, path, open])
- // Reveal: when this directory is force-opened (an ancestor of the reveal target), expand it
- // ONCE per reveal edge. After the edge, a manual collapse sticks because this effect won't
- // re-fire for the same nonce. Lazy-load children (awaiting the list, like the click path).
+ // Lazy-load children whenever this dir is (or becomes) open with nothing loaded yet —
+ // covers click-to-expand, restored-from-storage, and reveal-forced expansion alike.
useEffect(() => {
- if (!entry.dir || !forcedOpen.has(path)) return
- if (revealNonce === lastHonoredRef.current) return
- lastHonoredRef.current = revealNonce
- if (children === null) void fs.list(path).then(setChildren)
- setOpen(true)
- }, [forcedOpen, path, entry.dir, children, revealNonce, fs])
+ if (!entry.dir || !open || children !== null) return
+ let cancelled = false
+ void fs.list(path).then((c) => {
+ if (!cancelled) setChildren(c)
+ })
+ return () => {
+ cancelled = true
+ }
+ }, [entry.dir, open, children, path, fs])
// Reveal: scroll the selected (target) row into view once it has mounted.
useEffect(() => {
@@ -88,13 +84,13 @@ function TreeEntry({
const onClick = useCallback(() => {
if (entry.dir) {
onSelect(path)
- void expandDir()
+ toggleDir()
} else if (selected === path) {
onOpenFile(path)
} else {
onSelect(path)
}
- }, [entry.dir, selected, path, onOpenFile, onSelect, expandDir])
+ }, [entry.dir, selected, path, onOpenFile, onSelect, toggleDir])
return (
<>
@@ -124,9 +120,8 @@ function TreeEntry({
path={`${path}/${c.name}`}
depth={depth + 1}
fs={fs}
+ projectId={projectId}
selected={selected}
- forcedOpen={forcedOpen}
- revealNonce={revealNonce}
onContext={onContext}
onOpenFile={onOpenFile}
onSelect={onSelect}
@@ -161,7 +156,6 @@ export function ExplorerPanel({ onClose, onOpenFile, reveal }: ExplorerPanelProp
const [roots, setRoots] = useState<DirEntry[] | null>(null)
const [version, setVersion] = useState(0)
const [selected, setSelected] = useState<string | null>(null)
- const [forcedOpen, setForcedOpen] = useState<Set<string>>(new Set())
const [menu, setMenu] = useState<{ x: number; y: number; path: string } | null>(null)
useEffect(() => {
@@ -187,7 +181,7 @@ export function ExplorerPanel({ onClose, onOpenFile, reveal }: ExplorerPanelProp
acc = `${acc}/${parts[i]}`
dirs.add(acc)
}
- setForcedOpen(dirs)
+ useExplorer.getState().expandMany(project!.id, [...dirs])
setSelected(abs)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reveal?.nonce, cwd])
@@ -225,9 +219,8 @@ export function ExplorerPanel({ onClose, onOpenFile, reveal }: ExplorerPanelProp
path={`${cwd}/${e.name}`}
depth={0}
fs={fs}
+ projectId={project!.id}
selected={selected}
- forcedOpen={forcedOpen}
- revealNonce={reveal?.nonce}
onContext={onContext}
onOpenFile={handleOpenFile}
onSelect={setSelected}
diff --git a/src/renderer/state/explorer.test.ts b/src/renderer/state/explorer.test.ts
new file mode 100644
index 000000000..112efb221
--- /dev/null
+++ b/src/renderer/state/explorer.test.ts
@@ -0,0 +1,47 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+// The node test environment has no DOM, so shim an in-memory localStorage (matching the setup
+// in agentStatus.persist.test.ts). The store's load()/save() are try/catch-guarded, so the
+// import below runs fine even before this stub is installed.
+function memStorage(seed: Record<string, string> = {}): Storage {
+ const m = new Map(Object.entries(seed))
+ return {
+ getItem: (k: string) => m.get(k) ?? null,
+ setItem: (k: string, v: string) => void m.set(k, v),
+ removeItem: (k: string) => void m.delete(k),
+ clear: () => m.clear(),
+ key: () => null,
+ get length() {
+ return m.size
+ }
+ } as Storage
+}
+vi.stubGlobal('localStorage', memStorage())
+
+import { useExplorer, EXPLORER_EXPANDED_KEY } from './explorer'
+
+beforeEach(() => {
+ localStorage.clear()
+ useExplorer.setState({ expandedByProject: {} })
+})
+
+describe('explorer expansion store', () => {
+ it('expands and collapses per project', () => {
+ useExplorer.getState().setExpanded('p1', '/repo/src', true)
+ expect(useExplorer.getState().isExpanded('p1', '/repo/src')).toBe(true)
+ expect(useExplorer.getState().isExpanded('p2', '/repo/src')).toBe(false)
+ useExplorer.getState().setExpanded('p1', '/repo/src', false)
+ expect(useExplorer.getState().isExpanded('p1', '/repo/src')).toBe(false)
+ })
+
+ it('expandMany adds without duplicates', () => {
+ useExplorer.getState().setExpanded('p1', '/repo/src', true)
+ useExplorer.getState().expandMany('p1', ['/repo/src', '/repo/src/lib'])
+ expect(useExplorer.getState().expandedByProject['p1']).toEqual(['/repo/src', '/repo/src/lib'])
+ })
+
+ it('persists to localStorage and hydrates from it', () => {
+ useExplorer.getState().setExpanded('p1', '/repo/src', true)
+ expect(JSON.parse(localStorage.getItem(EXPLORER_EXPANDED_KEY)!)).toEqual({ p1: ['/repo/src'] })
+ })
+})
diff --git a/src/renderer/state/explorer.ts b/src/renderer/state/explorer.ts
new file mode 100644
index 000000000..3d29b6b74
--- /dev/null
+++ b/src/renderer/state/explorer.ts
@@ -0,0 +1,54 @@
+import { create } from 'zustand'
+
+// Which Explorer directories are expanded, per project — persisted so the tree comes back
+// exactly as the user left it after closing/reopening the drawer or restarting the app.
+// Values are absolute dir paths (remote paths for SSH projects). Stale paths (deleted dirs,
+// removed projects) are harmless: a path that never renders never matters.
+
+export const EXPLORER_EXPANDED_KEY = 'nodeterm.explorerExpanded'
+
+interface ExplorerState {
+ expandedByProject: Record<string, string[]>
+ isExpanded(projectId: string, path: string): boolean
+ setExpanded(projectId: string, path: string, open: boolean): void
+ expandMany(projectId: string, paths: string[]): void
+}
+
+function load(): Record<string, string[]> {
+ try {
+ const raw = localStorage.getItem(EXPLORER_EXPANDED_KEY)
+ const parsed = raw ? (JSON.parse(raw) as Record<string, string[]>) : {}
+ return parsed && typeof parsed === 'object' ? parsed : {}
+ } catch {
+ return {}
+ }
+}
+
+function save(v: Record<string, string[]>): void {
+ try {
+ localStorage.setItem(EXPLORER_EXPANDED_KEY, JSON.stringify(v))
+ } catch {
+ /* quota/private-mode: expansion is a nicety, never fail the UI */
+ }
+}
+
+export const useExplorer = create<ExplorerState>((set, get) => ({
+ expandedByProject: load(),
+ isExpanded: (projectId, path) => (get().expandedByProject[projectId] ?? []).includes(path),
+ setExpanded: (projectId, path, open) =>
+ set((s) => {
+ const cur = s.expandedByProject[projectId] ?? []
+ const next = open ? (cur.includes(path) ? cur : [...cur, path]) : cur.filter((p) => p !== path)
+ const expandedByProject = { ...s.expandedByProject, [projectId]: next }
+ save(expandedByProject)
+ return { expandedByProject }
+ }),
+ expandMany: (projectId, paths) =>
+ set((s) => {
+ const cur = s.expandedByProject[projectId] ?? []
+ const next = [...cur, ...paths.filter((p) => !cur.includes(p))]
+ const expandedByProject = { ...s.expandedByProject, [projectId]: next }
+ save(expandedByProject)
+ return { expandedByProject }
+ })
+}))