/** * FileBrowser — Markdown file tree for the sidebar * * Displays collapsible trees of markdown files from user-configured directories. * Clicking a file opens it in the main viewer for annotation. */ import React from "react"; import type { VaultNode } from "../../types"; import type { DirState } from "../../hooks/useFileBrowser"; import { CountBadge } from "./CountBadge"; import { ObsidianIconRaw } from "../icons/ObsidianIcons"; interface FileBrowserProps { dirs: DirState[]; expandedFolders: Set; onToggleFolder: (key: string) => void; collapsedDirs: Set; onToggleCollapse: (dirPath: string) => void; onSelectFile: (absolutePath: string, dirPath: string) => void; activeFile: string | null; onFetchAll: () => void; onRetryVaultDir?: (vaultPath: string) => void; annotationCounts?: Map; highlightedFiles?: Set; } /** Recursively sum annotation counts for all descendant files of a folder node */ function getAggregateCount( node: VaultNode, dirPath: string, counts: Map ): number { if (node.type === "file") { return counts.get(`${dirPath}/${node.path}`) ?? 0; } let total = 0; for (const child of node.children ?? []) { total += getAggregateCount(child, dirPath, counts); } return total; } const TreeNode: React.FC<{ node: VaultNode; depth: number; dirPath: string; expandedFolders: Set; onToggleFolder: (key: string) => void; onSelectFile: (absolutePath: string, dirPath: string) => void; activeFile: string | null; annotationCounts?: Map; highlightedFiles?: Set; }> = ({ node, depth, dirPath, expandedFolders, onToggleFolder, onSelectFile, activeFile, annotationCounts, highlightedFiles }) => { const folderKey = `${dirPath}:${node.path}`; const absolutePath = `${dirPath}/${node.path}`; const isExpanded = expandedFolders.has(folderKey); const isActive = node.type === "file" && absolutePath === activeFile; const paddingLeft = 8 + depth * 14; if (node.type === "folder") { const aggregateCount = annotationCounts ? getAggregateCount(node, dirPath, annotationCounts) : 0; return ( <> {isExpanded && node.children?.map((child) => ( ))} ); } const displayName = node.name.replace(/\.mdx?$/i, ""); const fileCount = annotationCounts?.get(absolutePath) ?? 0; const isHighlighted = highlightedFiles?.has(absolutePath); return ( ); }; const DirSection: React.FC<{ dir: DirState; expandedFolders: Set; onToggleFolder: (key: string) => void; onSelectFile: (absolutePath: string, dirPath: string) => void; activeFile: string | null; onRetry: () => void; annotationCounts?: Map; highlightedFiles?: Set; }> = ({ dir, expandedFolders, onToggleFolder, onSelectFile, activeFile, onRetry, annotationCounts, highlightedFiles }) => { if (dir.isLoading) { return (
Loading...
); } if (dir.error) { return (
{dir.error}
); } if (dir.tree.length === 0) { return (
No markdown files found
); } return (
{dir.tree.map((node) => ( ))}
); }; export const FileBrowser: React.FC = ({ dirs, expandedFolders, onToggleFolder, collapsedDirs, onToggleCollapse, onSelectFile, activeFile, onFetchAll, onRetryVaultDir, annotationCounts, highlightedFiles, }) => { if (dirs.length === 0) { return (
No directories configured. Add directories in Settings → Files.
); } // Summary header const totalCount = annotationCounts ? Array.from(annotationCounts.values()).reduce((s, c) => s + c, 0) : 0; const fileCount = annotationCounts?.size ?? 0; return (
{totalCount > 0 && (
{totalCount} annotation{totalCount === 1 ? '' : 's'} in {fileCount} file{fileCount === 1 ? '' : 's'}
)} {dirs.map((dir) => { const isCollapsed = collapsedDirs.has(dir.path); return (
{!isCollapsed && ( onRetryVaultDir(dir.path) : onFetchAll} annotationCounts={annotationCounts} highlightedFiles={highlightedFiles} /> )}
); })}
); };