import { ChevronDown, ChevronRight, File, FileImage, FilePlus2, FileText, Folder, Pencil, Star, Trash2, Upload } from 'lucide-react'; import { type DragEvent, useMemo, useRef, useState } from 'react'; import type { FileInfo } from '../api'; interface Props { files: FileInfo[]; activePath: string | null; mainFile: string; canEdit: boolean; onOpen: (path: string) => void; onCreate: () => void; onUpload: (files: File[], dir: string) => void; onRename: (path: string) => void; onDelete: (path: string, isFolder: boolean) => void; onSetMain: (path: string) => void; } interface Node { name: string; path: string; file?: FileInfo; children: Node[]; } function buildTree(files: FileInfo[]): Node[] { const root: Node = { name: '', path: '', children: [] }; for (const file of files) { let node = root; const parts = file.path.split('/'); parts.forEach((name, i) => { const path = parts.slice(0, i + 1).join('/'); let child = node.children.find((c) => c.name === name); if (!child) { child = { name, path, children: [] }; node.children.push(child); } if (i === parts.length - 1) child.file = file; node = child; }); } const sort = (nodes: Node[]) => { // Önce klasörler, sonra dosyalar; her biri Türkçe alfabetik sırada. nodes.sort((a, b) => Number(Boolean(a.file)) - Number(Boolean(b.file)) || a.name.localeCompare(b.name, 'tr')); nodes.forEach((n) => sort(n.children)); return nodes; }; return sort(root.children); } const IMAGE = /\.(png|jpe?g|gif|svg|webp|pdf|eps)$/i; export function FileTree(props: Props) { const { files, activePath, mainFile, canEdit } = props; const tree = useMemo(() => buildTree(files), [files]); const [collapsed, setCollapsed] = useState>(new Set()); const [dragOver, setDragOver] = useState(false); const uploadInput = useRef(null); const toggle = (path: string) => setCollapsed((prev) => { const next = new Set(prev); if (!next.delete(path)) next.add(path); return next; }); function onDrop(e: DragEvent) { e.preventDefault(); setDragOver(false); if (canEdit && e.dataTransfer.files.length) props.onUpload([...e.dataTransfer.files], ''); } function renderNode(node: Node, depth: number) { const indent = { paddingLeft: 10 + depth * 14 }; if (!node.file) { const open = !collapsed.has(node.path); return (
  • toggle(node.path)}> {open ? : } {node.name} {canEdit && ( e.stopPropagation()}> )}
    {open &&
      {node.children.map((c) => renderNode(c, depth + 1))}
    }
  • ); } const isMain = node.path === mainFile; const Icon = IMAGE.test(node.name) ? FileImage : /\.(tex|bib|sty|cls)$/i.test(node.name) ? FileText : File; return (
  • props.onOpen(node.path)} title={node.path} > {node.name} {isMain && } {canEdit && ( e.stopPropagation()}> {!isMain && node.name.endsWith('.tex') && ( )} {!isMain && } )}
  • ); } return (
    { if (canEdit) { e.preventDefault(); setDragOver(true); } }} onDragLeave={() => setDragOver(false)} onDrop={onDrop} > {canEdit && (
    { const list = [...(e.target.files ?? [])]; e.target.value = ''; if (list.length) props.onUpload(list, ''); }} />
    )}
      {tree.map((n) => renderNode(n, 0))}
    {canEdit &&

    Görsel ve dosyaları buraya sürükleyip bırakabilirsiniz.

    }
    ); }