| import { HAZARD_LABEL, type Cluster } from "./api"; | |
| export function clustersToCsv(clusters: Cluster[]): string { | |
| const header = [ | |
| "rank", | |
| "cluster_id", | |
| "hazard_type", | |
| "severity", | |
| "report_count", | |
| "status", | |
| "lat", | |
| "lon", | |
| "first_seen", | |
| "last_seen", | |
| ]; | |
| const rows = clusters.map((c, i) => [ | |
| i + 1, | |
| c.id, | |
| HAZARD_LABEL[c.hazard_type], | |
| c.severity, | |
| c.report_count, | |
| c.status, | |
| c.centroid_lat.toFixed(6), | |
| c.centroid_lon.toFixed(6), | |
| c.first_seen, | |
| c.last_seen, | |
| ]); | |
| return [header, ...rows] | |
| .map((r) => r.map((v) => `"${String(v).replace(/"/g, '""')}"`).join(",")) | |
| .join("\n"); | |
| } | |
| export function downloadCsv(filename: string, csv: string): void { | |
| const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement("a"); | |
| a.href = url; | |
| a.download = filename; | |
| a.click(); | |
| URL.revokeObjectURL(url); | |
| } | |