-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.tsx
616 lines (565 loc) · 16.9 KB
/
create.tsx
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import { useReducer, useState } from "react";
import {
Button,
Container,
Paper,
Radio,
Select,
Stepper,
TextInput,
Textarea,
Title,
Card,
Divider,
Flex,
Overlay,
Loader,
RadioGroup,
Text,
} from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { trpc } from "@/koala/trpc-config";
import { Gender, LangCode, supportedLanguages } from "@/koala/shared-types";
import { GetServerSideProps } from "next";
import { prismaClient } from "@/koala/prisma-client";
import { backfillDecks } from "@/koala/decks/backfill-decks";
import { getLangName } from "@/koala/get-lang-name";
import { draw } from "radash";
import { getServersideUser } from "@/koala/get-serverside-user";
import { useRouter } from "next/router";
type ProcessedCard = {
term: string;
definition: string;
gender: Gender;
};
type Action =
| { type: "ADD_CARD"; card: ProcessedCard }
| { type: "EDIT_CARD"; card: ProcessedCard; index: number }
| { type: "REMOVE_CARD"; index: number }
| { type: "SET_PROCESSED_CARDS"; processedCards: ProcessedCard[] }
| { type: "SET_RAW_INPUT"; rawInput: string }
| { type: "SET_CARD_TYPE"; cardType: string }
// New actions for deck selection
| { type: "SET_DECK_SELECTION"; deckSelection: "existing" | "new" }
| { type: "SET_DECK_ID"; deckId: number | undefined }
| { type: "SET_DECK_NAME"; deckName: string }
| { type: "SET_DECK_LANG"; deckLang: LangCode };
interface State {
// Deck selection
deckSelection: "existing" | "new";
deckId?: number; // if using an existing deck
deckName: string; // if creating a new deck
deckLang: LangCode; // always store the final deck language
// Card creation
rawInput: string;
processedCards: ProcessedCard[];
cardType: string; // listening, speaking, both
}
const DEFAULT_LANG: LangCode = "ko";
const INITIAL_STATE: State = {
deckSelection: "existing",
deckId: undefined,
deckName: "",
deckLang: DEFAULT_LANG, // default to Korean if user selects "new deck" but hasn't changed
rawInput: "",
processedCards: [],
cardType: "listening",
};
function reducer(state: State, action: Action): State {
switch (action.type) {
case "ADD_CARD":
return {
...state,
processedCards: [...state.processedCards, action.card],
};
case "EDIT_CARD": {
const updatedCards = [...state.processedCards];
updatedCards[action.index] = action.card;
return { ...state, processedCards: updatedCards };
}
case "REMOVE_CARD":
return {
...state,
processedCards: state.processedCards.filter(
(_, i) => i !== action.index,
),
};
case "SET_PROCESSED_CARDS":
return { ...state, processedCards: action.processedCards };
case "SET_RAW_INPUT":
return { ...state, rawInput: action.rawInput };
case "SET_CARD_TYPE":
return { ...state, cardType: action.cardType };
// New deck-related actions
case "SET_DECK_SELECTION":
return { ...state, deckSelection: action.deckSelection };
case "SET_DECK_ID":
return { ...state, deckId: action.deckId };
case "SET_DECK_NAME":
return { ...state, deckName: action.deckName };
case "SET_DECK_LANG":
return { ...state, deckLang: action.deckLang };
default:
return state;
}
}
function handleError(error: unknown) {
console.error(error);
notifications.show({
title: "Error",
message: "Something went wrong. Please report this issue if it persists.",
color: "red",
});
}
/*
STEP 1: Choose or Create Deck
*/
interface DeckStepProps {
decks: {
id: number;
name: string;
langCode: string;
}[];
state: State;
dispatch: React.Dispatch<Action>;
onNext: () => void;
}
function DeckStep({ decks, state, dispatch, onNext }: DeckStepProps) {
// Handler for picking an existing deck
const handleExistingDeckChange = (deckId: number | undefined) => {
dispatch({ type: "SET_DECK_ID", deckId });
const selectedDeck = decks.find((d) => d.id === deckId);
if (selectedDeck) {
dispatch({
type: "SET_DECK_LANG",
deckLang: selectedDeck.langCode as LangCode,
});
dispatch({ type: "SET_DECK_NAME", deckName: selectedDeck.name });
}
};
// Handler for switching between "existing" vs "new" deck
const handleDeckModeChange = (value: "existing" | "new") => {
dispatch({ type: "SET_DECK_SELECTION", deckSelection: value });
if (value === "existing") {
// Reset new-deck fields
dispatch({ type: "SET_DECK_NAME", deckName: "" });
dispatch({ type: "SET_DECK_LANG", deckLang: DEFAULT_LANG });
} else {
// Reset existing-deck fields
dispatch({ type: "SET_DECK_ID", deckId: undefined });
dispatch({ type: "SET_DECK_NAME", deckName: "" });
dispatch({ type: "SET_DECK_LANG", deckLang: DEFAULT_LANG });
}
};
// Check if "Next" is disabled
const isNextDisabled = (() => {
if (state.deckSelection === "existing") {
return !state.deckId; // must have chosen an existing deck
} else {
// must have a deck name if creating a new deck
return !state.deckName.trim();
}
})();
return (
<Paper withBorder p="md" radius="md">
<Flex direction="column" gap="md">
<Title order={3}>Step 1: Select or Create Deck</Title>
<div style={{ fontSize: 14, color: "gray" }}>
You can add new cards to an existing deck or create a new one below.
</div>
<RadioGroup
label="Deck Mode"
value={state.deckSelection}
onChange={(value) =>
handleDeckModeChange(value as "existing" | "new")
}
>
<Radio value="existing" label="Use an existing deck" />
<Radio value="new" label="Create a new deck" />
</RadioGroup>
{state.deckSelection === "existing" && (
<Select
label="Existing Deck"
placeholder="Select your deck"
value={state.deckId ? String(state.deckId) : null}
onChange={(val) => handleExistingDeckChange(Number(val))}
data={decks.map((d) => ({
label: `${d.name} (${getLangName(d.langCode)})`,
value: String(d.id),
}))}
/>
)}
{state.deckSelection === "new" && (
<>
<TextInput
label="New Deck Name"
placeholder="e.g. 'Spanish Travel Phrases'"
value={state.deckName}
onChange={(e) =>
dispatch({
type: "SET_DECK_NAME",
deckName: e.currentTarget.value,
})
}
/>
<Select
label="Language"
placeholder="Choose language"
value={state.deckLang}
onChange={(val) =>
dispatch({ type: "SET_DECK_LANG", deckLang: val as LangCode })
}
data={Object.keys(supportedLanguages)
.sort()
.map((langCode) => ({
label: supportedLanguages[langCode as LangCode],
value: langCode,
}))}
/>
</>
)}
<Divider my="sm" />
<Flex justify="flex-end">
<Button onClick={onNext} disabled={isNextDisabled}>
Next
</Button>
</Flex>
</Flex>
</Paper>
);
}
/*
STEP 2: Input Step
*/
interface InputStepProps {
state: State;
dispatch: React.Dispatch<Action>;
onSubmit: () => void;
loading: boolean;
}
const LANG_LEARNING_THEMES = [
"food",
"travel",
"work",
"school",
"family",
"shopping",
"health",
"weather",
"sports",
"hobbies",
"music",
"movies",
"books",
"technology",
"nature",
"animals",
"culture",
"history",
"science",
"math",
"computers",
"humor",
];
function InputStep({ state, dispatch, onSubmit, loading }: InputStepProps) {
const exampleText = () => {
const lang = getLangName(state.deckLang);
const theme = draw(LANG_LEARNING_THEMES);
return `Please make 25 ${lang} example sentences related to ${theme}.`;
};
// We now get our sample from the deckLang in state
const pasteExample = () => {
dispatch({
type: "SET_RAW_INPUT",
rawInput: exampleText(),
});
};
return (
<Paper withBorder p="md" radius="md">
<Flex direction="column" gap="md">
<Title order={3}>Step 2: Input Your Learning Material</Title>
<Text size="sm">
Paste a list of language phrases or vocabulary here. If you don't know
what to learn, try an example by clicking the button.
</Text>
<Text size="sm">
Koala is built for self-study learners who have a textbook
or language course to follow. If you don't have material of your own,
that's OK. Koala can generate content for you to study. Click the
button below until you find a topic that is interesting to you.
</Text>
<Button size="sm" onClick={pasteExample}>
Generate Learning Content 🎲
</Button>
<Textarea
label="Raw Input"
placeholder={exampleText()}
minRows={10}
maxRows={10}
autosize
value={state.rawInput}
onChange={(e) =>
dispatch({
type: "SET_RAW_INPUT",
rawInput: e.currentTarget.value,
})
}
/>
<Radio.Group
label="Card Type"
description="Choose how you want to practice these new cards."
value={state.cardType}
onChange={(value) =>
dispatch({ type: "SET_CARD_TYPE", cardType: value })
}
>
<Radio
value="listening"
label="Listening first, then speaking (Recommended for most sentences)."
/>
<Radio
value="speaking"
label="Speaking only (Good for vocab words)."
/>
<Radio
value="both"
label="Do listening and speaking at the same time. (For well-understood material)."
/>
</Radio.Group>
<Divider my="sm" />
<Flex justify="flex-end">
<Button onClick={onSubmit} disabled={!state.rawInput || loading}>
Process Input
</Button>
</Flex>
</Flex>
</Paper>
);
}
/*
STEP 3: Review & Edit
*/
interface ReviewStepProps {
state: State;
dispatch: React.Dispatch<Action>;
onBack: () => void;
onSave: () => void;
loading: boolean;
}
function ReviewStep({
state,
dispatch,
onBack,
onSave,
loading,
}: ReviewStepProps) {
const handleCardChange = (action: Action) => {
dispatch(action);
};
return (
<Paper withBorder p="md" radius="md">
<Flex direction="column" gap="md">
<Title order={3}>Step 3: Review & Edit Cards</Title>
<div style={{ fontSize: 14, color: "gray" }}>
Verify each card is correct. You can edit the term or definition if
needed, or remove cards that you don't want. When satisfied, click
"Save Cards".
</div>
{state.processedCards.length === 0 && (
<div style={{ fontSize: 14, color: "gray" }}>
No cards to edit yet. Please go back and process input.
</div>
)}
{state.processedCards.map((card, index) => (
<Card withBorder key={index} p="sm">
<Flex direction="column" gap="xs">
<TextInput
label="Term"
value={card.term}
onChange={(e) =>
handleCardChange({
type: "EDIT_CARD",
card: { ...card, term: e.currentTarget.value },
index,
})
}
/>
<TextInput
label="Definition"
value={card.definition}
onChange={(e) =>
handleCardChange({
type: "EDIT_CARD",
card: { ...card, definition: e.currentTarget.value },
index,
})
}
/>
<Flex justify="flex-end">
<Button
variant="outline"
color="red"
onClick={() =>
handleCardChange({ type: "REMOVE_CARD", index })
}
>
Remove
</Button>
</Flex>
</Flex>
</Card>
))}
<Divider my="sm" />
<Flex justify="space-between">
<Button variant="default" onClick={onBack} disabled={loading}>
Back
</Button>
<Button
onClick={onSave}
disabled={loading || state.processedCards.length === 0}
>
Save Cards
</Button>
</Flex>
</Flex>
</Paper>
);
}
export const getServerSideProps: GetServerSideProps = async (context) => {
const dbUser = await getServersideUser(context);
if (!dbUser) {
return { redirect: { destination: "/api/auth/signin", permanent: false } };
}
// Ensure user has a default set of decks
await backfillDecks(dbUser.id);
// Fetch user decks
const decks = await prismaClient.deck.findMany({
where: {
userId: dbUser?.id,
},
});
return {
props: {
decks: decks.map((deck) => ({
id: deck.id,
name: deck.name,
langCode: deck.langCode,
})),
},
};
};
type LanguageInputPageProps = {
decks: {
id: number;
name: string;
langCode: string;
}[];
};
const LanguageInputPage = (props: LanguageInputPageProps) => {
const { decks } = props;
const [state, dispatch] = useReducer(reducer, {
...INITIAL_STATE,
// Improve first time user experience:
deckLang: (decks?.[0]?.langCode as LangCode) || INITIAL_STATE.deckLang,
deckSelection: decks.length > 0 ? "existing" : "new",
});
const [activeStep, setActiveStep] = useState(0);
const [loading, setLoading] = useState(false);
const router = useRouter();
const parseCards = trpc.parseCards.useMutation();
const bulkCreateCards = trpc.bulkCreateCards.useMutation();
/* Move from Step 1 (deck) -> Step 2 (input) */
const handleDeckNext = () => {
setActiveStep(1);
};
/* Process the raw input (Step 2 -> Step 3) */
const handleRawInputSubmit = async () => {
setLoading(true);
try {
const { cards } = await parseCards.mutateAsync({
langCode: state.deckLang,
text: state.rawInput,
});
dispatch({ type: "SET_PROCESSED_CARDS", processedCards: cards });
setActiveStep(2); // Move to editing step
} catch (error) {
handleError(error);
} finally {
setLoading(false);
}
};
/* Save the final cards to the chosen or newly created deck (Step 3 finalize) */
const handleSave = async () => {
setLoading(true);
try {
// If existing deck, use that deck's name; otherwise, use the new deck name
let finalDeckName = state.deckName;
if (state.deckSelection === "existing") {
const existingDeck = decks.find((d) => d.id === state.deckId);
if (existingDeck) {
finalDeckName = existingDeck.name;
}
}
await bulkCreateCards.mutateAsync({
langCode: state.deckLang,
input: state.processedCards,
cardType: state.cardType,
deckName: finalDeckName, // find-or-create by deck name
});
// Reset and start over
dispatch({ type: "SET_PROCESSED_CARDS", processedCards: [] });
dispatch({ type: "SET_RAW_INPUT", rawInput: "" });
setActiveStep(0);
notifications.show({
title: "Success",
message: "Your cards have been created successfully!",
color: "green",
});
router.push("/review");
} catch (error) {
handleError(error);
} finally {
setLoading(false);
}
};
return (
<Container size="sm" mt="xl" style={{ position: "relative" }}>
{loading && (
<Overlay blur={2} opacity={0.6} color="#fff" zIndex={9999}>
<Loader size="lg" variant="dots" />
</Overlay>
)}
<Title order={1} mb="lg">
Create New Cards
</Title>
<Stepper active={activeStep} onStepClick={setActiveStep} mb="xl">
<Stepper.Step label="Deck Selection">
<DeckStep
decks={decks}
state={state}
dispatch={dispatch}
onNext={handleDeckNext}
/>
</Stepper.Step>
<Stepper.Step label="Input">
<InputStep
state={state}
dispatch={dispatch}
onSubmit={handleRawInputSubmit}
loading={loading}
/>
</Stepper.Step>
<Stepper.Step label="Review & Edit">
<ReviewStep
state={state}
dispatch={dispatch}
onBack={() => setActiveStep(1)}
onSave={handleSave}
loading={loading}
/>
</Stepper.Step>
</Stepper>
</Container>
);
};
export default LanguageInputPage;