sitcod3 commited on
Commit
a0057f9
·
verified ·
1 Parent(s): 5cb81c1

Upload src/components/CsvExportButton.tsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/components/CsvExportButton.tsx +47 -0
src/components/CsvExportButton.tsx ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Download } from "lucide-react";
2
+
3
+ interface CsvExportButtonProps {
4
+ data: any[];
5
+ filename?: string;
6
+ className?: string;
7
+ }
8
+
9
+ export default function CsvExportButton({
10
+ data,
11
+ filename = "smartlead-export.csv",
12
+ className = "px-3 py-1.5 bg-zinc-800 hover:bg-zinc-700 text-zinc-300 text-xs font-medium rounded-lg transition-colors flex items-center gap-1.5 border border-zinc-700",
13
+ }: CsvExportButtonProps) {
14
+ const exportCsv = () => {
15
+ if (!data || data.length === 0) return;
16
+
17
+ const headers = Object.keys(data[0]);
18
+ const rows = data.map((item) =>
19
+ headers
20
+ .map((header) => {
21
+ const val = item[header];
22
+ if (typeof val === "object") {
23
+ return `"${JSON.stringify(val).replace(/"/g, '""')}"`;
24
+ }
25
+ return `"${String(val ?? "").replace(/"/g, '""')}"`;
26
+ })
27
+ .join(",")
28
+ );
29
+
30
+ const csvContent = [headers.join(","), ...rows].join("\n");
31
+ const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
32
+ const url = URL.createObjectURL(blob);
33
+ const link = document.createElement("a");
34
+ link.setAttribute("href", url);
35
+ link.setAttribute("download", filename);
36
+ document.body.appendChild(link);
37
+ link.click();
38
+ document.body.removeChild(link);
39
+ };
40
+
41
+ return (
42
+ <button type="button" onClick={exportCsv} className={className}>
43
+ <Download className="w-3.5 h-3.5" />
44
+ Export CSV
45
+ </button>
46
+ );
47
+ }