-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvFlashcardsPage.jsx
More file actions
323 lines (295 loc) · 11.6 KB
/
CsvFlashcardsPage.jsx
File metadata and controls
323 lines (295 loc) · 11.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
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
import React, { useState, useRef } from 'react';
import { Link } from 'react-router-dom';
import {
ArrowLeftIcon,
UploadSimpleIcon,
ArrowRightIcon,
ShuffleIcon,
ArrowCounterClockwiseIcon,
CardsIcon,
} from '@phosphor-icons/react';
import { useToast } from '../../hooks/useToast';
import Seo from '../../components/Seo';
import GenerativeArt from '../../components/GenerativeArt';
import LuxeArt from '../../components/LuxeArt';
import BreadcrumbTitle from '../../components/BreadcrumbTitle';
import { motion } from 'framer-motion';
function CsvFlashcardsPage() {
const [cards, setCards] = useState([]);
const [currentIndex, setCurrentIndex] = useState(0);
const [isFlipped, setIsFlipped] = useState(false);
const [fileName, setFileName] = useState('');
const fileInputRef = useRef(null);
const { addToast } = useToast();
const handleFileUpload = (event) => {
const file = event.target.files[0];
if (!file) return;
if (file.type !== 'text/csv' && !file.name.endsWith('.csv')) {
addToast({
title: 'Invalid File',
message: 'Please upload a valid CSV file.',
type: 'error',
});
return;
}
setFileName(file.name);
const reader = new FileReader();
reader.onload = (e) => {
const text = e.target.result;
parseCSV(text);
};
reader.readAsText(file);
};
const parseCSV = (text) => {
try {
// Basic CSV parser
const lines = text.split(/\r?\n/).filter((line) => line.trim() !== '');
if (lines.length < 1) {
throw new Error('File is empty');
}
const parsedCards = lines
.map((line, index) => {
// Handle basic comma separation, respecting quotes could be added here if needed
// For now, simple split, assuming no commas in content or handle simple cases
const parts = line.split(',');
// If we have more than 2 parts, join the rest for the answer or question?
// Let's assume Col 1 = Question, Col 2 = Answer.
if (parts.length < 2) return null;
const question = parts[0].trim();
const answer = parts.slice(1).join(',').trim(); // Re-join rest in case answer had commas
return { id: index, question, answer };
})
.filter((card) => card !== null);
if (parsedCards.length === 0) {
throw new Error('No valid cards found');
}
setCards(parsedCards);
setCurrentIndex(0);
setIsFlipped(false);
addToast({
title: 'Success',
message: `Loaded ${parsedCards.length} cards.`,
type: 'success',
});
} catch (error) {
console.error(error);
addToast({
title: 'Error',
message: 'Failed to parse CSV. Ensure format is: Question,Answer',
type: 'error',
});
setCards([]);
}
};
const handleNext = () => {
setIsFlipped(false);
setTimeout(() => {
setCurrentIndex((prev) => (prev + 1) % cards.length);
}, 300);
};
const handlePrev = () => {
setIsFlipped(false);
setTimeout(() => {
setCurrentIndex((prev) => (prev - 1 + cards.length) % cards.length);
}, 300);
};
const handleShuffle = () => {
const shuffled = [...cards].sort(() => Math.random() - 0.5);
setCards(shuffled);
setCurrentIndex(0);
setIsFlipped(false);
addToast({
title: 'Shuffled',
message: 'Deck shuffled.',
duration: 2000,
});
};
const handleReset = () => {
setCards([]);
setFileName('');
setCurrentIndex(0);
setIsFlipped(false);
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
};
const handleFlip = () => {
setIsFlipped(!isFlipped);
};
return (
<div className="min-h-screen bg-[#050505] text-white selection:bg-indigo-500/30 font-sans">
<Seo
title="CSV Flashcards | Fezcodex"
description="Study tool. Load custom flashcards via CSV."
keywords={['Fezcodex', 'Flashcards', 'CSV', 'Study', 'Memory']}
/>
<div className="mx-auto max-w-5xl px-6 py-24 md:px-12">
<header className="mb-12">
<Link
to="/apps"
className="mb-8 inline-flex items-center gap-2 text-xs font-mono text-gray-500 hover:text-white transition-colors uppercase tracking-widest"
>
<ArrowLeftIcon weight="bold" />
<span>Applications</span>
</Link>
<div className="flex flex-col md:flex-row md:items-end justify-between gap-12">
<div className="space-y-4">
<BreadcrumbTitle
title="Flashcard Forge"
slug="csv-deck"
variant="brutalist"
/>
<p className="text-xl text-gray-400 max-w-2xl font-light leading-relaxed">
Memory retention protocol. Upload structured CSV data
(Question,Answer) to initiate a recall session.
</p>
</div>
</div>
</header>
{cards.length === 0 ? (
<div
className="border border-dashed border-white/20 bg-white/[0.02] rounded-sm p-12 text-center hover:bg-white/[0.04] transition-colors group cursor-pointer"
onClick={() => fileInputRef.current.click()}
>
<input
type="file"
accept=".csv"
className="hidden"
ref={fileInputRef}
onChange={handleFileUpload}
/>
<div className="mb-6 flex justify-center">
<div className="p-4 bg-indigo-500/10 rounded-full group-hover:bg-indigo-500/20 transition-colors">
<UploadSimpleIcon size={48} className="text-indigo-400" />
</div>
</div>
<h3 className="text-lg font-bold mb-2">Upload CSV Deck</h3>
<p className="text-gray-500 text-sm max-w-sm mx-auto">
Drag and drop or click to select a file. <br />
Format:{' '}
<span className="font-mono text-indigo-300">
Question,Answer
</span>{' '}
(No header required)
</p>
</div>
) : (
<div className="space-y-8">
{/* Controls Header */}
<div className="flex flex-wrap items-center justify-between gap-4 p-4 border border-white/10 bg-white/[0.02]">
<div className="flex items-center gap-4 text-sm font-mono text-gray-400">
<CardsIcon size={20} />
<span>{fileName}</span>
<span className="w-px h-4 bg-white/10" />
<span className="text-white">
Card {currentIndex + 1}{' '}
<span className="text-gray-600">/</span> {cards.length}
</span>
</div>
<div className="flex gap-2">
<button
onClick={handleShuffle}
className="p-2 hover:bg-white/10 rounded-sm text-gray-400 hover:text-white transition-colors"
title="Shuffle Deck"
>
<ShuffleIcon size={20} />
</button>{' '}
<button
onClick={handleReset}
className="p-2 hover:bg-red-500/10 rounded-sm text-gray-400 hover:text-red-400 transition-colors"
title="Unload Deck"
>
<ArrowCounterClockwiseIcon size={20} />
</button>
</div>
</div>
{/* Card Area */}
<div
className="w-full aspect-[3/2] md:aspect-[2/1] relative cursor-pointer group"
onClick={handleFlip}
style={{ perspective: '1000px' }}
>
<motion.div
className="w-full h-full relative preserve-3d transition-transform duration-500"
animate={{ rotateY: isFlipped ? 180 : 0 }}
transition={{ type: 'spring', stiffness: 260, damping: 20 }}
style={{ transformStyle: 'preserve-3d' }}
>
{/* Front */}
<div
className="absolute inset-0 bg-black border border-white/10 flex flex-col items-center justify-center p-8 md:p-16 text-center select-none shadow-2xl overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-white/20"
style={{
backfaceVisibility: 'hidden',
transform: 'rotateY(0deg) translateZ(1px)',
}}
>
{' '}
<div className="absolute inset-0 opacity-10 pointer-events-none">
<GenerativeArt
seed={`card-${currentIndex}-front`}
className="w-full h-full"
/>
</div>
<div className="absolute top-4 left-4 text-[10px] font-mono uppercase tracking-widest text-indigo-400">
Query
</div>
<h2 className="text-2xl md:text-4xl font-light leading-tight relative z-10">
{cards[currentIndex]?.question}
</h2>
<div className="absolute bottom-6 text-xs text-gray-600 font-mono animate-pulse">
Click to Reveal
</div>
</div>
{/* Back */}
<div
className="absolute inset-0 bg-white text-black flex flex-col items-center justify-center p-8 md:p-16 text-center select-none shadow-2xl overflow-y-auto scrollbar-thin scrollbar-track-transparent scrollbar-thumb-black/20"
style={{
transform: 'rotateY(180deg) translateZ(1px)',
backfaceVisibility: 'hidden',
backgroundColor: 'white',
}}
>
<div className="absolute inset-0 opacity-60 pointer-events-none">
<LuxeArt
seed={`card-${currentIndex}-back`}
colorful={true}
className="w-full h-full mix-blend-multiply"
/>
</div>
<div className="absolute top-4 left-4 text-[10px] font-mono uppercase tracking-widest text-indigo-600 font-bold">
Response
</div>
<h2 className="text-2xl md:text-4xl font-bold leading-tight relative z-10">
{cards[currentIndex]?.answer}
</h2>
</div>
</motion.div>
</div>
{/* Navigation */}
<div className="grid grid-cols-2 gap-4">
<button
onClick={handlePrev}
className="flex items-center justify-center gap-3 p-4 border border-white/10 hover:bg-white/5 transition-all active:scale-[0.98] group"
>
<ArrowLeftIcon className="group-hover:-translate-x-1 transition-transform" />
<span className="font-mono uppercase tracking-widest text-sm">
Previous
</span>
</button>
<button
onClick={handleNext}
className="flex items-center justify-center gap-3 p-4 bg-white text-black hover:bg-indigo-400 transition-all active:scale-[0.98] group"
>
<span className="font-mono uppercase tracking-widest text-sm font-bold">
Next
</span>
<ArrowRightIcon className="group-hover:translate-x-1 transition-transform" />
</button>
</div>
</div>
)}
</div>
</div>
);
}
export default CsvFlashcardsPage;