"use client"; import { useState } from "react"; import type { MouseEvent } from "react"; import { FileTree, useFileTree, useFileTreeSelection } from "@pierre/trees/react"; import { FileViewer } from "./FileViewer"; type FileTreeClickEvent = MouseEvent & { item?: { path?: unknown } }; export type TreeBrowserProps = { paths: string[]; }; export function TreeBrowser({ paths }: TreeBrowserProps) { const [selectedFile, setSelectedFile] = useState(null); const { model } = useFileTree({ paths, search: true, fileTreeSearchMode: "hide-non-matches", flattenEmptyDirectories: true, }); const selectedPaths = useFileTreeSelection(model); const selected = selectedPaths[0]; const currentFile = selected && !selected.endsWith("/") ? selected : selectedFile; return ( <>
Files
{ const path = (event as FileTreeClickEvent).item?.path; if (typeof path === "string" && !path.endsWith("/")) { setSelectedFile(path); } }} />
{currentFile ? ( ) : (
Select a file to view
)}
); }