"use client"; import { ActionMenu, type ActionMenuItem } from "@suphark/ui/components/shared/action-menu"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { errorMessage } from "@suphark/ui/lib/action-result"; import { downloadBlob, downloadResponse } from "@suphark/ui/lib/download"; import { ChevronDownIcon, DownloadIcon } from "lucide-react"; import { type ReactElement, useState } from "react"; import { toast } from "sonner"; export type ExportFormat = | "xlsx" | "csv" | "pdf" | { value: string; label: string; extension?: string }; const BUILT_IN: Record<"xlsx" | "csv" | "pdf", { label: string; extension: string }> = { xlsx: { label: "Excel", extension: ".xlsx" }, csv: { label: "CSV", extension: ".csv" }, pdf: { label: "PDF", extension: ".pdf" }, }; export interface ExportMenuProps { formats: ReadonlyArray; /** * สร้างไฟล์ — คืน `Response` (ชื่อไฟล์จาก Content-Disposition) หรือ `Blob` (ใช้ `fileName`) แล้วแพ็กเกจดาวน์โหลดให้ * คืนอย่างอื่น = แอปจัดการเอง (เช่น เปิดงานส่งออกเบื้องหลัง) · throw / Response ไม่ ok = toast แจ้ง */ onExport: (format: string, options: { selectedOnly: boolean }) => unknown; /** จำนวนแถวที่จะส่งออก (หัวเมนู "248 รายการตามตัวกรอง") */ count?: number; /** จำนวนแถวที่เลือก — > 0 = มีตัวเลือก "เฉพาะแถวที่เลือก (n)" */ selectedCount?: number; /** ชื่อไฟล์ (ไม่รวมนามสกุล) เมื่อ `onExport` คืน Blob หรือ Response ไม่มี Content-Disposition */ fileName?: string; /** ทับปุ่มเปิดเมนู (ค่าเริ่มต้น ปุ่ม outline "ส่งออก ▾") */ trigger?: ReactElement; align?: "start" | "center" | "end"; sheetOnMobile?: boolean; } /** * เมนูส่งออกไฟล์ (Excel/CSV/PDF) + ดาวน์โหลดให้เอง — toast "กำลังเตรียมไฟล์…" ระหว่างรอ * * fetch(`/api/vendors/export?format=${format}&ids=…`)} /> */ export function ExportMenu({ formats, onExport, count, selectedCount = 0, fileName = "export", trigger, align = "end", sheetOnMobile, }: ExportMenuProps) { const text = useUiText(); const t = text.exportMenu; const [selectedOnly, setSelectedOnly] = useState(false); const [busy, setBusy] = useState(false); const onlySelected = selectedOnly && selectedCount > 0; const run = async (value: string, extension: string) => { setBusy(true); const id = toast.loading(t?.preparing ?? "กำลังเตรียมไฟล์…"); try { const result = await onExport(value, { selectedOnly: onlySelected }); let saved: string | null = null; if (result instanceof Response) saved = await downloadResponse(result, fileName + extension); else if (result instanceof Blob) { saved = fileName + extension; downloadBlob(result, saved); } if (saved) { toast.success((t?.done ?? "ดาวน์โหลด {name} แล้ว").replace("{name}", saved), { id }); } else toast.dismiss(id); } catch (err) { toast.error(errorMessage(err, t?.failed ?? "ส่งออกไม่สำเร็จ"), { id }); } finally { setBusy(false); } }; const items: ActionMenuItem[] = []; if (typeof count === "number") { items.push({ type: "label", label: (t?.rows ?? "{count} รายการตามตัวกรอง").replace("{count}", count.toLocaleString()), }); } for (const format of formats) { const f = typeof format === "string" ? { value: format, ...BUILT_IN[format] } : format; const extension = f.extension ?? `.${f.value}`; items.push({ label: ( <> {f.label} {extension} ), disabled: busy, onClick: () => run(f.value, extension), }); } if (selectedCount > 0) { items.push( { type: "separator" }, { type: "checkbox", label: ( {(t?.selectedOnly ?? "เฉพาะแถวที่เลือก ({count})").replace( "{count}", String(selectedCount), )} ), checked: selectedOnly, onCheckedChange: setSelectedOnly, }, ); } return ( {t?.button ?? "ส่งออก"} ) } /> ); }