"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { focusAfterRemove } from "@suphark/ui/lib/focus-after-remove"; import { formatBytes } from "@suphark/ui/lib/formatters"; import { cn } from "@suphark/ui/lib/utils"; import { AlertCircle, Check, ChevronDown, RotateCcw, X } from "lucide-react"; import { type ReactNode, useState } from "react"; import { FileTypeIcon } from "./file-type-icon"; import { ProgressBar } from "./progress-bar"; export type UploadStatus = "queued" | "uploading" | "done" | "error" | "aborted"; export interface UploadItem { id: string; name: string; sizeBytes: number; /** 0–1 */ progress: number; status: UploadStatus; /** ข้อความเมื่อ status = error */ error?: string; /** mime สำหรับไอคอน (ไม่ส่ง = เดาจากนามสกุล) */ mime?: string; } export interface UploadListText { title: string; /** สรุป "x/y เสร็จ" */ summary: (done: number, total: number) => string; allDone: string; queued: string; aborted: string; cancel: string; retry: string; clearDone: string; collapse: string; expand: string; } const TEXT: Record<"th" | "en", UploadListText> = { th: { title: "อัปโหลด", summary: (d, t) => `${d}/${t} เสร็จ`, allDone: "อัปโหลดเสร็จทั้งหมด", queued: "รอคิว", aborted: "ยกเลิกแล้ว", cancel: "ยกเลิก", retry: "ลองใหม่", clearDone: "ล้างรายการที่เสร็จ", collapse: "ยุบ", expand: "ขยาย", }, en: { title: "Uploads", summary: (d, t) => `${d}/${t} done`, allDone: "All uploads complete", queued: "Queued", aborted: "Cancelled", cancel: "Cancel", retry: "Retry", clearDone: "Clear completed", collapse: "Collapse", expand: "Expand", }, }; export interface UploadListProps { items: UploadItem[]; onCancel?: (id: string) => void; onRetry?: (id: string) => void; /** แสดงปุ่มล้างรายการที่เสร็จ (done/aborted) */ onClearDone?: () => void; /** ปุ่มปิดทั้งแผง (เช่นเมื่อเสร็จหมดแล้ว) */ onClose?: () => void; /** ลอยมุมล่างขวาแบบ Google Drive */ floating?: boolean; defaultCollapsed?: boolean; title?: ReactNode; language?: "th" | "en"; text?: Partial; className?: string; } /** * รายการอัปโหลดพร้อม progress ต่อไฟล์ + สรุปรวม (x/y เสร็จ) ยุบ/ขยายได้ — ใช้ `ProgressBar` และ `FileTypeIcon` * แอปเป็นคนคุมคิว/XHR เอง ส่งเฉพาะ `items` เข้ามา (ยกเลิก/ลองใหม่ยิง callback กลับ) */ export function UploadList({ items, onCancel, onRetry, onClearDone, onClose, floating = false, defaultCollapsed = false, title, language = "th", text: textProp, className, }: UploadListProps) { const text: UploadListText = { ...TEXT[language], ...textProp }; const [collapsed, setCollapsed] = useState(defaultCollapsed); const total = items.length; const done = items.filter((i) => i.status === "done").length; const settled = items.every((i) => i.status === "done" || i.status === "aborted"); const hasClearable = items.some((i) => i.status === "done" || i.status === "aborted"); if (total === 0) return null; return (

{title ?? text.title}

{settled && done === total ? text.allDone : text.summary(done, total)}

{onClearDone && hasClearable && ( )} {onClose && ( )}
{!collapsed && ( )}
); }