"use client"; import * as React from "react"; import { useSearchParams } from "next/navigation"; import { DownloadIcon, FileIcon, FolderIcon, FolderPlusIcon, HardDriveIcon, RefreshCwIcon, SaveIcon, Trash2Icon, UploadIcon, } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle } from "@/components/ui/sheet"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Textarea } from "@/components/ui/textarea"; import { type UITranslate, useUITranslation } from "@/i18n/use-ui-translation"; import { api } from "@/lib/api"; import type { WorkspaceEntry, WorkspaceFile } from "@/lib/types"; import { cn } from "@/lib/utils"; function fmtSize(n: number): string { if (n < 1024) return `${n} B`; if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`; if (n < 1024 * 1024 * 1024) return `${(n / 1024 / 1024).toFixed(1)} MB`; return `${(n / 1024 / 1024 / 1024).toFixed(1)} GB`; } function fmtTime(ms: number, ui: UITranslate): string { return new Date(ms).toLocaleString(ui("screen.91610255157661"), { year: "2-digit", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", }); } type EditState = { file: WorkspaceFile; content: string; dirty: boolean; saving: boolean; }; function WorkspaceInner() { const searchParams = useSearchParams(); const requestedPath = searchParams.get("path") ?? ""; const { ui, liveUI } = useUITranslation(); const [path, setPath] = React.useState(""); const [entries, setEntries] = React.useState([]); const [loading, setLoading] = React.useState(true); const [edit, setEdit] = React.useState(null); const [mkdirOpen, setMkdirOpen] = React.useState(false); const [mkdirName, setMkdirName] = React.useState(""); const uploadRef = React.useRef(null); const load = React.useCallback( (p: string) => { setLoading(true); api .workspaceList(p) .then((r) => { setEntries(r.entries); setPath(r.path); }) .catch((e) => toast.error(liveUI("screen.d9861c29979739", { v0: (e as Error).message }))) .finally(() => setLoading(false)); }, [liveUI], ); React.useEffect(() => { let alive = true; // A path may identify a file or a directory. Directory listing wins; // otherwise open the file and show its containing directory. void api .workspaceList(requestedPath) .then((result) => { if (!alive) return; setEntries(result.entries); setPath(result.path); setLoading(false); }) .catch(async () => { if (!alive) return; load(requestedPath.split("/").slice(0, -1).join("/")); try { const file = await api.workspaceRead(requestedPath); if (alive) setEdit({ file, content: file.content ?? "", dirty: false, saving: false }); } catch (error) { if (alive) toast.error((error as Error).message); } }); return () => { alive = false; }; }, [load, requestedPath]); const crumbs = React.useMemo(() => { const parts = path ? path.split("/") : []; const acc: { name: string; path: string }[] = [{ name: ui("screen.6fed7e86016823"), path: "" }]; let cur = ""; for (const part of parts) { cur = cur ? `${cur}/${part}` : part; acc.push({ name: part, path: cur }); } return acc; }, [path, ui]); const openFile = (e: WorkspaceEntry) => { api .workspaceRead(e.path) .then((f) => setEdit({ file: f, content: f.content ?? "", dirty: false, saving: false })) .catch((err) => toast.error(ui("screen.076ee27586c9a7", { v0: (err as Error).message }))); }; const saveFile = () => { if (!edit) return; setEdit({ ...edit, saving: true }); api .workspaceWrite(edit.file.path, edit.content) .then(() => { toast.success(ui("screen.1bd91a7d0c53c9")); setEdit((cur) => (cur ? { ...cur, dirty: false, saving: false } : cur)); load(path); }) .catch((err) => { toast.error(ui("screen.20e1b0f2590bc7", { v0: (err as Error).message })); setEdit((cur) => (cur ? { ...cur, saving: false } : cur)); }); }; const del = (e: WorkspaceEntry) => { if ( !window.confirm( ui("screen.3a786999516d49", { v0: e.dir ? ui("screen.52daa71ebc3105") : ui("screen.39932f24fe11a6"), v1: e.name, v2: e.dir ? ui("screen.ae5d8ac7afd3cb") : "", }), ) ) return; api .workspaceDelete(e.path) .then(() => { toast.success(ui("screen.077a6d37719a0e")); load(path); }) .catch((err) => toast.error(ui("screen.77dcc9d08e857c", { v0: (err as Error).message }))); }; const doUpload = (files: FileList | null) => { if (!files || files.length === 0) return; api .workspaceUpload(path, Array.from(files)) .then((r) => { toast.success(ui("screen.dc75e034e98100", { v0: r.uploaded })); load(path); }) .catch((err) => toast.error(ui("screen.a6794c150f204c", { v0: (err as Error).message }))) .finally(() => { if (uploadRef.current) uploadRef.current.value = ""; }); }; const doMkdir = () => { const name = mkdirName.trim(); if (!name) return; const target = path ? `${path}/${name}` : name; api .workspaceMkdir(target) .then(() => { toast.success(ui("screen.c28d7343946db1")); setMkdirOpen(false); setMkdirName(""); load(path); }) .catch((err) => toast.error(ui("screen.12a4969260f84f", { v0: (err as Error).message }))); }; return (
{/* 头部:面包屑 + 操作 */}
{crumbs.map((c, i) => ( {i > 0 && /} ))}
doUpload(e.target.files)} />
{ui("screen.d44e9b3d3b31d3")} {ui("screen.50db7447b966f5")} {ui("screen.257bbcc44c839d")} {ui("screen.ed31fbb483ee1b")} {entries.length === 0 && ( {loading ? ui("screen.4927a53bcc886a") : ui("screen.3ea0e6e41518ea")} )} {entries.map((e) => ( {e.dir ? "—" : fmtSize(e.size)} {fmtTime(e.mtime, ui)}
{!e.dir && ( )}
))}
{/* 文件查看 / 编辑 */} !o && setEdit(null)}> {edit && ( <> {edit.file.path} {fmtSize(edit.file.size)} {edit.file.binary || edit.file.too_large ? (

{edit.file.too_large ? ui("screen.15678c48f7988d") : ui("screen.c54a32ff99392f")}

) : ( <>