({ workspace, path, onNavigate, viewMode = "list" }: FileBrowserProps)
| 274 | |
| 275 | // ─── Main FileBrowser Component ──────────────────────────────────────────────── |
| 276 | export function FileBrowser({ workspace, path, onNavigate, viewMode = "list" }: FileBrowserProps) { |
| 277 | const [items, setItems] = useState<FileEntry[]>([]); |
| 278 | const [loading, setLoading] = useState(true); |
| 279 | const [error, setError] = useState<string | null>(null); |
| 280 | const [previewFile, setPreviewFile] = useState<{ workspace: string; path: string; name: string } | null>(null); |
| 281 | const [editorFile, setEditorFile] = useState<{ workspace: string; path: string; name: string } | null>(null); |
| 282 | const [dragging, setDragging] = useState(false); |
| 283 | const [uploading, setUploading] = useState(false); |
| 284 | const [confirmDelete, setConfirmDelete] = useState<FileEntry | null>(null); |
| 285 | const [newFolderName, setNewFolderName] = useState(""); |
| 286 | const [showNewFolder, setShowNewFolder] = useState(false); |
| 287 | const [newFileName, setNewFileName] = useState(""); |
| 288 | const [showNewFile, setShowNewFile] = useState(false); |
| 289 | const [actionMenu, setActionMenu] = useState<string | null>(null); |
| 290 | const fileInputRef = useRef<HTMLInputElement>(null); |
| 291 | |
| 292 | const loadItems = useCallback(() => { |
| 293 | setLoading(true); |
| 294 | setError(null); |
| 295 | fetch(`/api/browse?workspace=${encodeURIComponent(workspace)}&path=${encodeURIComponent(path)}`) |
| 296 | .then((res) => { |
| 297 | if (!res.ok) throw new Error("Failed to load directory"); |
| 298 | return res.json(); |
| 299 | }) |
| 300 | .then((data) => { |
| 301 | setItems(data.items || []); |
| 302 | setLoading(false); |
| 303 | }) |
| 304 | .catch((err) => { |
| 305 | setError(err.message); |
| 306 | setLoading(false); |
| 307 | }); |
| 308 | }, [workspace, path]); |
| 309 | |
| 310 | useEffect(() => { |
| 311 | loadItems(); |
| 312 | }, [loadItems]); |
| 313 | |
| 314 | const handleItemClick = (item: FileEntry) => { |
| 315 | if (item.type === "folder") { |
| 316 | const newPath = path ? `${path}/${item.name}` : item.name; |
| 317 | onNavigate(newPath); |
| 318 | } else { |
| 319 | const filePath = path ? `${path}/${item.name}` : item.name; |
| 320 | if (isEditable(item.name)) { |
| 321 | setEditorFile({ workspace, path: filePath, name: item.name }); |
| 322 | } else { |
| 323 | setPreviewFile({ workspace, path: filePath, name: item.name }); |
| 324 | } |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | // Upload handler |
| 329 | const handleUpload = async (files: FileList | null) => { |
| 330 | if (!files || files.length === 0) return; |
| 331 | setUploading(true); |
| 332 | try { |
| 333 | const formData = new FormData(); |
nothing calls this directly
no test coverage detected