"use client"; import React, { useCallback, useEffect, useMemo, useState } from "react"; import { Loader2, Download, Folder, FileText, RefreshCw, ArrowUp, Home } from "lucide-react"; type FileItem = { name: string; type: "dir" | "file"; size?: number; mtimeMs?: number }; type ListResponse = | { path: string; items: FileItem[] } | { path: string; entries: FileItem[] } | { items?: FileItem[]; entries?: FileItem[]; path?: string }; const BASE = (process.env.NEXT_PUBLIC_FILE_API_BASE_URL || "").replace(/\/$/, "") || ""; function joinPath(a: string, b: string) { if (!a || a === "/") return b.startsWith("/") ? b : `/${b}`; return `${a.replace(/\/$/, "")}/${b.replace(/^\//, "")}`; } function parentPath(path: string) { if (!path || path === "/") return "/"; const parts = path.replace(/\/+$/, "").split("/"); parts.pop(); const p = parts.join("/"); return p === "" ? "/" : p; } function formatSize(bytes?: number) { if (bytes == null) return "—"; const units = ["B", "KB", "MB", "GB", "TB"]; let v = bytes; let u = 0; while (v >= 1024 && u < units.length - 1) { v /= 1024; u++; } return `${v.toFixed(u ? 1 : 0)} ${units[u]}`; } function formatDate(ms?: number) { if (!ms) return "—"; const d = new Date(ms); return d.toLocaleDateString(undefined, { year: "numeric", month: "numeric", day: "numeric" }); } export default function FileBrowserPanel() { const [path, setPath] = useState("/"); const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [previewHref, setPreviewHref] = useState(null); const urlList = useMemo(() => { const p = encodeURIComponent(path || "/"); return `${BASE}/api/files/list?path=${p}`; }, [path]); const urlDownload = useCallback((p: string) => { const qp = encodeURIComponent(p || "/"); return `${BASE}/api/files/download?path=${qp}`; }, []); const urlRaw = useCallback((p: string) => { const qp = encodeURIComponent(p || "/"); return `${BASE}/api/files/raw?path=${qp}`; }, []); const fetchList = useCallback(async () => { setLoading(true); setError(null); setPreviewHref(null); try { const res = await fetch(urlList, { headers: { Accept: "application/json" }, cache: "no-store" }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const json: ListResponse = await res.json(); const arr = (json as any).items || (json as any).entries || []; if (!Array.isArray(arr)) throw new Error("Malformed list response"); arr.sort((a: FileItem, b: FileItem) => (a.type !== b.type ? (a.type === "dir" ? -1 : 1) : a.name.localeCompare(b.name))); setItems(arr); } catch (e: any) { setError(e?.message || String(e)); setItems([]); } finally { setLoading(false); } }, [urlList]); useEffect(() => { fetchList(); }, [fetchList]); const onOpen = (it: FileItem) => { if (it.type === "dir") setPath((p) => joinPath(p, it.name)); else setPreviewHref(urlRaw(joinPath(path, it.name))); }; const onUp = () => setPath((p) => parentPath(p)); const onHome = () => setPath("/"); const onDownload = (it: FileItem) => { const href = urlDownload(joinPath(path, it.name)); const a = document.createElement("a"); a.href = href; a.download = it.name; document.body.appendChild(a); a.click(); a.remove(); }; return (
{/* compact, consistent type + strict columns */} {/* Toolbar */}
{path || "/"}
{/* Layout: left flex, right fixed min; no horizontal scroll on left */}
{/* LEFT: Table */}
{/* Header */}
Name
Type
Size
Date
Get
{loading ? (
Loading…
) : error ? (
Error: {error}
) : items.length === 0 ? (
Empty folder.
) : ( items.map((it) => (
{/* Name */} {/* Type */}
{it.type}
{/* Size */}
{formatSize(it.size)}
{/* Date only */}
{formatDate(it.mtimeMs)}
{/* Actions (icon-only, fits in 44px) */}
{it.type === "file" && ( )}
)) )}
{/* RIGHT: Preview */}
Preview
{previewHref ? (