forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportResultsDisplay.tsx
More file actions
343 lines (322 loc) · 13.3 KB
/
Copy pathImportResultsDisplay.tsx
File metadata and controls
343 lines (322 loc) · 13.3 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'use client';
import React, { useState } from 'react';
import {
CheckCircle,
AlertCircle,
XCircle,
ChevronDown,
ChevronUp,
Download,
} from 'lucide-react';
import type { ImportSummary } from './CSVUploadDialog';
interface ImportResultsDisplayProps {
summary: ImportSummary | null;
onClose: () => void;
}
export function ImportResultsDisplay({
summary,
onClose,
}: ImportResultsDisplayProps) {
const [expandedSections, setExpandedSections] = useState({
created: true,
failed: true,
skipped: true,
});
if (!summary) return null;
const toggleSection = (
section: keyof typeof expandedSections
) => {
setExpandedSections((prev) => ({
...prev,
[section]: !prev[section],
}));
};
const downloadResultsAsJSON = () => {
const dataStr = JSON.stringify(summary, null, 2);
const dataBlob = new Blob([dataStr], { type: 'application/json' });
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = `import-results-${Date.now()}.json`;
link.click();
URL.revokeObjectURL(url);
};
const downloadErrorsAsCSV = () => {
const allErrors = [...summary.failed, ...summary.skipped];
if (allErrors.length === 0) return;
const headers = ['Row', 'Field', 'Message'];
const rows = allErrors.map((error) => [
error.row,
error.field || 'N/A',
error.message,
]);
const csv =
[headers, ...rows]
.map((row) =>
row
.map((cell) =>
typeof cell === 'string' && cell.includes(',')
? `"${cell.replace(/"/g, '""')}"`
: cell
)
.join(',')
)
.join('\n');
const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `import-errors-${Date.now()}.csv`;
link.click();
URL.revokeObjectURL(url);
};
const hasErrors = summary.failedCount > 0 || summary.skippedCount > 0;
const successRate = summary.totalRows > 0
? ((summary.createdCount / summary.totalRows) * 100).toFixed(1)
: 0;
return (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4 overflow-y-auto">
<div className="bg-white dark:bg-slate-900 rounded-lg shadow-lg max-w-2xl w-full my-8">
{/* Header */}
<div className="p-6 border-b border-gray-200 dark:border-gray-700">
<div className="flex items-start justify-between">
<div>
<h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-2">
Import Complete
</h2>
<p className="text-sm text-gray-600 dark:text-gray-400">
{summary.totalRows} total rows processed
</p>
</div>
{!hasErrors && (
<div className="flex items-center gap-2 px-3 py-1 bg-green-100 dark:bg-green-900/30 rounded-full">
<CheckCircle className="h-4 w-4 text-green-600 dark:text-green-400" />
<span className="text-xs font-medium text-green-700 dark:text-green-300">
Success
</span>
</div>
)}
</div>
</div>
{/* Body */}
<div className="p-6 space-y-6 max-h-[60vh] overflow-y-auto">
{/* Summary Stats */}
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
{/* Created */}
<div className="bg-green-50 dark:bg-green-900/20 rounded-lg p-4">
<div className="flex items-center gap-2 mb-1">
<CheckCircle className="h-4 w-4 text-green-600 dark:text-green-400" />
<p className="text-xs font-medium text-gray-600 dark:text-gray-400">
Created
</p>
</div>
<p className="text-2xl font-bold text-green-700 dark:text-green-300">
{summary.createdCount}
</p>
</div>
{/* Success Rate */}
<div className="bg-blue-50 dark:bg-blue-900/20 rounded-lg p-4">
<div className="flex items-center gap-2 mb-1">
<CheckCircle className="h-4 w-4 text-blue-600 dark:text-blue-400" />
<p className="text-xs font-medium text-gray-600 dark:text-gray-400">
Success Rate
</p>
</div>
<p className="text-2xl font-bold text-blue-700 dark:text-blue-300">
{successRate}%
</p>
</div>
{/* Skipped */}
{summary.skippedCount > 0 && (
<div className="bg-yellow-50 dark:bg-yellow-900/20 rounded-lg p-4">
<div className="flex items-center gap-2 mb-1">
<AlertCircle className="h-4 w-4 text-yellow-600 dark:text-yellow-400" />
<p className="text-xs font-medium text-gray-600 dark:text-gray-400">
Skipped
</p>
</div>
<p className="text-2xl font-bold text-yellow-700 dark:text-yellow-300">
{summary.skippedCount}
</p>
</div>
)}
{/* Failed */}
{summary.failedCount > 0 && (
<div className="bg-red-50 dark:bg-red-900/20 rounded-lg p-4">
<div className="flex items-center gap-2 mb-1">
<XCircle className="h-4 w-4 text-red-600 dark:text-red-400" />
<p className="text-xs font-medium text-gray-600 dark:text-gray-400">
Failed
</p>
</div>
<p className="text-2xl font-bold text-red-700 dark:text-red-300">
{summary.failedCount}
</p>
</div>
)}
</div>
{/* Created Rows Section */}
{summary.createdCount > 0 && (
<div>
<button
onClick={() => toggleSection('created')}
className="w-full flex items-center justify-between p-3 bg-green-50 dark:bg-green-900/20 rounded-lg hover:bg-green-100 dark:hover:bg-green-900/30 transition-colors"
>
<div className="flex items-center gap-2">
<CheckCircle className="h-5 w-5 text-green-600 dark:text-green-400" />
<span className="font-medium text-green-900 dark:text-green-200">
Successfully Created ({summary.createdCount})
</span>
</div>
{expandedSections.created ? (
<ChevronUp className="h-5 w-5" />
) : (
<ChevronDown className="h-5 w-5" />
)}
</button>
{expandedSections.created && (
<div className="mt-2 space-y-2">
{summary.created.map((row) => (
<div
key={`${row.row}-${row.id}`}
className="p-3 bg-green-50 dark:bg-green-900/20 rounded text-sm border border-green-200 dark:border-green-900/50"
>
<div className="flex items-start justify-between">
<div>
<p className="font-medium text-green-900 dark:text-green-200">
Row {row.row}: {row.invoiceNumber}
</p>
<p className="text-xs text-green-700 dark:text-green-300 mt-1 font-mono">
ID: {row.id}
</p>
</div>
</div>
</div>
))}
</div>
)}
</div>
)}
{/* Skipped Rows Section */}
{summary.skippedCount > 0 && (
<div>
<button
onClick={() => toggleSection('skipped')}
className="w-full flex items-center justify-between p-3 bg-yellow-50 dark:bg-yellow-900/20 rounded-lg hover:bg-yellow-100 dark:hover:bg-yellow-900/30 transition-colors"
>
<div className="flex items-center gap-2">
<AlertCircle className="h-5 w-5 text-yellow-600 dark:text-yellow-400" />
<span className="font-medium text-yellow-900 dark:text-yellow-200">
Validation Errors - Skipped ({summary.skippedCount})
</span>
</div>
{expandedSections.skipped ? (
<ChevronUp className="h-5 w-5" />
) : (
<ChevronDown className="h-5 w-5" />
)}
</button>
{expandedSections.skipped && (
<div className="mt-2 space-y-2">
{summary.skipped.map((error, idx) => (
<div
key={`${error.row}-${idx}`}
className="p-3 bg-yellow-50 dark:bg-yellow-900/20 rounded text-sm border border-yellow-200 dark:border-yellow-900/50"
>
<p className="font-medium text-yellow-900 dark:text-yellow-200">
Row {error.row}
{error.field && ` • Field: ${error.field}`}
</p>
<p className="text-yellow-700 dark:text-yellow-300 mt-1">
{error.message}
</p>
</div>
))}
</div>
)}
</div>
)}
{/* Failed Rows Section */}
{summary.failedCount > 0 && (
<div>
<button
onClick={() => toggleSection('failed')}
className="w-full flex items-center justify-between p-3 bg-red-50 dark:bg-red-900/20 rounded-lg hover:bg-red-100 dark:hover:bg-red-900/30 transition-colors"
>
<div className="flex items-center gap-2">
<XCircle className="h-5 w-5 text-red-600 dark:text-red-400" />
<span className="font-medium text-red-900 dark:text-red-200">
Database Errors - Failed ({summary.failedCount})
</span>
</div>
{expandedSections.failed ? (
<ChevronUp className="h-5 w-5" />
) : (
<ChevronDown className="h-5 w-5" />
)}
</button>
{expandedSections.failed && (
<div className="mt-2 space-y-2">
{summary.failed.map((error, idx) => (
<div
key={`${error.row}-${idx}`}
className="p-3 bg-red-50 dark:bg-red-900/20 rounded text-sm border border-red-200 dark:border-red-900/50"
>
<p className="font-medium text-red-900 dark:text-red-200">
Row {error.row}
{error.field && ` • Field: ${error.field}`}
</p>
<p className="text-red-700 dark:text-red-300 mt-1">
{error.message}
</p>
</div>
))}
</div>
)}
</div>
)}
{/* Info Box */}
<div className="bg-blue-50 dark:bg-blue-900/20 rounded-lg p-4 text-sm text-gray-600 dark:text-gray-400">
<p className="font-medium mb-2">About the Results</p>
<ul className="list-disc list-inside space-y-1 text-xs">
<li>
<strong>Created:</strong> Invoices successfully imported and saved to database
</li>
<li>
<strong>Skipped:</strong> Rows with validation errors (e.g., invalid email, invalid amount)
</li>
<li>
<strong>Failed:</strong> Rows that passed validation but failed to save (e.g., duplicate invoice number)
</li>
</ul>
</div>
</div>
{/* Footer */}
<div className="flex gap-2 p-4 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-slate-800 flex-wrap">
{(summary.failedCount > 0 || summary.skippedCount > 0) && (
<button
onClick={downloadErrorsAsCSV}
className="flex items-center gap-2 px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
>
<Download className="h-4 w-4" />
Download Errors
</button>
)}
<button
onClick={downloadResultsAsJSON}
className="flex items-center gap-2 px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-200 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded hover:bg-gray-50 dark:hover:bg-gray-600 transition-colors"
>
<Download className="h-4 w-4" />
Download Results
</button>
<button
onClick={onClose}
className="ml-auto px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded hover:bg-blue-700 transition-colors"
>
Close
</button>
</div>
</div>
</div>
);
}