| 15 | } |
| 16 | |
| 17 | async function triggerDownload(endpoint: string, format: "csv" | "json", filename: string) { |
| 18 | const separator = endpoint.includes("?") ? "&" : "?"; |
| 19 | const url = `${endpoint}${separator}format=${format}`; |
| 20 | const res = await fetch(url); |
| 21 | if (!res.ok) throw new Error("Export failed"); |
| 22 | const blob = await res.blob(); |
| 23 | const a = document.createElement("a"); |
| 24 | a.href = URL.createObjectURL(blob); |
| 25 | a.download = `${filename}.${format}`; |
| 26 | document.body.appendChild(a); |
| 27 | a.click(); |
| 28 | a.remove(); |
| 29 | URL.revokeObjectURL(a.href); |
| 30 | } |
| 31 | |
| 32 | export function ExportButton({ endpoint, filename, label = "Export" }: ExportButtonProps) { |
| 33 | const [isExporting, setIsExporting] = useState(false); |