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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
import {
faCheck,
faExclamationTriangle,
faFileImport,
faSpinner,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { type ChangeEvent, useCallback, useEffect, useState } from "react";
import { ApiClientError, apiClient } from "../api";
import { parseCSV } from "../utils/csvParser";
interface NoteField {
id: string;
name: string;
order: number;
}
interface NoteType {
id: string;
name: string;
frontTemplate: string;
backTemplate: string;
isReversible: boolean;
fields: NoteField[];
}
interface ImportNotesModalProps {
isOpen: boolean;
deckId: string;
onClose: () => void;
onImportComplete: () => void;
}
type ImportPhase =
| "upload"
| "validating"
| "preview"
| "importing"
| "complete";
interface ValidationError {
rowNumber: number;
message: string;
}
interface ValidatedRow {
rowNumber: number;
noteTypeId: string;
noteTypeName: string;
fields: Record<string, string>;
preview: Record<string, string>;
}
interface ImportResult {
created: number;
failed: { index: number; error: string }[];
}
export function ImportNotesModal({
isOpen,
deckId,
onClose,
onImportComplete,
}: ImportNotesModalProps) {
const [phase, setPhase] = useState<ImportPhase>("upload");
const [error, setError] = useState<string | null>(null);
const [noteTypes, setNoteTypes] = useState<NoteType[]>([]);
const [validatedRows, setValidatedRows] = useState<ValidatedRow[]>([]);
const [validationErrors, setValidationErrors] = useState<ValidationError[]>(
[],
);
const [importResult, setImportResult] = useState<ImportResult | null>(null);
const fetchNoteTypes = useCallback(async () => {
try {
const res = await apiClient.rpc.api["note-types"].$get();
const data = await apiClient.handleResponse<{
noteTypes: { id: string; name: string }[];
}>(res);
// Fetch details for each note type to get fields
const noteTypesWithFields: NoteType[] = [];
for (const nt of data.noteTypes) {
const detailRes = await apiClient.rpc.api["note-types"][":id"].$get({
param: { id: nt.id },
});
if (detailRes.ok) {
const detailData = await apiClient.handleResponse<{
noteType: NoteType;
}>(detailRes);
noteTypesWithFields.push(detailData.noteType);
}
}
setNoteTypes(noteTypesWithFields);
} catch (err) {
if (err instanceof ApiClientError) {
setError(err.message);
} else {
setError("Failed to load note types. Please try again.");
}
}
}, []);
useEffect(() => {
if (isOpen && noteTypes.length === 0) {
fetchNoteTypes();
}
}, [isOpen, noteTypes.length, fetchNoteTypes]);
const resetState = () => {
setPhase("upload");
setError(null);
setValidatedRows([]);
setValidationErrors([]);
setImportResult(null);
};
const handleClose = () => {
resetState();
onClose();
};
const handleFileChange = async (e: ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
setPhase("validating");
setError(null);
setValidationErrors([]);
setValidatedRows([]);
try {
const content = await file.text();
const parseResult = parseCSV(content);
if (!parseResult.success) {
setError(parseResult.error.message);
setPhase("upload");
return;
}
const { headers, rows } = parseResult.data;
// Validate headers: must have deck, note_type, and at least one field
if (headers.length < 3) {
setError(
"CSV must have at least 3 columns: deck, note_type, and field(s)",
);
setPhase("upload");
return;
}
if (headers[0] !== "deck" || headers[1] !== "note_type") {
setError("First two columns must be 'deck' and 'note_type'");
setPhase("upload");
return;
}
const fieldNames = headers.slice(2);
const validated: ValidatedRow[] = [];
const errors: ValidationError[] = [];
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
if (!row) continue;
const rowNumber = i + 2; // +2 because 1-indexed and header is row 1
const noteTypeName = row.note_type ?? "";
const noteType = noteTypes.find(
(nt) => nt.name.toLowerCase() === noteTypeName.toLowerCase(),
);
if (!noteType) {
errors.push({
rowNumber,
message: `Note type "${noteTypeName}" not found`,
});
continue;
}
// Map field names to field type IDs
const fields: Record<string, string> = {};
const preview: Record<string, string> = {};
let fieldError = false;
for (const fieldName of fieldNames) {
const fieldType = noteType.fields.find(
(f) => f.name.toLowerCase() === fieldName.toLowerCase(),
);
if (!fieldType) {
errors.push({
rowNumber,
message: `Field "${fieldName}" not found in note type "${noteTypeName}"`,
});
fieldError = true;
break;
}
const value = row[fieldName] ?? "";
fields[fieldType.id] = value;
preview[fieldName] = value;
}
if (fieldError) continue;
validated.push({
rowNumber,
noteTypeId: noteType.id,
noteTypeName: noteType.name,
fields,
preview,
});
}
setValidatedRows(validated);
setValidationErrors(errors);
setPhase("preview");
} catch (err) {
setError(err instanceof Error ? err.message : "Failed to parse CSV");
setPhase("upload");
}
// Reset file input
e.target.value = "";
};
const handleImport = async () => {
if (validatedRows.length === 0) return;
setPhase("importing");
setError(null);
try {
const res = await apiClient.rpc.api.decks[":deckId"].notes.import.$post({
param: { deckId },
json: {
notes: validatedRows.map((row) => ({
noteTypeId: row.noteTypeId,
fields: row.fields,
})),
},
});
const result = await apiClient.handleResponse<ImportResult>(res);
setImportResult(result);
setPhase("complete");
onImportComplete();
} catch (err) {
if (err instanceof ApiClientError) {
setError(err.message);
} else {
setError("Failed to import notes. Please try again.");
}
setPhase("preview");
}
};
if (!isOpen) return null;
return (
<div
role="dialog"
aria-modal="true"
aria-labelledby="import-notes-title"
className="fixed inset-0 bg-ink/40 backdrop-blur-sm flex items-center justify-center z-50 p-4 animate-fade-in"
onClick={(e) => {
if (e.target === e.currentTarget && phase !== "importing") {
handleClose();
}
}}
onKeyDown={(e) => {
if (e.key === "Escape" && phase !== "importing") {
handleClose();
}
}}
>
<div className="bg-white rounded-2xl shadow-xl w-full max-w-lg animate-scale-in max-h-[90vh] flex flex-col">
<div className="p-6 flex-shrink-0">
<h2
id="import-notes-title"
className="font-display text-xl font-medium text-ink mb-4"
>
Import Notes from CSV
</h2>
{error && (
<div
role="alert"
className="bg-error/5 text-error text-sm px-4 py-3 rounded-lg border border-error/20 mb-4"
>
{error}
</div>
)}
{/* Phase: Upload */}
{phase === "upload" && (
<div className="space-y-4">
<div className="border-2 border-dashed border-border rounded-lg p-8 text-center">
<FontAwesomeIcon
icon={faFileImport}
className="w-10 h-10 text-muted mb-4"
/>
<p className="text-slate mb-4">Select a CSV file to import</p>
<label className="inline-flex items-center gap-2 bg-primary hover:bg-primary-dark text-white font-medium py-2 px-4 rounded-lg cursor-pointer transition-all duration-200">
<span>Choose File</span>
<input
type="file"
accept=".csv,text/csv"
onChange={handleFileChange}
className="hidden"
/>
</label>
</div>
<div className="bg-ivory rounded-lg px-4 py-3 text-sm text-muted">
<p className="font-medium text-slate mb-1">Expected format:</p>
<code className="text-xs">
deck,note_type,Front,Back
<br />
MyDeck,Basic,hello,world
</code>
</div>
</div>
)}
{/* Phase: Validating */}
{phase === "validating" && (
<div className="flex items-center justify-center py-8">
<FontAwesomeIcon
icon={faSpinner}
className="w-8 h-8 text-primary animate-spin"
/>
<span className="ml-3 text-muted">Validating CSV...</span>
</div>
)}
</div>
{/* Phase: Preview - scrollable content */}
{phase === "preview" && (
<div className="flex-1 overflow-y-auto px-6">
{validationErrors.length > 0 && (
<div className="bg-warning/5 border border-warning/20 rounded-lg p-4 mb-4">
<div className="flex items-center gap-2 text-warning font-medium mb-2">
<FontAwesomeIcon icon={faExclamationTriangle} />
{validationErrors.length} error(s) found
</div>
<ul className="text-sm text-slate space-y-1 max-h-32 overflow-y-auto">
{validationErrors.map((err) => (
<li key={err.rowNumber}>
Row {err.rowNumber}: {err.message}
</li>
))}
</ul>
</div>
)}
{validatedRows.length > 0 && (
<div className="space-y-4">
<p className="text-slate">
<span className="font-medium">{validatedRows.length}</span>{" "}
note(s) ready to import
</p>
{/* Preview table */}
<div className="border border-border rounded-lg overflow-hidden">
<div className="max-h-48 overflow-y-auto">
<table className="w-full text-sm">
<thead className="bg-ivory sticky top-0">
<tr>
<th className="px-3 py-2 text-left font-medium text-slate">
#
</th>
<th className="px-3 py-2 text-left font-medium text-slate">
Type
</th>
<th className="px-3 py-2 text-left font-medium text-slate">
Preview
</th>
</tr>
</thead>
<tbody className="divide-y divide-border">
{validatedRows.slice(0, 10).map((row) => (
<tr key={row.rowNumber}>
<td className="px-3 py-2 text-muted">
{row.rowNumber}
</td>
<td className="px-3 py-2 text-slate">
{row.noteTypeName}
</td>
<td className="px-3 py-2 text-slate truncate max-w-[200px]">
{Object.values(row.preview).join(" | ")}
</td>
</tr>
))}
</tbody>
</table>
</div>
{validatedRows.length > 10 && (
<div className="bg-ivory px-3 py-2 text-xs text-muted text-center">
...and {validatedRows.length - 10} more
</div>
)}
</div>
</div>
)}
</div>
)}
{/* Phase: Importing */}
{phase === "importing" && (
<div className="px-6 flex items-center justify-center py-8">
<FontAwesomeIcon
icon={faSpinner}
className="w-8 h-8 text-primary animate-spin"
/>
<span className="ml-3 text-muted">Importing notes...</span>
</div>
)}
{/* Phase: Complete */}
{phase === "complete" && importResult && (
<div className="px-6">
<div className="text-center py-4">
<div className="w-14 h-14 mx-auto mb-4 bg-success/10 rounded-full flex items-center justify-center">
<FontAwesomeIcon
icon={faCheck}
className="w-7 h-7 text-success"
/>
</div>
<p className="text-lg font-medium text-slate mb-2">
Import complete!
</p>
<p className="text-muted">
{importResult.created} note(s) imported successfully
</p>
{importResult.failed.length > 0 && (
<div className="mt-4 bg-error/5 border border-error/20 rounded-lg p-4 text-left">
<p className="text-error font-medium mb-2">
{importResult.failed.length} failed:
</p>
<ul className="text-sm text-slate space-y-1 max-h-24 overflow-y-auto">
{importResult.failed.map((f) => (
<li key={f.index}>
Row {validatedRows[f.index]?.rowNumber ?? f.index + 1}:{" "}
{f.error}
</li>
))}
</ul>
</div>
)}
</div>
</div>
)}
{/* Footer buttons */}
<div className="p-6 flex-shrink-0 border-t border-border mt-auto">
<div className="flex gap-3 justify-end">
{phase === "upload" && (
<button
type="button"
onClick={handleClose}
className="px-4 py-2 text-slate hover:bg-ivory rounded-lg transition-colors"
>
Cancel
</button>
)}
{phase === "preview" && (
<>
<button
type="button"
onClick={resetState}
className="px-4 py-2 text-slate hover:bg-ivory rounded-lg transition-colors"
>
Back
</button>
<button
type="button"
onClick={handleImport}
disabled={validatedRows.length === 0}
className="px-4 py-2 bg-primary hover:bg-primary-dark text-white font-medium rounded-lg transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
>
Import {validatedRows.length} Note(s)
</button>
</>
)}
{phase === "complete" && (
<button
type="button"
onClick={handleClose}
className="px-4 py-2 bg-primary hover:bg-primary-dark text-white font-medium rounded-lg transition-all duration-200"
>
Done
</button>
)}
</div>
</div>
</div>
</div>
);
}
|