dracosh 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -6
- package/locales/de.json +207 -9
- package/locales/en.json +207 -9
- package/locales/fr.json +210 -9
- package/locales/pl.json +213 -9
- package/locales/uk.json +213 -9
- package/package.json +2 -2
- package/src/ai.js +245 -0
- package/src/alerts.js +108 -57
- package/src/assist.js +232 -0
- package/src/background.js +63 -0
- package/src/bug.js +112 -0
- package/src/cli.js +26 -3
- package/src/doctor.js +123 -0
- package/src/library.js +14 -2
- package/src/lock.js +1 -1
- package/src/manage.js +14 -3
- package/src/notify.js +44 -0
- package/src/player.js +117 -0
- package/src/settings.js +8 -1
- package/src/speech.js +82 -0
- package/src/ui/AddCard.js +228 -32
- package/src/ui/Ai.js +284 -0
- package/src/ui/AnswerInput.js +13 -6
- package/src/ui/App.js +47 -19
- package/src/ui/BadgeUnlock.js +4 -6
- package/src/ui/Badges.js +5 -7
- package/src/ui/Cards.js +126 -12
- package/src/ui/Companion.js +2 -2
- package/src/ui/Confetti.js +2 -2
- package/src/ui/DieRoll.js +6 -6
- package/src/ui/Import.js +7 -7
- package/src/ui/Preview.js +9 -6
- package/src/ui/Settings.js +14 -7
- package/src/ui/Stats.js +11 -21
- package/src/ui/TheOne.js +2 -2
- package/src/ui/Voice.js +238 -0
- package/src/ui/Welcome.js +4 -3
- package/src/ui/icons.js +2 -4
- package/src/ui/kit.js +88 -11
- package/src/voice.js +129 -0
package/src/ui/AddCard.js
CHANGED
|
@@ -6,20 +6,22 @@ import { readCardList } from "../cardlist.js";
|
|
|
6
6
|
import { frontKey } from "../judge.js";
|
|
7
7
|
import { isKnown, languageName } from "../language.js";
|
|
8
8
|
import { LanguagePicker, pairText } from "./Languages.js";
|
|
9
|
+
import { providerName } from "../ai.js";
|
|
10
|
+
import { suggestCard, suggestCards } from "../assist.js";
|
|
9
11
|
import { AnswerInput } from "./AnswerInput.js";
|
|
10
12
|
import { Choice, choiceHints } from "./Choice.js";
|
|
11
13
|
import { t } from "../i18n.js";
|
|
12
|
-
import { cutToWidth, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
14
|
+
import { cellWidth, cutToWidth, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
13
15
|
|
|
14
16
|
const SHOWN_CARDS = 5; // the cards added so far, newest last
|
|
15
17
|
const PASTE_ROWS = 8; // rows of a pasted list on screen at once
|
|
16
18
|
const answers = (back) => back.join(" / ");
|
|
17
19
|
|
|
18
|
-
// How a card will be asked
|
|
20
|
+
// How a card will be asked: a cloze's gaps under the field, and a new deck's way once picked
|
|
21
|
+
// ("kot → ? you type cat / kitty")
|
|
19
22
|
function askedAs(fields, directions) {
|
|
20
23
|
if (fields.cloze) return t("add.preview.cloze", { prompt: blankGap(fields.front, 0) + (fields.back.length ? ` (${answers(fields.back)})` : ""), count: gapsOf(fields.front).length });
|
|
21
24
|
const [front, back] = [fields.front, answers(fields.back)];
|
|
22
|
-
if (!directions) return t("add.preview.pair", { front, back });
|
|
23
25
|
if (directions === "backward") return t("add.preview.asks", { prompt: back, answer: front });
|
|
24
26
|
const forward = t("add.preview.asks", { prompt: front, answer: back });
|
|
25
27
|
return directions === "both" ? t("add.preview.both", { forward, backward: t("add.preview.asks", { prompt: back, answer: front }) }) : forward;
|
|
@@ -61,9 +63,33 @@ function withRepeats(items) {
|
|
|
61
63
|
// replaces the tour), then add cards one line each until Esc: "kot = cat; kitty // example". After a new
|
|
62
64
|
// deck's first card it asks which way the deck is asked. A pasted list opens a review first.
|
|
63
65
|
// `actions` reads and writes the library; `startNew` skips the deck list ("+ New deck" in /decks).
|
|
64
|
-
// `suggest` (from the welcome): { name, example }, a deck name to start from
|
|
65
|
-
//
|
|
66
|
-
|
|
66
|
+
// `suggest` (from the welcome): { name, example, request? }, a deck name to start from, a card in your
|
|
67
|
+
// pair and, for a first deck from AI help, what to ask it for.
|
|
68
|
+
// `into`: straight to adding cards to this deck (+ Add cards in /cards). `ai`: settings.ai, for
|
|
69
|
+
// suggestions while a card is typed (null: none).
|
|
70
|
+
// the ways to add a card, a name and what to type side by side
|
|
71
|
+
function Ways({ example, ai }) {
|
|
72
|
+
const rows = [
|
|
73
|
+
{ name: t("add.ways.type"), what: example, typed: true },
|
|
74
|
+
{ name: t("add.ways.paste"), what: t("add.ways.pasteWhat") },
|
|
75
|
+
ai && { name: `✦ ${t("add.ways.ai")}`, what: t("add.ways.aiExample"), typed: true, color: theme.accent }
|
|
76
|
+
].filter(Boolean);
|
|
77
|
+
const width = Math.max(...rows.map((row) => cellWidth(row.name))) + 3;
|
|
78
|
+
return html`
|
|
79
|
+
<${Box} flexDirection="column" marginTop=${1}>
|
|
80
|
+
${rows.map(
|
|
81
|
+
(row) => html`
|
|
82
|
+
<${Box} key=${row.name}>
|
|
83
|
+
<${Box} width=${width} flexShrink=${0}><${Text} color=${row.color} dimColor=${!row.color}>${row.name}<//><//>
|
|
84
|
+
<${Text} wrap="truncate-end" color=${row.typed ? row.color : undefined} dimColor=${!row.typed}>${row.what}<//>
|
|
85
|
+
<//>
|
|
86
|
+
`
|
|
87
|
+
)}
|
|
88
|
+
<//>
|
|
89
|
+
`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function AddCard({ current, actions, onDone, startNew = false, suggest = null, into = null, ai = null }) {
|
|
67
93
|
const panel = usePanelWidth();
|
|
68
94
|
const [step, setStep] = useState("loading"); // loading | pick | deck | line | back | direction | review | fix | saving
|
|
69
95
|
const [decks, setDecks] = useState([]);
|
|
@@ -80,10 +106,21 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
80
106
|
const [direction, setDirection] = useState(0);
|
|
81
107
|
const [sample, setSample] = useState(null); // the card the direction question shows
|
|
82
108
|
const busy = useRef(false); // a save in flight: Enter again does nothing
|
|
83
|
-
const [pasted, setPasted] = useState(null); // { items, selected, confirm,
|
|
109
|
+
const [pasted, setPasted] = useState(null); // { items, selected, confirm, total, ai? }: ai = { request, writing, problem } for a list from AI help
|
|
110
|
+
const listing = useRef(null); // the AI list being written: its AbortController
|
|
111
|
+
const [spin, setSpin] = useState(0);
|
|
112
|
+
const writing = Boolean(pasted?.ai?.writing);
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (!writing) return;
|
|
115
|
+
const timer = setInterval(() => setSpin((n) => n + 1), 100);
|
|
116
|
+
return () => clearInterval(timer);
|
|
117
|
+
}, [writing]);
|
|
118
|
+
useEffect(() => () => listing.current?.abort(), []);
|
|
84
119
|
const [message, setMessage] = useState("");
|
|
85
120
|
const [error, setError] = useState("");
|
|
86
121
|
const lookup = useRef(0);
|
|
122
|
+
const taken = useRef(new Set()); // the fronts whose suggestion was taken (Tab) in this /add
|
|
123
|
+
const help = useAssist({ ai, actions, deck, step, typed, draft, added });
|
|
87
124
|
|
|
88
125
|
useEffect(() => {
|
|
89
126
|
actions.ownDecks().then(
|
|
@@ -126,8 +163,8 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
126
163
|
const { deck: created, removed } = await actions.startDeck({ name });
|
|
127
164
|
setDeck({ name: created.name, directions: created.directions, isNew: true });
|
|
128
165
|
setNewDeck({ name: created.name, removed });
|
|
129
|
-
setMessage(t("add.deckReady", { deck: created.name }));
|
|
130
|
-
clearField();
|
|
166
|
+
setMessage(t(suggest?.request ? "add.deckReadyAi" : "add.deckReady", { deck: created.name }));
|
|
167
|
+
clearField(suggest?.request ? `?${suggest.request}` : ""); // from the welcome: a list from AI, ready to ask for
|
|
131
168
|
setStep("line");
|
|
132
169
|
} catch (err) {
|
|
133
170
|
setError(err.message);
|
|
@@ -168,9 +205,15 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
168
205
|
}, () => {});
|
|
169
206
|
}
|
|
170
207
|
|
|
171
|
-
async function save(
|
|
208
|
+
async function save(typed) {
|
|
172
209
|
if (busy.current) return;
|
|
173
210
|
busy.current = true;
|
|
211
|
+
// an answer or the example AI suggested for this front, taken and still there: the card is AI's,
|
|
212
|
+
// marked until it's checked. A card fixed (↑) with nothing changed keeps its mark.
|
|
213
|
+
const same = (a, b) => a.trim().toLowerCase() === b.trim().toLowerCase();
|
|
214
|
+
const suggested = taken.current.has(frontKey(typed.front)) && help.suggestionFor(typed.front);
|
|
215
|
+
const written = Boolean(suggested && (typed.back.some((answer) => suggested.back.some((other) => same(answer, other))) || (typed.example && suggested.example && same(typed.example, suggested.example))));
|
|
216
|
+
const fields = { ...typed, ai: written || Boolean(editing?.ai && formatLine(typed) === formatLine(editing)) };
|
|
174
217
|
try {
|
|
175
218
|
if (editing) {
|
|
176
219
|
const { card, staleKeys } = await actions.updateCardIn(deck.name, editing.id, fields);
|
|
@@ -209,6 +252,11 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
209
252
|
}
|
|
210
253
|
if (step === "line") {
|
|
211
254
|
if (!value) return setError(t("add.needFront"));
|
|
255
|
+
if (ai && value === "?") return setError(t("add.ai.needRequest"));
|
|
256
|
+
if (/^\?\s*\S/.test(value) && !value.includes(" = ")) {
|
|
257
|
+
if (ai) return askForList(value.slice(1).trim());
|
|
258
|
+
return setError(t("add.ai.listOff"));
|
|
259
|
+
}
|
|
212
260
|
const fields = parseLine(value);
|
|
213
261
|
if (!fields.front) return setError(t("add.needFront"));
|
|
214
262
|
if (duplicate) return; // the warning is already under the field
|
|
@@ -234,7 +282,7 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
234
282
|
.then((existing) => {
|
|
235
283
|
setPasted((p) => {
|
|
236
284
|
const issue = issueOf(fields, deck.isNew ? null : deck.directions, Boolean(existing));
|
|
237
|
-
return { ...p, items: p.items.map((item, i) => (i === p.selected ? { line: value, fields, issue, dropped: false } : item)), confirm: false };
|
|
285
|
+
return { ...p, items: p.items.map((item, i) => (i === p.selected ? { ...item, line: value, fields, issue, dropped: false, changed: item.changed || value !== item.line } : item)), confirm: false };
|
|
238
286
|
});
|
|
239
287
|
setStep("review");
|
|
240
288
|
});
|
|
@@ -318,6 +366,7 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
318
366
|
async function openPaste(text) {
|
|
319
367
|
const list = readCardList(text, { comments: false }); // a pasted "#tag = hashtag" is a card
|
|
320
368
|
if (!list.items.length) return setStep("line");
|
|
369
|
+
setEditing(null);
|
|
321
370
|
const fronts = await Promise.all(list.items.map((item) => actions.existingCard(deck.name, item.fields.front).catch(() => null)));
|
|
322
371
|
const existing = new Set(fronts.filter(Boolean).map((card) => frontKey(card.front)));
|
|
323
372
|
setPasted({ items: readPaste(list, existing, deck.isNew ? null : deck.directions), selected: 0, confirm: false, total: list.items.length });
|
|
@@ -326,12 +375,46 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
326
375
|
setStep("review");
|
|
327
376
|
}
|
|
328
377
|
|
|
378
|
+
// "?20 words for the kitchen": AI help writes a list into the review as it goes; none is kept
|
|
379
|
+
// until you keep it (space, or a for all)
|
|
380
|
+
async function askForList(request) {
|
|
381
|
+
listing.current?.abort();
|
|
382
|
+
const controller = (listing.current = new AbortController());
|
|
383
|
+
setEditing(null); // the list's cards are new ones: the last card isn't being fixed any more
|
|
384
|
+
setPasted({ items: [], selected: 0, confirm: false, total: 0, ai: { request, writing: true, problem: "" } });
|
|
385
|
+
setError("");
|
|
386
|
+
setMessage("");
|
|
387
|
+
clearField();
|
|
388
|
+
setStep("review");
|
|
389
|
+
const update = (change) => setPasted((p) => (p && listing.current === controller ? change(p) : p));
|
|
390
|
+
try {
|
|
391
|
+
const [languages, cards] = await Promise.all([actions.languagesOf(deck.name).catch(() => ({})), actions.cardsIn(deck.name).catch(() => [])]);
|
|
392
|
+
const known = new Set([...cards, ...added].map((card) => frontKey(card.front)));
|
|
393
|
+
const directions = deck.isNew ? null : deck.directions;
|
|
394
|
+
await suggestCards(ai, {
|
|
395
|
+
request,
|
|
396
|
+
languages,
|
|
397
|
+
cards: [...cards, ...added],
|
|
398
|
+
signal: controller.signal,
|
|
399
|
+
onCard: (fields) => update((p) => ({ ...p, total: p.total + 1, items: [...p.items, { line: formatLine(fields), fields, issue: issueOf(fields, directions, known.has(frontKey(fields.front))), dropped: true, ai: true }] }))
|
|
400
|
+
});
|
|
401
|
+
update((p) => ({ ...p, ai: { ...p.ai, writing: false, problem: p.items.length ? "" : t("add.ai.noList") } }));
|
|
402
|
+
} catch (err) {
|
|
403
|
+
if (controller.signal.aborted) return;
|
|
404
|
+
update((p) => ({ ...p, ai: { ...p.ai, writing: false, problem: err.message } }));
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
329
408
|
async function addPasted() {
|
|
330
409
|
const ready = withRepeats(pasted.items).filter((item) => !item.dropped && !item.issue);
|
|
331
|
-
if (!ready.length) return setError(t("add.paste.nothing"));
|
|
410
|
+
if (!ready.length) return setError(t(pasted.ai ? "add.ai.nothingKept" : "add.paste.nothing"));
|
|
411
|
+
// what's kept so far is added; the rest of the list isn't written
|
|
412
|
+
listing.current?.abort();
|
|
413
|
+
listing.current = null;
|
|
414
|
+
if (pasted.ai?.writing) setPasted((p) => ({ ...p, ai: { ...p.ai, writing: false } }));
|
|
332
415
|
setStep("saving");
|
|
333
416
|
try {
|
|
334
|
-
const { added: cards } = await actions.addCardsTo(deck.name, ready.map((item) => item.fields));
|
|
417
|
+
const { added: cards } = await actions.addCardsTo(deck.name, ready.map((item) => ({ ...item.fields, ai: Boolean(item.ai && !item.changed) })));
|
|
335
418
|
setAdded((list) => [...list, ...cards]);
|
|
336
419
|
setPasted(null);
|
|
337
420
|
const skipped = pasted.items.length - ready.length - pasted.items.filter((item) => item.dropped).length;
|
|
@@ -359,8 +442,14 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
359
442
|
|
|
360
443
|
useInput(
|
|
361
444
|
(input, key) => {
|
|
445
|
+
const keeping = pasted.items.some((item) => !item.dropped && !item.issue);
|
|
362
446
|
if (key.escape) {
|
|
363
|
-
|
|
447
|
+
// an AI list with nothing kept goes at once; anything kept or pasted is asked about first
|
|
448
|
+
if (pasted.confirm || (pasted.ai && !keeping)) {
|
|
449
|
+
listing.current?.abort();
|
|
450
|
+
listing.current = null;
|
|
451
|
+
// an AI list let go: its request is back in the field, to change and ask again
|
|
452
|
+
if (pasted.ai) clearField(`?${pasted.ai.request}`);
|
|
364
453
|
setPasted(null);
|
|
365
454
|
setError("");
|
|
366
455
|
return setStep("line");
|
|
@@ -368,9 +457,15 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
368
457
|
return setPasted((p) => ({ ...p, confirm: true }));
|
|
369
458
|
}
|
|
370
459
|
if (pasted.confirm) setPasted((p) => ({ ...p, confirm: false }));
|
|
460
|
+
if (!pasted.items.length) return; // an AI list still on its way
|
|
371
461
|
if (key.upArrow) return setPasted((p) => ({ ...p, selected: Math.max(0, p.selected - 1) }));
|
|
372
|
-
if (key.downArrow) return setPasted((p) => ({ ...p, selected: Math.min(p.items.length - 1, p.selected + 1) }));
|
|
373
|
-
if (input === "x") return setPasted((p) => ({ ...p, items: p.items.map((item, i) => (i === p.selected ? { ...item, dropped: !item.dropped } : item)) }));
|
|
462
|
+
if (key.downArrow) return setPasted((p) => ({ ...p, selected: Math.max(0, Math.min(p.items.length - 1, p.selected + 1)) }));
|
|
463
|
+
if (input === "x" || (pasted.ai && input === " ")) return setPasted((p) => ({ ...p, items: p.items.map((item, i) => (i === p.selected ? { ...item, dropped: !item.dropped } : item)) }));
|
|
464
|
+
if (pasted.ai && input === "a") {
|
|
465
|
+
// all kept, or with all kept already, none
|
|
466
|
+
const all = pasted.items.every((item) => item.issue || !item.dropped);
|
|
467
|
+
return setPasted((p) => ({ ...p, items: p.items.map((item) => (item.issue ? item : { ...item, dropped: all })) }));
|
|
468
|
+
}
|
|
374
469
|
if (input === "e") {
|
|
375
470
|
clearField(pasted.items[pasted.selected].line);
|
|
376
471
|
return setStep("fix");
|
|
@@ -394,10 +489,14 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
394
489
|
if (step === "languages") return html`<${LanguagePicker} deck=${deck.name} sample=${sample} languages=${langs} learning=${langs.learning} home=${langs.home} onDone=${saveLanguages} onCancel=${() => setStep("direction")} />`;
|
|
395
490
|
|
|
396
491
|
const title =
|
|
397
|
-
step === "pick" ? t("add.titlePick") : step === "deck" ? t(decks.length ? "add.titleNew" : "add.titleFirst") : step === "review" || step === "fix" ? t("add.paste.title", { deck: deck.name }) : t("add.titleCard", { deck: deck?.name ?? "" });
|
|
492
|
+
step === "pick" ? t("add.titlePick") : step === "deck" ? t(decks.length ? "add.titleNew" : "add.titleFirst") : step === "review" || step === "fix" ? t(pasted?.ai ? "add.ai.listTitle" : "add.paste.title", { deck: deck.name }) : t("add.titleCard", { deck: deck?.name ?? "" });
|
|
398
493
|
const fieldLabel = step === "deck" ? "deck" : step === "back" ? "back" : step === "line" || step === "fix" ? "line" : null;
|
|
399
|
-
|
|
400
|
-
const
|
|
494
|
+
// with a suggestion on screen, the card it would make (Tab takes it)
|
|
495
|
+
const asking = Boolean(ai && step === "line" && !editing && typed.startsWith("?")); // "?…" asks AI for a list
|
|
496
|
+
const parsed = step === "line" && typed.trim() && !asking ? parseLine(typed + help.ghost) : null;
|
|
497
|
+
// only what isn't plain from the line itself: a cloze's gaps, or how a lone front goes on (unless AI
|
|
498
|
+
// help is busy with its back)
|
|
499
|
+
const preview = !parsed ? null : parsed.cloze ? askedAs(parsed, null) : parsed.back.length || parsed.hasBack || help.status ? null : t("add.preview.noBack");
|
|
401
500
|
const shownPaste = pasted ? withRepeats(pasted.items) : [];
|
|
402
501
|
const ready = shownPaste.filter((item) => !item.dropped && !item.issue).length;
|
|
403
502
|
const narrow = panel < 60;
|
|
@@ -412,7 +511,7 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
412
511
|
html`
|
|
413
512
|
<${Box} flexDirection="column" marginTop=${1}>
|
|
414
513
|
${added.length > SHOWN_CARDS && html`<${Text} dimColor>${t("add.more", { count: added.length - SHOWN_CARDS })}<//>`}
|
|
415
|
-
${added.slice(-SHOWN_CARDS).map((card) => html`<${Text} key=${card.id} dimColor wrap="truncate-end"><${Text} color=${theme.good}>${card.fixed ? "✎" : "✓"}<//> ${card.cloze ? plainFront(card.front) : `${card.front} = ${answers(card.back)}`}<//>`)}
|
|
514
|
+
${added.slice(-SHOWN_CARDS).map((card) => html`<${Text} key=${card.id} dimColor wrap="truncate-end"><${Text} color=${card.ai ? theme.accent : theme.good}>${card.ai ? "✦" : card.fixed ? "✎" : "✓"}<//> ${card.cloze ? plainFront(card.front) : `${card.front} = ${answers(card.back)}`}<//>`)}
|
|
416
515
|
<//>
|
|
417
516
|
`}
|
|
418
517
|
${message && html`<${Box} marginTop=${1}><${Text} color=${theme.good}>${message}<//><//>`}
|
|
@@ -422,7 +521,9 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
422
521
|
html`
|
|
423
522
|
<${Box} flexDirection="column" marginTop=${1}>
|
|
424
523
|
<${Text} bold>${t(`add.fields.${fieldLabel}.label`)}<//>
|
|
425
|
-
|
|
524
|
+
${fieldLabel === "line"
|
|
525
|
+
? !asking && html`<${Ways} example=${suggest?.example || t("add.fields.line.hint")} ai=${ai && !editing} />`
|
|
526
|
+
: html`<${Text} dimColor>${t(`add.fields.${fieldLabel}.hint`)}<//>`}
|
|
426
527
|
<${Box} marginTop=${1}>
|
|
427
528
|
<${AnswerInput}
|
|
428
529
|
key=${`${fieldLabel}-${field.key}`}
|
|
@@ -434,8 +535,11 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
434
535
|
onRecall=${step === "line" ? recall : undefined}
|
|
435
536
|
cardLine=${step === "line" || step === "fix"}
|
|
436
537
|
onPaste=${step === "line" ? openPaste : undefined}
|
|
538
|
+
ghost=${help.ghost}
|
|
539
|
+
onGhost=${() => help.front && taken.current.add(frontKey(help.front))}
|
|
437
540
|
/>
|
|
438
541
|
<//>
|
|
542
|
+
${help.status && html`<${Box} paddingLeft=${2} marginTop=${1}><${Text} color=${help.problem ? theme.warn : theme.accent}>${help.spinner ?? "✦"} ${help.status}<//><//>`}
|
|
439
543
|
${preview && html`<${Box} paddingLeft=${2}><${Text} dimColor>${preview}<//><//>`}
|
|
440
544
|
${duplicate && step === "line" && html`<${Box} paddingLeft=${2}><${Text} color=${theme.warn}>${t("add.duplicateCard", { front: duplicate.front, deck: deck.name })}<//><//>`}
|
|
441
545
|
<//>
|
|
@@ -486,29 +590,39 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
486
590
|
pasted &&
|
|
487
591
|
html`
|
|
488
592
|
<${Box} flexDirection="column" marginTop=${1}>
|
|
489
|
-
|
|
593
|
+
${pasted.ai
|
|
594
|
+
? html`<${Box} flexDirection="column">
|
|
595
|
+
${pasted.ai.writing
|
|
596
|
+
? html`<${Text} color=${theme.accent}>${SPIN[spin % SPIN.length]} ${t("add.ai.request", { request: cutToWidth(pasted.ai.request, Math.max(16, panel - 40)) })} · ${pasted.items.length ? t("add.ai.writing", { provider: providerName(ai.provider), count: pasted.items.length }) : t("add.ai.writingStart", { provider: providerName(ai.provider) })}<//>`
|
|
597
|
+
: html`<${Text}>${t("add.ai.request", { request: cutToWidth(pasted.ai.request, Math.max(16, panel - 40)) })} · ${t("add.ai.listSummary", { count: pasted.items.length, kept: ready })}<//>`}
|
|
598
|
+
${pasted.ai.problem && html`<${Text} color=${theme.warn}>${pasted.ai.problem}<//>`}
|
|
599
|
+
<//>`
|
|
600
|
+
: html`<${Text}>${t("add.paste.summary", { lines: pasted.total, count: ready })}${shownPaste.some((item) => item.issue && !item.dropped) ? t("add.paste.attention", { count: shownPaste.filter((item) => item.issue && !item.dropped).length }) : ""}<//>`}
|
|
490
601
|
<${Box} flexDirection="column" marginTop=${1}>
|
|
491
602
|
${shownPaste.slice(firstRow, firstRow + PASTE_ROWS).map((item, i) => {
|
|
492
603
|
const index = firstRow + i;
|
|
493
604
|
const on = index === pasted.selected && step === "review";
|
|
494
|
-
|
|
495
|
-
const
|
|
605
|
+
// a suggestion not kept (yet) is a plain circle, not a line crossed out
|
|
606
|
+
const mark = item.ai ? (item.issue ? "!" : item.dropped ? "○" : "✓") : item.dropped ? "✗" : item.issue ? "!" : "✓";
|
|
607
|
+
const color = item.issue && (item.ai || !item.dropped) ? theme.warn : item.dropped ? undefined : theme.good;
|
|
608
|
+
const crossed = item.dropped && !item.ai;
|
|
609
|
+
const showIssue = item.issue && (item.ai || !item.dropped);
|
|
496
610
|
return html`
|
|
497
|
-
<${Box} key=${index} flexDirection=${narrow &&
|
|
611
|
+
<${Box} key=${index} flexDirection=${narrow && showIssue ? "column" : "row"}>
|
|
498
612
|
<${Box}>
|
|
499
613
|
<${Box} flexShrink=${0}>
|
|
500
614
|
<${Text} color=${on ? theme.accent : undefined}>${on ? "❯ " : " "}<//>
|
|
501
|
-
<${Text} color=${color} dimColor=${item.dropped}>${mark} <//>
|
|
615
|
+
<${Text} color=${color} dimColor=${item.dropped && !item.issue}>${mark} <//>
|
|
502
616
|
<//>
|
|
503
|
-
<${Box} flexGrow=${1}><${Text} dimColor=${item.dropped} strikethrough=${
|
|
617
|
+
<${Box} flexGrow=${1}><${Text} dimColor=${item.dropped} strikethrough=${crossed} wrap="truncate-end">${item.fields.cloze ? plainFront(item.fields.front) : item.fields.back.length ? `${item.fields.front} = ${answers(item.fields.back)}${item.fields.example ? " " : ""}` : item.fields.front}${item.fields.example && !item.fields.cloze ? html`<${Text} dimColor>${item.fields.example}<//>` : ""}<//><//>
|
|
504
618
|
<//>
|
|
505
|
-
${
|
|
619
|
+
${showIssue && html`<${Box} flexShrink=${0} marginLeft=${narrow ? 4 : 0}><${Text} color=${theme.warn}>${narrow ? "" : " "}${t(`add.paste.issues.${item.issue}`)}<//><//>`}
|
|
506
620
|
<//>
|
|
507
621
|
`;
|
|
508
622
|
})}
|
|
509
623
|
<//>
|
|
510
624
|
${pasted.items.length > PASTE_ROWS && html`<${Text} dimColor>${t("add.paste.position", { at: pasted.selected + 1, count: pasted.items.length })}<//>`}
|
|
511
|
-
${pasted.confirm && html`<${Box} marginTop=${1}><${Text} color=${theme.warn}>${t("add.paste.confirm", { count: ready })}<//><//>`}
|
|
625
|
+
${pasted.confirm && html`<${Box} marginTop=${1}><${Text} color=${theme.warn}>${t(pasted.ai ? "add.ai.confirm" : "add.paste.confirm", { count: ready })}<//><//>`}
|
|
512
626
|
<//>
|
|
513
627
|
`}
|
|
514
628
|
${error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`}
|
|
@@ -522,14 +636,96 @@ export function AddCard({ current, actions, onDone, startNew = false, suggest =
|
|
|
522
636
|
: step === "deck"
|
|
523
637
|
? t("keyHints.addName")
|
|
524
638
|
: step === "review"
|
|
525
|
-
?
|
|
639
|
+
? pasted?.ai
|
|
640
|
+
? t(!pasted.items.length ? "keyHints.aiWaiting" : ready ? "keyHints.aiReview" : "keyHints.aiReviewNone", { count: ready })
|
|
641
|
+
: t("keyHints.pasteReview", { count: ready })
|
|
526
642
|
: step === "fix"
|
|
527
643
|
? t("keyHints.pasteFix")
|
|
528
644
|
: step === "back"
|
|
529
|
-
? t("keyHints.addBack")
|
|
530
|
-
:
|
|
645
|
+
? (help.ghost ? `${t("keyHints.takeSuggestion")} · ` : "") + t("keyHints.addBack")
|
|
646
|
+
: asking
|
|
647
|
+
? t("keyHints.askList")
|
|
648
|
+
: `${help.ghost ? t("keyHints.takeSuggestion") : t("keyHints.tabPart")} · ${editing ? t("keyHints.addFixing") : added.length ? t("keyHints.addMore") : t("keyHints.addFirst")}`}
|
|
531
649
|
/>
|
|
532
650
|
<//>
|
|
533
651
|
<//>
|
|
534
652
|
`;
|
|
535
653
|
}
|
|
654
|
+
|
|
655
|
+
const PAUSE_MS = 350; // typing stops this long before a suggestion is asked for
|
|
656
|
+
const SPIN = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
657
|
+
const STOPPING = new Set(["noKey", "badKey", "noModel", "noCredit", "offline", "offlineLocal"]); // no point asking again in this /add
|
|
658
|
+
|
|
659
|
+
// Suggestions for the card being typed: once its front is followed by " = " (or on the back's own
|
|
660
|
+
// field), the model is asked for the back and an example, shown dimmed after what's typed (`ghost`).
|
|
661
|
+
// It goes away as soon as the typing no longer matches it; every key cancels a request in flight.
|
|
662
|
+
// The ghost is one part at a time: Tab takes the back, then the example. → { ghost, status, spinner, problem }
|
|
663
|
+
function useAssist({ ai, actions, deck, step, typed, draft, added }) {
|
|
664
|
+
const [answers, setAnswers] = useState({}); // front → suggestion line ("" for nothing usable)
|
|
665
|
+
const [asking, setAsking] = useState(null); // the front asked about now
|
|
666
|
+
const [problem, setProblem] = useState("");
|
|
667
|
+
const context = useRef(null); // { deck, languages, cards } of the deck being added to
|
|
668
|
+
const [spin, setSpin] = useState(0);
|
|
669
|
+
const stopped = useRef(false);
|
|
670
|
+
|
|
671
|
+
// what's typed → the front to ask about, and the text the suggestion follows
|
|
672
|
+
const target = (() => {
|
|
673
|
+
if (!ai || !deck || stopped.current) return null;
|
|
674
|
+
if (step === "back" && draft && !draft.cloze && !typed.includes(" // ")) return { front: draft.front, base: "" };
|
|
675
|
+
if (step !== "line" || typed.startsWith("?")) return null; // "?…" asks for a list
|
|
676
|
+
const at = typed.indexOf(" = ");
|
|
677
|
+
if (at < 1 || typed.includes(" // ") || typed.indexOf(" = ", at + 3) >= 0) return null;
|
|
678
|
+
const fields = parseLine(typed.slice(0, at));
|
|
679
|
+
return fields.front && !fields.cloze ? { front: fields.front, base: typed.slice(0, at + 3) } : null;
|
|
680
|
+
})();
|
|
681
|
+
const front = target?.front ?? null;
|
|
682
|
+
|
|
683
|
+
useEffect(() => {
|
|
684
|
+
if (!front || front in answers) return;
|
|
685
|
+
const controller = new AbortController();
|
|
686
|
+
const timer = setTimeout(async () => {
|
|
687
|
+
setAsking(front);
|
|
688
|
+
try {
|
|
689
|
+
if (context.current?.deck !== deck.name) {
|
|
690
|
+
const [languages, cards] = await Promise.all([actions.languagesOf(deck.name).catch(() => ({})), actions.cardsIn(deck.name).catch(() => [])]);
|
|
691
|
+
context.current = { deck: deck.name, languages, cards };
|
|
692
|
+
}
|
|
693
|
+
const cards = [...context.current.cards, ...added];
|
|
694
|
+
const found = await suggestCard(ai, { front, languages: context.current.languages, cards, signal: controller.signal });
|
|
695
|
+
setProblem("");
|
|
696
|
+
setAnswers((all) => ({ ...all, [front]: found ? found.back.join("; ") + (found.example ? ` // ${found.example}` : "") : "" }));
|
|
697
|
+
} catch (err) {
|
|
698
|
+
if (controller.signal.aborted) return;
|
|
699
|
+
if (STOPPING.has(err.code)) stopped.current = true;
|
|
700
|
+
setProblem(err.message);
|
|
701
|
+
} finally {
|
|
702
|
+
setAsking((now) => (now === front ? null : now));
|
|
703
|
+
}
|
|
704
|
+
}, PAUSE_MS);
|
|
705
|
+
return () => {
|
|
706
|
+
clearTimeout(timer);
|
|
707
|
+
controller.abort();
|
|
708
|
+
};
|
|
709
|
+
}, [front, deck?.name]);
|
|
710
|
+
|
|
711
|
+
const waiting = Boolean(ai && front && asking === front);
|
|
712
|
+
useEffect(() => {
|
|
713
|
+
if (!waiting) return;
|
|
714
|
+
const timer = setInterval(() => setSpin((n) => n + 1), 100);
|
|
715
|
+
return () => clearInterval(timer);
|
|
716
|
+
}, [waiting]);
|
|
717
|
+
|
|
718
|
+
// what was suggested for a front → { back, example } (null: nothing)
|
|
719
|
+
const suggestionFor = (asked) => (answers[asked] ? parseBack(answers[asked]) : null);
|
|
720
|
+
if (!ai) return { ghost: "", status: "", problem: false, suggestionFor };
|
|
721
|
+
const line = target && answers[target.front] ? target.base + answers[target.front] : "";
|
|
722
|
+
const rest = line && line.length > typed.length && line.toLowerCase().startsWith(typed.toLowerCase()) ? line.slice(typed.length) : "";
|
|
723
|
+
const cut = rest.indexOf(" // ");
|
|
724
|
+
const ghost = cut > 0 ? rest.slice(0, cut) : rest; // the back first; its example once the back is in
|
|
725
|
+
const name = providerName(ai.provider);
|
|
726
|
+
const common = { ghost, front, suggestionFor };
|
|
727
|
+
if (problem) return { ...common, status: problem, problem: true };
|
|
728
|
+
if (ghost) return { ...common, status: t(ghost.startsWith(" // ") ? "add.ai.example" : "add.ai.suggested", { provider: name }), problem: false };
|
|
729
|
+
if (waiting) return { ...common, status: t("add.ai.thinking", { provider: name }), spinner: SPIN[spin % SPIN.length], problem: false };
|
|
730
|
+
return { ...common, status: "", problem: false };
|
|
731
|
+
}
|