Something went wrong. Try again.
Fork of backnotprop/plannotator with reduced surface area
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201/** * File Browser Hook * * Manages multiple directory file trees for the sidebar Files tab. * Each directory gets its own tree, loading, and error state. * Vault directories are supported via the isVault flag — they fetch * from the Obsidian vault endpoint instead of the generic files endpoint. */
import { useState, useCallback } from "react";import type { VaultNode } from "../types";
export interface DirState { path: string; name: string; tree: VaultNode[]; isLoading: boolean; error: string | null; /** When true, fetches via /api/reference/obsidian/files and opens docs via /api/reference/obsidian/doc */ isVault?: boolean;}
export interface UseFileBrowserReturn { dirs: DirState[]; expandedFolders: Set<string>; toggleFolder: (key: string) => void; collapsedDirs: Set<string>; toggleCollapse: (dirPath: string) => void; fetchTree: (dirPath: string) => void; fetchAll: (directories: string[]) => void; addVaultDir: (vaultPath: string) => void; clearVaultDirs: () => void; activeFile: string | null; activeDirPath: string | null; setActiveFile: (path: string | null) => void;}
export function useFileBrowser(): UseFileBrowserReturn { const [dirs, setDirs] = useState<DirState[]>([]); const [expandedFolders, setExpandedFolders] = useState<Set<string>>(new Set()); const [activeFile, setActiveFile] = useState<string | null>(null); const [collapsedDirs, setCollapsedDirs] = useState<Set<string>>(new Set());
const toggleCollapse = useCallback((dirPath: string) => { setCollapsedDirs((prev) => { const next = new Set(prev); if (next.has(dirPath)) next.delete(dirPath); else next.add(dirPath); return next; }); }, []);
const fetchTree = useCallback(async (dirPath: string) => { const name = dirPath.split("/").pop() || dirPath;
setDirs((prev) => { const exists = prev.find((d) => d.path === dirPath); if (exists) { return prev.map((d) => d.path === dirPath ? { ...d, isLoading: true, error: null } : d ); } return [...prev, { path: dirPath, name, tree: [], isLoading: true, error: null }]; });
try { const res = await fetch( `/api/reference/files?dirPath=${encodeURIComponent(dirPath)}` ); const data = await res.json();
if (!res.ok || data.error) { setDirs((prev) => prev.map((d) => d.path === dirPath ? { ...d, isLoading: false, error: data.error || "Failed to load" } : d ) ); return; }
setDirs((prev) => prev.map((d) => d.path === dirPath ? { ...d, tree: data.tree, isLoading: false, error: null } : d ) );
const rootFolders = (data.tree as VaultNode[]) .filter((n) => n.type === "folder") .map((n) => `${dirPath}:${n.path}`); setExpandedFolders((prev) => { const next = new Set(prev); rootFolders.forEach((f) => next.add(f)); return next; }); } catch { setDirs((prev) => prev.map((d) => d.path === dirPath ? { ...d, isLoading: false, error: "Failed to connect to server" } : d ) ); } }, []);
const fetchAll = useCallback( (directories: string[]) => { setDirs((prev) => { // Preserve any vault dirs that were already loaded const vaultDirs = prev.filter((d) => d.isVault); const regularDirs = directories.map((path) => ({ path, name: path.split("/").pop() || path, tree: [], isLoading: false, error: null, })); return [...regularDirs, ...vaultDirs]; }); directories.forEach((d) => fetchTree(d)); }, [fetchTree] );
const clearVaultDirs = useCallback(() => { setDirs((prev) => prev.filter((d) => !d.isVault)); }, []);
const addVaultDir = useCallback(async (vaultPath: string) => { const name = vaultPath.split("/").pop() || vaultPath;
// Atomically replace any existing vault dirs (handles vault path change without accumulating stale entries) setDirs((prev) => { const nonVaultDirs = prev.filter((d) => !d.isVault); return [...nonVaultDirs, { path: vaultPath, name, tree: [], isLoading: true, error: null, isVault: true }]; });
try { const res = await fetch( `/api/reference/obsidian/files?vaultPath=${encodeURIComponent(vaultPath)}` ); const data = await res.json();
if (!res.ok || data.error) { setDirs((prev) => prev.map((d) => d.path === vaultPath ? { ...d, isLoading: false, error: data.error || "Failed to load" } : d ) ); return; }
setDirs((prev) => prev.map((d) => d.path === vaultPath ? { ...d, tree: data.tree, isLoading: false, isVault: true } : d ) );
const rootFolders = (data.tree as VaultNode[]) .filter((n) => n.type === "folder") .map((n) => `${vaultPath}:${n.path}`); setExpandedFolders((prev) => { const next = new Set(prev); rootFolders.forEach((f) => next.add(f)); return next; }); } catch { setDirs((prev) => prev.map((d) => d.path === vaultPath ? { ...d, isLoading: false, error: "Failed to connect to server" } : d ) ); } }, []);
const toggleFolder = useCallback((key: string) => { setExpandedFolders((prev) => { const next = new Set(prev); if (next.has(key)) { next.delete(key); } else { next.add(key); } return next; }); }, []);
return { dirs, expandedFolders, toggleFolder, collapsedDirs, toggleCollapse, fetchTree, fetchAll, addVaultDir, clearVaultDirs, activeFile, activeDirPath: activeFile ? (dirs.find((d) => activeFile.startsWith(d.path + "/"))?.path ?? null) : null, setActiveFile, };}