import { useState } from "react"; import type { FileNode, FilePreviewData } from "../types"; import { api } from "../api/client"; interface Props { nodes: FileNode[]; onPreview: (data: FilePreviewData) => void; } const EXT_ICONS: Record = { py: "🐍", ts: "📘", tsx: "⚛️", js: "📜", jsx: "⚛️", json: "📋", yaml: "📋", yml: "📋", toml: "📋", md: "📝", mdx: "📝", txt: "📄", pdf: "📕", png: "🖼️", jpg: "🖼️", jpeg: "🖼️", svg: "🎨", sh: "⚡", bash: "⚡", zsh: "⚡", rs: "🦀", go: "🐹", cpp: "⚙️", c: "⚙️", css: "🎨", scss: "🎨", html: "🌐", sql: "🗃️", db: "🗃️", }; function extIcon(name: string): string { const ext = name.split(".").pop()?.toLowerCase() || ""; return EXT_ICONS[ext] || "📄"; } function FileItem({ node, depth, onPreview }: { node: FileNode; depth: number; onPreview: (d: FilePreviewData) => void; }) { const [open, setOpen] = useState(depth < 1); const [loading, setLoading] = useState(false); async function handlePreview() { if (node.is_dir || !node.preview_type || node.preview_type === "none") return; // Images and PDFs: FilePreview renders them via URL — no JSON API call needed. if (node.preview_type === "image") { onPreview({ type: "image", path: node.path, name: node.name }); return; } if (node.preview_type === "pdf") { onPreview({ type: "pdf", path: node.path, name: node.name }); return; } setLoading(true); try { const data = await api.file.preview(node.path); onPreview(data); } catch (e) { console.error(e); } finally { setLoading(false); } } const indent = depth * 14; const canPreview = !node.is_dir && node.preview_type && node.preview_type !== "none"; return (
{ if (canPreview) (e.currentTarget as HTMLElement).style.background = "rgba(255,255,255,0.06)"; }} onMouseLeave={(e) => { (e.currentTarget as HTMLElement).style.background = "transparent"; }} onClick={node.is_dir ? () => setOpen(!open) : handlePreview} > {node.is_dir ? (open ? "▾" : "▸") : ""} {node.is_dir ? (open ? "📂" : "📁") : extIcon(node.name)} {node.name} {node.operation === "write" && !node.is_dir && ( )} {loading && }
{node.is_dir && open && node.children && (
{node.children.map((child) => ( ))}
)}
); } export function FileExplorer({ nodes, onPreview }: Props) { if (!nodes.length) { return (
No files touched in this session
); } return (
{nodes.map((node) => ( ))}
); }