-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathExportButton.tsx
More file actions
195 lines (180 loc) · 6 KB
/
Copy pathExportButton.tsx
File metadata and controls
195 lines (180 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use client';
import React, { useState } from 'react';
import {
Download,
FileText,
Table,
Loader2,
ChevronRight,
FileJson,
} from 'lucide-react';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
DropdownMenuSub,
DropdownMenuSubTrigger,
DropdownMenuPortal,
DropdownMenuSubContent,
} from '@/components/ui/dropdown-menu';
import { Button } from '@/components/ui/button';
import { exportHackathon } from '@/lib/api/hackathons/rewards';
import { toast } from 'sonner';
interface ExportButtonProps {
organizationId: string;
hackathonId: string;
hackathonName?: string;
}
const DATASETS = [
{ id: 'full', label: 'Full Report', description: 'All sections included' },
{ id: 'overview', label: 'Overview', description: 'Metrics & status' },
{
id: 'participants',
label: 'Participants',
description: 'User list & contact',
},
{
id: 'submissions',
label: 'Submissions',
description: 'Project links & scores',
},
{ id: 'prize_tiers', label: 'Prize Tiers', description: 'Rewards & logic' },
{
id: 'winners',
label: 'Winners',
description: 'Wallet address, activation & USDC trustline',
},
{
id: 'judging',
label: 'Judging',
description: 'Results, judges, scores & comments',
},
] as const;
export function ExportButton({
organizationId,
hackathonId,
hackathonName = 'hackathon',
}: ExportButtonProps) {
const [isExporting, setIsExporting] = useState(false);
const handleExport = async (
format: 'csv' | 'pdf',
dataset: (typeof DATASETS)[number]['id']
) => {
if (isExporting) return;
setIsExporting(true);
const toastId = toast.loading(
`Preparing ${format.toUpperCase()} export...`
);
try {
const blob = await exportHackathon(
organizationId,
hackathonId,
format,
dataset
);
// Create a link and trigger download
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
const timestamp = new Date().toISOString().split('T')[0];
const filename = `${hackathonName.toLowerCase().replace(/\s+/g, '-')}-${dataset}-${timestamp}.${format}`;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
// Cleanup
link.parentNode?.removeChild(link);
window.URL.revokeObjectURL(url);
toast.success('Export downloaded successfully', { id: toastId });
} catch (error) {
console.error('Export failed:', error);
toast.error('Failed to export data. Please try again.', { id: toastId });
} finally {
setIsExporting(false);
}
};
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant='outline'
size='sm'
className='h-9 gap-2 border-zinc-800 bg-zinc-950 px-4 text-zinc-400 hover:bg-zinc-900 hover:text-white'
disabled={isExporting}
>
{isExporting ? (
<Loader2 className='h-4 w-4 animate-spin' />
) : (
<Download className='h-4 w-4' />
)}
<span>Export Data</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent
align='end'
className='w-56 border-zinc-800 bg-zinc-950 text-zinc-300'
>
<DropdownMenuLabel className='text-xs font-semibold tracking-wider text-zinc-500 uppercase'>
Choose Format
</DropdownMenuLabel>
<DropdownMenuSeparator className='bg-zinc-800' />
{/* CSV Export */}
<DropdownMenuSub>
<DropdownMenuSubTrigger className='gap-2 focus:bg-zinc-900 focus:text-white'>
<Table className='h-4 w-4 text-emerald-500' />
<span>CSV Spreadsheet</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent className='w-64 border-zinc-800 bg-zinc-950 text-zinc-300'>
<DropdownMenuLabel className='text-xs text-zinc-500'>
Include Dataset
</DropdownMenuLabel>
<DropdownMenuSeparator className='bg-zinc-800' />
{DATASETS.map(ds => (
<DropdownMenuItem
key={ds.id}
className='flex flex-col items-start gap-1 py-2 focus:bg-zinc-900 focus:text-white'
onClick={() => handleExport('csv', ds.id)}
>
<span className='font-medium'>{ds.label}</span>
<span className='text-[10px] text-zinc-500'>
{ds.description}
</span>
</DropdownMenuItem>
))}
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
{/* PDF Export */}
<DropdownMenuSub>
<DropdownMenuSubTrigger className='gap-2 focus:bg-zinc-900 focus:text-white'>
<FileText className='h-4 w-4 text-rose-500' />
<span>Branded PDF</span>
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent className='w-64 border-zinc-800 bg-zinc-950 text-zinc-300'>
<DropdownMenuLabel className='text-xs text-zinc-500'>
Include Dataset
</DropdownMenuLabel>
<DropdownMenuSeparator className='bg-zinc-800' />
{DATASETS.map(ds => (
<DropdownMenuItem
key={ds.id}
className='flex flex-col items-start gap-1 py-2 focus:bg-zinc-900 focus:text-white'
onClick={() => handleExport('pdf', ds.id)}
>
<span className='font-medium'>{ds.label}</span>
<span className='text-[10px] text-zinc-500'>
{ds.description}
</span>
</DropdownMenuItem>
))}
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
</DropdownMenuContent>
</DropdownMenu>
);
}