62 lines · 1.6 KB
'use client'
import { useState, useMemo } from 'react'
import { FileTree, useFileTree, useFileTreeSelection } from '@pierre/trees/react'
import { FileViewer } from './FileViewer'

interface Props {
  paths: string[]
}

export function TreeBrowser({ paths }: Props) {
  const [selectedFile, setSelectedFile] = useState<string | null>(null)

  const { model } = useFileTree({
    paths,
    search: true,
    fileTreeSearchMode: 'hide-non-matches',
    flattenEmptyDirectories: true,
  })

  const selectedPaths = useFileTreeSelection(model)

  const currentFile = useMemo(() => {
    const sel = selectedPaths[0]
    if (sel && !sel.endsWith('/')) {
      return sel
    }
    return selectedFile
  }, [selectedPaths, selectedFile])

  return (
    <>
      <div className="flex w-72 flex-col border-r border-gray-800">
        <div className="border-b border-gray-800 px-3 py-2 text-xs font-medium text-gray-500 uppercase">
          Files
        </div>
        <div className="flex-1 overflow-auto">
          <FileTree
            model={model}
            className="h-full"
            style={{ minHeight: '100%' }}