import { useState, memo } from 'react' import { useMobile } from '@/hooks/useMobile' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { DeleteDialog } from '@/components/ui/delete-dialog' import { API_BASE_URL } from '@/config' import { File, Folder, FolderOpen, ChevronRight, ChevronDown, MoreVertical, Trash2, Edit3, Download } from 'lucide-react' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' import type { FileInfo } from '@/types/files' interface FileTreeProps { files: FileInfo[] onFileSelect: (file: FileInfo) => void onDirectoryClick: (path: string) => void selectedFile: FileInfo | null onDelete: (path: string) => void onRename: (oldPath: string, newPath: string) => void currentPath?: string basePath?: string onNavigateUp?: () => void } interface TreeNodeProps { file: FileInfo level: number onFileSelect: (file: FileInfo) => void onDirectoryClick: (path: string) => void selectedFile?: FileInfo | null onDelete?: (path: string) => void onRename?: (oldPath: string, newPath: string) => void } function TreeNode({ file, level, onFileSelect, onDirectoryClick, selectedFile, onDelete, onRename }: TreeNodeProps) { const isMobile = useMobile() const [expanded, setExpanded] = useState(false) const [editing, setEditing] = useState(false) const [editName, setEditName] = useState(file.name) const [deleteDialogOpen, setDeleteDialogOpen] = useState(false) const handleClick = () => { if (file.isDirectory) { onDirectoryClick(file.path) } else { onFileSelect(file) } } const handleDelete = () => { setDeleteDialogOpen(true) } const handleDeleteConfirm = () => { onDelete?.(file.path) setDeleteDialogOpen(false) } const handleDeleteCancel = () => { setDeleteDialogOpen(false) } const handleRename = () => { setEditing(true) setEditName(file.name) } const handleRenameSubmit = () => { if (editName && editName !== file.name) { const newPath = file.path.replace(/\/[^/]+$/, `/${editName}`) onRename?.(file.path, newPath) } setEditing(false) } const handleRenameCancel = () => { setEditing(false) setEditName(file.name) } const handleDownload = () => { if (file.isDirectory) return const downloadUrl = `${API_BASE_URL}/api/files/${file.path}?download=true` const link = document.createElement('a') link.href = downloadUrl link.download = file.name document.body.appendChild(link) link.click() document.body.removeChild(link) } const getFileIcon = () => { if (file.isDirectory) { return expanded ? : } const ext = file.name.split('.').pop()?.toLowerCase() const iconMap: Record = { 'js': '🟨', 'ts': '🔷', 'jsx': '🟨', 'tsx': '🔷', 'json': '📋', 'md': '📝', 'html': '🌐', 'css': '🎨', 'png': 'đŸ–ŧī¸', 'jpg': 'đŸ–ŧī¸', 'jpeg': 'đŸ–ŧī¸', 'gif': 'đŸ–ŧī¸', 'svg': 'đŸ–ŧī¸', 'pdf': '📄', 'zip': 'đŸ“Ļ', } return ( {iconMap[ext || ''] || } ) } return (
{file.isDirectory && ( )}
{getFileIcon()} {editing ? ( setEditName(e.target.value)} onBlur={handleRenameSubmit} onKeyDown={(e) => { if (e.key === 'Enter') handleRenameSubmit() if (e.key === 'Escape') handleRenameCancel() }} onClick={(e) => e.stopPropagation()} className="h-10" autoFocus /> ) : ( {file.name} )}
{!file.isDirectory && ( Download )} Rename Delete
{file.isDirectory && expanded && file.children && (
{file.children.map((child) => ( ))}
)}
) } export const FileTree = memo(function FileTree({ files, onFileSelect, onDirectoryClick, selectedFile, onDelete, onRename, currentPath = '', basePath = '', onNavigateUp }: FileTreeProps) { const handleGoUp = () => { onNavigateUp?.() } const showGoUp = currentPath && currentPath !== basePath return (
{showGoUp && (
â†Šī¸ ..
)} {files.length === 0 ? (
No files in this directory
) : ( files.map((file) => ( )) )}
) })