dracosh 0.6.0 → 0.8.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 +45 -8
- package/locales/de.json +359 -11
- package/locales/en.json +359 -11
- package/locales/fr.json +375 -11
- package/locales/pl.json +391 -11
- package/locales/uk.json +391 -11
- package/package.json +1 -1
- package/src/ai.js +245 -0
- package/src/alerts.js +77 -23
- package/src/assist.js +232 -0
- package/src/cli.js +9 -5
- package/src/commands.js +3 -0
- package/src/firstwords.js +49 -0
- package/src/language.js +11 -6
- package/src/library.js +135 -3
- package/src/lock.js +1 -1
- package/src/manage.js +160 -4
- package/src/progress.js +8 -0
- package/src/session.js +38 -6
- package/src/settings.js +11 -1
- package/src/store.js +10 -4
- package/src/transfer.js +22 -5
- package/src/ui/AddCard.js +236 -38
- package/src/ui/Ai.js +284 -0
- package/src/ui/AnswerInput.js +35 -7
- package/src/ui/App.js +263 -18
- package/src/ui/Cards.js +350 -0
- package/src/ui/Choice.js +6 -1
- package/src/ui/Decks.js +181 -15
- package/src/ui/Import.js +316 -0
- package/src/ui/Languages.js +48 -9
- package/src/ui/Preview.js +22 -1
- package/src/ui/Settings.js +23 -7
- package/src/ui/Trash.js +63 -0
- package/src/ui/Voice.js +238 -0
- package/src/ui/Welcome.js +80 -0
- package/src/voice.js +129 -0
- package/src/words.js +27 -4
package/src/ui/Languages.js
CHANGED
|
@@ -128,40 +128,79 @@ export function LanguagePicker({ deck, sample, languages, learning = null, home,
|
|
|
128
128
|
`;
|
|
129
129
|
|
|
130
130
|
const example = sample ? cutToWidth(side === "front" ? sample.front : sample.back.join(" / "), 24) : "";
|
|
131
|
-
const top = Math.max(0, Math.min(selected - Math.floor(SHOWN / 2), options.length - SHOWN));
|
|
132
131
|
const noteOf = (code) => {
|
|
133
132
|
if (code === UNKNOWN) return t("languages.mixedNote");
|
|
134
133
|
if (code !== languages[side]) return "";
|
|
135
134
|
return languages.guessed ? t("languages.guessed") : t("languages.current");
|
|
136
135
|
};
|
|
136
|
+
return html`<${LanguageList} title=${t(side === "front" ? "languages.frontQuestion" : "languages.backQuestion", { deck })} note=${example && t("languages.example", { sample: example })} query=${query} options=${options} selected=${selected} noteOf=${noteOf} />`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// the search and its list, as both pickers show them; `nameOf` names an option (a language by default)
|
|
140
|
+
// `step`: where in a series of questions this one is ("2/4"), said in the key hints
|
|
141
|
+
function LanguageList({ title, note, query, options, selected, noteOf, noMatch = options.length === 0, step = "", nameOf = (code) => (code === UNKNOWN ? t("languages.mixed") : languageName(code)) }) {
|
|
142
|
+
const panel = usePanelWidth();
|
|
143
|
+
const top = Math.max(0, Math.min(selected - Math.floor(SHOWN / 2), options.length - SHOWN));
|
|
137
144
|
return html`
|
|
138
145
|
<${Fragment}>
|
|
139
146
|
<${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
|
|
140
|
-
<${Text} bold color=${theme.accent}>${
|
|
141
|
-
${
|
|
147
|
+
<${Text} bold color=${theme.accent}>${title}<//>
|
|
148
|
+
${note && html`<${Text} dimColor>${note}<//>`}
|
|
142
149
|
<${Box} marginTop=${1}>
|
|
143
150
|
<${Box} flexShrink=${0}><${Text} dimColor>${t("languages.search")} <//><//>
|
|
144
151
|
<${Text} wrap="truncate-start">${query}<//>
|
|
145
152
|
<${Box} flexShrink=${0}><${Text} inverse> <//><//>
|
|
146
153
|
<//>
|
|
147
154
|
<${Box} flexDirection="column">
|
|
148
|
-
${
|
|
155
|
+
${noMatch && html`<${Text} dimColor wrap="truncate-end">${t("languages.noMatch", { query: cutToWidth(query, 20) })}<//>`}
|
|
149
156
|
<${Text} dimColor>${top > 0 ? " ↑" : " "}<//>
|
|
150
157
|
${options.slice(top, top + SHOWN).map((code, i) => {
|
|
151
158
|
const on = top + i === selected;
|
|
152
|
-
const
|
|
153
|
-
const note = noteOf(code);
|
|
159
|
+
const extra = noteOf(code);
|
|
154
160
|
return html`
|
|
155
161
|
<${Box} key=${code}>
|
|
156
|
-
<${Box} flexShrink=${0}><${Text} color=${on ? theme.accent : undefined} bold=${on}>${on ? "❯" : " "} ${
|
|
157
|
-
${
|
|
162
|
+
<${Box} flexShrink=${0}><${Text} color=${on ? theme.accent : undefined} bold=${on}>${on ? "❯" : " "} ${nameOf(code)}<//><//>
|
|
163
|
+
${extra && html`<${Text} dimColor wrap="truncate-end"> ${extra}<//>`}
|
|
158
164
|
<//>
|
|
159
165
|
`;
|
|
160
166
|
})}
|
|
161
167
|
<${Text} dimColor>${top + SHOWN < options.length ? " ↓" : " "}<//>
|
|
162
168
|
<//>
|
|
163
169
|
<//>
|
|
164
|
-
<${Box} paddingX=${1}><${KeyHints} text=${t("keyHints.languagePick")} /><//>
|
|
170
|
+
<${Box} paddingX=${1}><${KeyHints} text=${step ? `${step} · ${t("keyHints.languagePick")}` : t("keyHints.languagePick")} /><//>
|
|
165
171
|
<//>
|
|
166
172
|
`;
|
|
167
173
|
}
|
|
174
|
+
|
|
175
|
+
// One language, asked with a search (the welcome, /settings): `current` selected to begin with, and
|
|
176
|
+
// marked `currentNote` ("current", or "suggested" for a guess); `exclude` left out. `other`: a last
|
|
177
|
+
// option for "none of these" (UNKNOWN), named so and always there, whatever is typed. → onPick(code);
|
|
178
|
+
// Esc clears the search, then goes back (onBack).
|
|
179
|
+
export function LanguageSearch({ title, note, current = null, currentNote = t("languages.current"), exclude = [], other = null, step = "", onPick, onBack }) {
|
|
180
|
+
const listFor = (text) => [...matching(text, [current]).filter((code) => code !== UNKNOWN && !exclude.includes(code)), ...(other ? [UNKNOWN] : [])];
|
|
181
|
+
const [query, setQuery] = useState("");
|
|
182
|
+
const options = listFor(query);
|
|
183
|
+
const startAt = (list) => Math.max(0, current ? list.indexOf(current) : 0);
|
|
184
|
+
const [selected, setSelected] = useState(() => startAt(options));
|
|
185
|
+
|
|
186
|
+
function search(text) {
|
|
187
|
+
setQuery(text);
|
|
188
|
+
setSelected(text ? 0 : startAt(listFor(text)));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
useInput((input, key) => {
|
|
192
|
+
if (key.escape || key.leftArrow) return query ? search("") : onBack?.();
|
|
193
|
+
if (key.return) return options[selected] && onPick(options[selected]);
|
|
194
|
+
if (key.upArrow) return setSelected((i) => Math.max(0, i - 1));
|
|
195
|
+
if (key.downArrow) return setSelected((i) => Math.max(0, Math.min(options.length - 1, i + 1)));
|
|
196
|
+
if (key.backspace || key.delete) return query && search(query.slice(0, -1));
|
|
197
|
+
if (key.ctrl && input === "u") return search("");
|
|
198
|
+
const text = input.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
199
|
+
if (text && !key.ctrl && !key.meta) search(query + text);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const nameOf = (code) => (code === UNKNOWN ? other : languageName(code));
|
|
203
|
+
const noteOf = (code) => (code === current ? currentNote : "");
|
|
204
|
+
const noMatch = Boolean(query) && options.every((code) => code === UNKNOWN) && !(other && look(other).includes(look(query.trim())));
|
|
205
|
+
return html`<${LanguageList} title=${title} note=${note} query=${query} options=${options} selected=${selected} noteOf=${noteOf} nameOf=${nameOf} noMatch=${noMatch} step=${step} />`;
|
|
206
|
+
}
|
package/src/ui/Preview.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t } from "../i18n.js";
|
|
1
|
+
import { language, setLanguage, t } from "../i18n.js";
|
|
2
2
|
import { Fragment, useEffect, useMemo, useState } from "react";
|
|
3
3
|
import { Box, useApp, useInput } from "ink";
|
|
4
4
|
import { DEMO_CLOZE, DEMO_SENTENCES, DEMO_WORDS } from "../demo.js";
|
|
@@ -16,6 +16,7 @@ import { Badges } from "./Badges.js";
|
|
|
16
16
|
import { Confetti } from "./Confetti.js";
|
|
17
17
|
import { Evolution } from "./Evolution.js";
|
|
18
18
|
import { Hatch } from "./Hatch.js";
|
|
19
|
+
import { Welcome } from "./Welcome.js";
|
|
19
20
|
import { Header, Snore, useBlink, useMove } from "./Header.js";
|
|
20
21
|
import { html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
21
22
|
import { Mascot } from "./Mascot.js";
|
|
@@ -174,6 +175,24 @@ function ConfettiLab({ onBack }) {
|
|
|
174
175
|
`;
|
|
175
176
|
}
|
|
176
177
|
|
|
178
|
+
// The welcome after the hatching, step by step; nothing is kept, and the app's language comes back
|
|
179
|
+
// as it was when you leave
|
|
180
|
+
function WelcomeLab({ onBack }) {
|
|
181
|
+
const [before] = useState(language);
|
|
182
|
+
const [step, setStep] = useState("language");
|
|
183
|
+
const [settings, setSettings] = useState({ language: before, homeLanguage: null, learningLanguage: null });
|
|
184
|
+
const leave = () => {
|
|
185
|
+
setLanguage(before);
|
|
186
|
+
onBack();
|
|
187
|
+
};
|
|
188
|
+
useInput((input, key) => key.escape && step === "language" && leave());
|
|
189
|
+
return html`<${Welcome} step=${step} settings=${settings} onPreviewLanguage=${setLanguage} onStep=${(next, patch) => {
|
|
190
|
+
if (patch?.language) setLanguage(patch.language);
|
|
191
|
+
setSettings((now) => ({ ...now, ...patch }));
|
|
192
|
+
setStep(next);
|
|
193
|
+
}} onStart=${leave} />`;
|
|
194
|
+
}
|
|
195
|
+
|
|
177
196
|
// labels and hints come from the locale, read each time the menu is drawn
|
|
178
197
|
const scene = (id, text = () => [t(`preview.scenes.${id}.label`), t(`preview.scenes.${id}.hint`)]) => ({
|
|
179
198
|
id,
|
|
@@ -186,6 +205,7 @@ const scene = (id, text = () => [t(`preview.scenes.${id}.label`), t(`preview.sce
|
|
|
186
205
|
});
|
|
187
206
|
const SCENES = [
|
|
188
207
|
scene("hatch"),
|
|
208
|
+
scene("welcome"),
|
|
189
209
|
...STAGES.slice(1).map((stage, i) => scene(`evolve-${i + 1}`, () => [t("preview.evolution", { name: stage.name }), t("preview.evolutionHint", { count: stage.from })])),
|
|
190
210
|
...["mascot", "quiz", "add", "reveal", "cloze", "quiz-look", "unlock", "the-one", "die", "confetti", "stats", "badges", "summary"].map((id) => scene(id))
|
|
191
211
|
];
|
|
@@ -248,6 +268,7 @@ export function Preview({ alerts }) {
|
|
|
248
268
|
const to = Number(scene.slice("evolve-".length));
|
|
249
269
|
return wrap(html`<${Evolution} key=${run} from=${to - 1} to=${to} name=${STAGES[to].name} onClose=${back} />`);
|
|
250
270
|
}
|
|
271
|
+
if (scene === "welcome") return wrap(html`<${WelcomeLab} key=${run} onBack=${back} />`);
|
|
251
272
|
if (scene === "mascot") return wrap(html`<${MascotLab} onBack=${back} />`);
|
|
252
273
|
if (scene === "quiz-look") return wrap(html`<${QuizLookLab} onBack=${back} />`);
|
|
253
274
|
if (scene === "confetti") return wrap(html`<${ConfettiLab} onBack=${back} />`);
|
package/src/ui/Settings.js
CHANGED
|
@@ -3,6 +3,10 @@ import { Box, useInput } from "ink";
|
|
|
3
3
|
import { language, LANGUAGES, t } from "../i18n.js";
|
|
4
4
|
import { GOALS, INTERVALS, QUIET_PRESETS, RHYTHMS, TIP_MODES, VOLUMES, formatInterval } from "../settings.js";
|
|
5
5
|
import { formatQuiet } from "../quiet.js";
|
|
6
|
+
import { aiCostLabel } from "../ai.js";
|
|
7
|
+
import { voiceLabel } from "../voice.js";
|
|
8
|
+
import { languageName } from "../language.js";
|
|
9
|
+
import { elementName } from "../progress.js";
|
|
6
10
|
import { Bar, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
7
11
|
|
|
8
12
|
function VolumeValue({ value }) {
|
|
@@ -12,15 +16,19 @@ function VolumeValue({ value }) {
|
|
|
12
16
|
// label and hint come from settings.rows.<key> in the locale
|
|
13
17
|
const ALL_ROWS = [
|
|
14
18
|
{ key: "language", options: Object.keys(LANGUAGES), format: (code) => LANGUAGES[code] },
|
|
19
|
+
{ key: "homeLanguage", action: true, format: (code) => languageName(code) ?? t("settings.notSet") },
|
|
20
|
+
{ key: "ai", action: true, format: aiCostLabel },
|
|
15
21
|
{ key: "rhythm", options: RHYTHMS, format: (v) => t(`settings.rhythms.${v}`) },
|
|
16
22
|
{ key: "everyMs", options: INTERVALS, format: formatInterval },
|
|
17
23
|
{ key: "sound", options: [true, false], format: (v) => t(v ? "settings.on" : "settings.off"), toggle: true },
|
|
18
24
|
{ key: "speak", options: [false, true], format: (v) => t(v ? "settings.on" : "settings.off"), toggle: true },
|
|
25
|
+
{ key: "voice", action: true, format: voiceLabel },
|
|
19
26
|
{ key: "volume", options: VOLUMES, format: (v) => html`<${VolumeValue} value=${v} />` },
|
|
20
27
|
{ key: "dailyGoal", options: GOALS, format: (n) => t("settings.goal", { n }) },
|
|
21
28
|
{ key: "tips", options: TIP_MODES, format: (v) => t(`settings.tipModes.${v}`) },
|
|
22
29
|
{ key: "skipWeekends", options: [true, false], format: (v) => t(v ? "settings.weekendsRest" : "settings.weekendsCount"), toggle: true },
|
|
23
30
|
{ key: "quiet", options: QUIET_PRESETS, format: formatQuiet },
|
|
31
|
+
{ key: "dragonLook", action: true, onlyWith: "element", format: (element) => elementName(element) },
|
|
24
32
|
{ key: "reset", action: true, format: () => t("settings.pressEnter") }
|
|
25
33
|
];
|
|
26
34
|
const label = (row) => t(`settings.rows.${row.key}.label`);
|
|
@@ -31,13 +39,21 @@ const nearestIndex = (options, value) => {
|
|
|
31
39
|
return options.reduce((best, option, i) => (Math.abs(option - value) < Math.abs(options[best] - value) ? i : best), 0);
|
|
32
40
|
};
|
|
33
41
|
|
|
34
|
-
// Changes apply and persist immediately; there is no save step. Action rows (Reset) open
|
|
35
|
-
// screen through `onAction`; without it (the preview) they are left out.
|
|
36
|
-
|
|
42
|
+
// Changes apply and persist immediately; there is no save step. Action rows (Your language, Reset) open
|
|
43
|
+
// their own screen through `onAction(key)`; without it (the preview) they are left out.
|
|
44
|
+
// `startRow` and `onRow`: the row to open on, and each row moved to, so a screen opened from a row
|
|
45
|
+
// comes back to it
|
|
46
|
+
// `element`: the dragon's element, if the die gave it one (the row to go back to the default look)
|
|
47
|
+
export function Settings({ settings, element = null, onChange, onClose, onAction, startRow = 0, onRow = () => {} }) {
|
|
37
48
|
const panel = usePanelWidth();
|
|
38
|
-
const [row,
|
|
39
|
-
const
|
|
40
|
-
|
|
49
|
+
const [row, setRowState] = useState(startRow);
|
|
50
|
+
const setRow = (next) => setRowState((r) => {
|
|
51
|
+
const value = typeof next === "function" ? next(r) : next;
|
|
52
|
+
onRow(value);
|
|
53
|
+
return value;
|
|
54
|
+
});
|
|
55
|
+
const ROWS = (onAction ? ALL_ROWS : ALL_ROWS.filter((item) => !item.action)).filter((item) => item.onlyWith !== "element" || element);
|
|
56
|
+
const values = { ...settings, language: settings.language ?? language(), dragonLook: element };
|
|
41
57
|
const labelWidth = Math.max(...ROWS.map((item) => label(item).length)) + 2;
|
|
42
58
|
|
|
43
59
|
useInput((input, key) => {
|
|
@@ -64,7 +80,7 @@ export function Settings({ settings, onChange, onClose, onAction }) {
|
|
|
64
80
|
(item, i) => html`
|
|
65
81
|
<${Box} key=${item.key}>
|
|
66
82
|
<${Box} flexShrink=${0}><${Text} color=${i === row ? theme.accent : undefined} bold=${i === row}>${i === row ? "❯" : " "} ${label(item).padEnd(labelWidth)}<//><//>
|
|
67
|
-
<${Text} color=${i === row ? theme.accent : undefined}>${i === row && !item.action ? "❮ " : " "}${item.format(values[item.key])}${i === row && !item.action ? " ❯" : ""}<//>
|
|
83
|
+
<${Text} wrap="truncate-end" color=${i === row ? theme.accent : undefined}>${i === row && !item.action ? "❮ " : " "}${item.format(values[item.key])}${i === row && !item.action ? " ❯" : ""}<//>
|
|
68
84
|
<//>
|
|
69
85
|
`
|
|
70
86
|
)}
|
package/src/ui/Trash.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Fragment, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { Box, useInput } from "ink";
|
|
3
|
+
import { t } from "../i18n.js";
|
|
4
|
+
import { dayKey } from "../dates.js";
|
|
5
|
+
import { TRASH_DAYS } from "../library.js";
|
|
6
|
+
import { formatDay } from "../vacation.js";
|
|
7
|
+
import { Choice } from "./Choice.js";
|
|
8
|
+
import { cutToWidth, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
9
|
+
import { plainFront } from "../cardline.js";
|
|
10
|
+
|
|
11
|
+
// a deleted deck by its name, a card as "front = back", several cards by how many
|
|
12
|
+
const labelOf = (entry) => (entry.kind !== "cards" ? entry.name : entry.card ? cutToWidth(entry.card.cloze ? plainFront(entry.card.front) : `${entry.card.front} = ${entry.card.back.join("; ")}`, 34) : t("count.cards", { count: entry.cards }));
|
|
13
|
+
// /trash: decks and cards deleted in the last TRASH_DAYS, newest first; Enter brings one back with its
|
|
14
|
+
// progress (`onRestore`(id) → { deck, kind, cards, skipped }): a deck whole, cards into their deck.
|
|
15
|
+
export function Trash({ actions, onRestore, onClose }) {
|
|
16
|
+
const panel = usePanelWidth();
|
|
17
|
+
const [entries, setEntries] = useState(null);
|
|
18
|
+
const [selected, setSelected] = useState(0);
|
|
19
|
+
const [message, setMessage] = useState("");
|
|
20
|
+
const [error, setError] = useState("");
|
|
21
|
+
const busy = useRef(false);
|
|
22
|
+
|
|
23
|
+
const load = () => actions.trash().then(setEntries, (err) => setError(err.message));
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
load();
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
useInput((input, key) => {
|
|
29
|
+
if (entries && !entries.length && (key.escape || key.return || input === "q")) onClose();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
async function restore(i) {
|
|
33
|
+
if (busy.current) return;
|
|
34
|
+
busy.current = true;
|
|
35
|
+
try {
|
|
36
|
+
const back = await onRestore(entries[i].id);
|
|
37
|
+
setMessage(back.kind === "cards" ? t("trash.cardsRestored", { count: back.cards, deck: back.deck }) + (back.skipped ? ` ${t("trash.skipped", { count: back.skipped })}` : "") : t("trash.restored", { deck: back.deck }));
|
|
38
|
+
await load();
|
|
39
|
+
setSelected((s) => Math.max(0, Math.min(s, entries.length - 2)));
|
|
40
|
+
} catch (err) {
|
|
41
|
+
setError(err.message);
|
|
42
|
+
} finally {
|
|
43
|
+
busy.current = false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return html`
|
|
48
|
+
<${Fragment}>
|
|
49
|
+
<${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
|
|
50
|
+
<${Text} bold color=${theme.accent}>${t("trash.title")}<//>
|
|
51
|
+
<${Text} dimColor>${t("trash.note", { days: TRASH_DAYS })}<//>
|
|
52
|
+
<${Box} marginTop=${1}>
|
|
53
|
+
${entries?.length
|
|
54
|
+
? html`<${Choice} options=${entries.map((entry) => ({ label: labelOf(entry), note: t(entry.kind === "cards" ? "trash.cardsRow" : "trash.row", { cards: t("count.cards", { count: entry.cards }), deck: entry.name, deleted: formatDay(dayKey(entry.deletedAt)), until: formatDay(dayKey(entry.until)) }) }))} selected=${Math.min(selected, entries.length - 1)} onMove=${setSelected} onPick=${restore} onCancel=${onClose} />`
|
|
55
|
+
: entries && html`<${Text}>${t("trash.empty")}<//>`}
|
|
56
|
+
<//>
|
|
57
|
+
${message && html`<${Box} marginTop=${1}><${Text} color=${theme.good}>${message}<//><//>`}
|
|
58
|
+
${error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`}
|
|
59
|
+
<//>
|
|
60
|
+
<${Box} paddingX=${1}><${KeyHints} text=${entries?.length ? t("keyHints.trash") : t("keyHints.back")} /><//>
|
|
61
|
+
<//>
|
|
62
|
+
`;
|
|
63
|
+
}
|
package/src/ui/Voice.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { Fragment, useRef, useState } from "react";
|
|
2
|
+
import { Box, useInput } from "ink";
|
|
3
|
+
import { language, t } from "../i18n.js";
|
|
4
|
+
import { forgetKey, maskKey, saveKey } from "../ai.js";
|
|
5
|
+
import { ELEVENLABS, findVoiceKey, listVoices, speech, voiceLabel } from "../voice.js";
|
|
6
|
+
import { fold } from "../judge.js";
|
|
7
|
+
import { AnswerInput } from "./AnswerInput.js";
|
|
8
|
+
import { Choice, choiceHints } from "./Choice.js";
|
|
9
|
+
import { cutToWidth, html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
10
|
+
|
|
11
|
+
const SHOWN = 8; // voices listed at a time
|
|
12
|
+
const look = (text) => fold(text.toLowerCase());
|
|
13
|
+
const SHORT = ["noKey", "badKey", "noCredit", "noVoice"];
|
|
14
|
+
|
|
15
|
+
// /settings → Voice: the system's voices (free, built in) or a natural one from ElevenLabs with your own
|
|
16
|
+
// key. For ElevenLabs: the key, the voice, then a short sample read in it; only then is anything saved.
|
|
17
|
+
// `current`: settings.voice. onSave(voice | null) keeps the choice. `play(file)`: plays the sample.
|
|
18
|
+
// `speakOn`: Read aloud is on already (picking a voice turns it on). Esc goes back a step, from the
|
|
19
|
+
// first one it leaves.
|
|
20
|
+
export function VoiceSetup({ current, onSave, onClose, play = () => {}, speakOn = true }) {
|
|
21
|
+
const panel = usePanelWidth();
|
|
22
|
+
const [step, setStep] = useState("provider");
|
|
23
|
+
const [selected, setSelected] = useState(current ? 1 : 0);
|
|
24
|
+
const [key, setKey] = useState(null); // { key, from: "env" | "saved" | "typed", env? }
|
|
25
|
+
const [voices, setVoices] = useState(null);
|
|
26
|
+
const [voice, setVoice] = useState(null);
|
|
27
|
+
const [query, setQuery] = useState("");
|
|
28
|
+
const [error, setError] = useState("");
|
|
29
|
+
const [message, setMessage] = useState("");
|
|
30
|
+
const history = useRef([]);
|
|
31
|
+
const job = useRef(0); // a newer step makes an older request's answer stale
|
|
32
|
+
const request = useRef(null); // the request in flight, cancelled when it goes stale (a sample is billed)
|
|
33
|
+
|
|
34
|
+
const go = (next, pick = 0) => {
|
|
35
|
+
const at = history.current.indexOf(next);
|
|
36
|
+
if (at >= 0) history.current = history.current.slice(0, at);
|
|
37
|
+
else if (step !== next && step !== "checking" && step !== "failed") history.current.push(step);
|
|
38
|
+
setError("");
|
|
39
|
+
setMessage("");
|
|
40
|
+
setSelected(pick);
|
|
41
|
+
setStep(next);
|
|
42
|
+
};
|
|
43
|
+
const back = () => {
|
|
44
|
+
job.current += 1;
|
|
45
|
+
request.current?.abort();
|
|
46
|
+
setError("");
|
|
47
|
+
setMessage("");
|
|
48
|
+
const last = history.current.pop();
|
|
49
|
+
if (!last) return onClose();
|
|
50
|
+
setSelected(last === "provider" ? 1 : 0);
|
|
51
|
+
setStep(last);
|
|
52
|
+
};
|
|
53
|
+
const stillOn = () => {
|
|
54
|
+
const mine = ++job.current;
|
|
55
|
+
request.current?.abort();
|
|
56
|
+
request.current = new AbortController();
|
|
57
|
+
return () => mine === job.current;
|
|
58
|
+
};
|
|
59
|
+
// here the message says what's wrong, not where to fix it (this is the place)
|
|
60
|
+
const said = (err) => (SHORT.includes(err.code) ? t(`voice.short.${err.code}`, { env: ELEVENLABS.keyEnv }) : err.message);
|
|
61
|
+
|
|
62
|
+
async function pickProvider(index) {
|
|
63
|
+
if (index === 0) {
|
|
64
|
+
if (current) onSave(null);
|
|
65
|
+
return onClose(current ? t("voice.systemSet") : undefined);
|
|
66
|
+
}
|
|
67
|
+
const fresh = stillOn();
|
|
68
|
+
const found = await findVoiceKey();
|
|
69
|
+
if (!fresh()) return;
|
|
70
|
+
setKey(found);
|
|
71
|
+
if (found?.from === "env") return loadVoices(found);
|
|
72
|
+
go(found ? "keep-key" : "key");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function loadVoices(withKey = key) {
|
|
76
|
+
go("voices");
|
|
77
|
+
const fresh = stillOn();
|
|
78
|
+
setVoices(null);
|
|
79
|
+
setQuery("");
|
|
80
|
+
try {
|
|
81
|
+
const list = await listVoices({ key: withKey?.key ?? null, url: current?.url, signal: request.current.signal });
|
|
82
|
+
if (!fresh()) return;
|
|
83
|
+
setVoices(list);
|
|
84
|
+
setSelected(Math.max(0, list.findIndex((item) => item.id === current?.id)));
|
|
85
|
+
} catch (err) {
|
|
86
|
+
if (!fresh()) return;
|
|
87
|
+
setVoices([]);
|
|
88
|
+
if (["badKey", "noKey"].includes(err.code) && withKey?.from !== "env") {
|
|
89
|
+
const at = history.current.indexOf("key");
|
|
90
|
+
if (at >= 0) history.current = history.current.slice(0, at);
|
|
91
|
+
setSelected(0);
|
|
92
|
+
setStep("key");
|
|
93
|
+
}
|
|
94
|
+
setError(said(err));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function check(picked) {
|
|
99
|
+
const chosen = { provider: "elevenlabs", id: picked.id, name: picked.name, ...(current?.url ? { url: current.url } : {}) };
|
|
100
|
+
setVoice(chosen);
|
|
101
|
+
go("checking");
|
|
102
|
+
const fresh = stillOn();
|
|
103
|
+
try {
|
|
104
|
+
const file = await speech(chosen, t("voice.sample"), language(), { key: key?.key ?? null, signal: request.current.signal });
|
|
105
|
+
if (!fresh()) return;
|
|
106
|
+
play(file);
|
|
107
|
+
if (key?.from === "typed") await saveKey("elevenlabs", key.key);
|
|
108
|
+
onSave(chosen);
|
|
109
|
+
onClose(t(speakOn ? "voice.ready" : "voice.readyOn", { label: voiceLabel(chosen) }));
|
|
110
|
+
} catch (err) {
|
|
111
|
+
if (!fresh()) return;
|
|
112
|
+
setError(said(err));
|
|
113
|
+
setStep("failed");
|
|
114
|
+
setSelected(0);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const found = (voices ?? []).filter((item) => !query || look(`${item.name} ${item.note}`).includes(look(query)));
|
|
119
|
+
|
|
120
|
+
useInput((input, k) => {
|
|
121
|
+
if ((step === "checking" || (step === "voices" && voices === null)) && k.escape) return back();
|
|
122
|
+
if (step !== "voices" || voices === null) return;
|
|
123
|
+
if (k.escape) return query ? (setQuery(""), setSelected(0)) : back();
|
|
124
|
+
if (k.upArrow) return setSelected((i) => Math.max(0, i - 1));
|
|
125
|
+
if (k.downArrow) return setSelected((i) => Math.min(found.length - 1, i + 1));
|
|
126
|
+
if (k.return) return found[selected] && check(found[selected]);
|
|
127
|
+
if (k.backspace || k.delete) return query && (setQuery((q) => q.slice(0, -1)), setSelected(0));
|
|
128
|
+
const text = input.replace(/[\u0000-\u001f\u007f]/g, "");
|
|
129
|
+
if (text && !k.ctrl && !k.meta) {
|
|
130
|
+
setQuery((q) => q + text);
|
|
131
|
+
setSelected(0);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const problem = error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`;
|
|
136
|
+
const frame = (title, body, hints, { errorAbove = false } = {}) => html`
|
|
137
|
+
<${Fragment}>
|
|
138
|
+
<${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
|
|
139
|
+
<${Text} bold color=${theme.accent}>${title}<//>
|
|
140
|
+
${errorAbove && problem}
|
|
141
|
+
${body}
|
|
142
|
+
${message && html`<${Box} marginTop=${1}><${Text} color=${theme.good}>${message}<//><//>`}
|
|
143
|
+
${!errorAbove && problem}
|
|
144
|
+
<//>
|
|
145
|
+
<${Box} paddingX=${1}><${KeyHints} text=${hints} /><//>
|
|
146
|
+
<//>
|
|
147
|
+
`;
|
|
148
|
+
|
|
149
|
+
if (step === "provider") {
|
|
150
|
+
const options = [{ label: t("voice.system"), note: t("voice.systemNote") }, { label: "ElevenLabs", note: t("voice.elevenNote") }];
|
|
151
|
+
return frame(
|
|
152
|
+
t("voice.title"),
|
|
153
|
+
html`
|
|
154
|
+
<${Box} flexDirection="column">
|
|
155
|
+
<${Text} dimColor>${t("voice.note")}<//>
|
|
156
|
+
<${Box} marginTop=${1}><${Text} dimColor>${t("voice.now", { label: voiceLabel(current) })}<//><//>
|
|
157
|
+
<${Box} marginTop=${1}><${Choice} options=${options} selected=${selected} onMove=${setSelected} onPick=${(i) => pickProvider(i).catch((err) => setError(err.message))} onCancel=${() => onClose()} /><//>
|
|
158
|
+
<//>
|
|
159
|
+
`,
|
|
160
|
+
choiceHints("back")
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (step === "keep-key") {
|
|
165
|
+
const options = [{ label: t("ai.keepKey"), note: maskKey(key.key) }, { label: t("ai.newKey") }, { label: t("ai.forgetKey"), danger: true }];
|
|
166
|
+
return frame(
|
|
167
|
+
t("voice.keyTitle"),
|
|
168
|
+
html`<${Box} marginTop=${1}><${Choice} options=${options} selected=${selected} onMove=${setSelected} onPick=${(i) => {
|
|
169
|
+
if (i === 0) return loadVoices();
|
|
170
|
+
if (i === 1) return go("key");
|
|
171
|
+
forgetKey("elevenlabs").then(() => {
|
|
172
|
+
setKey(null);
|
|
173
|
+
go("key");
|
|
174
|
+
setMessage(t("ai.keyForgotten", { provider: "ElevenLabs" }));
|
|
175
|
+
}, (err) => setError(err.message));
|
|
176
|
+
}} onCancel=${back} /><//>`,
|
|
177
|
+
choiceHints("back")
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (step === "key")
|
|
182
|
+
return frame(
|
|
183
|
+
t("voice.keyTitle"),
|
|
184
|
+
html`
|
|
185
|
+
<${Box} flexDirection="column">
|
|
186
|
+
<${Text} dimColor>${t("voice.keyNote", { env: ELEVENLABS.keyEnv, site: ELEVENLABS.site })}<//>
|
|
187
|
+
<${Box} marginTop=${1}><${AnswerInput} key="voice-key" plain secret escLeaves onChange=${() => (setError(""), setMessage(""))} onExit=${back} onSubmit=${(text) => {
|
|
188
|
+
const typed = text.trim();
|
|
189
|
+
if (!typed) return setError(t("ai.needKey"));
|
|
190
|
+
const next = { key: typed, from: "typed" };
|
|
191
|
+
setKey(next);
|
|
192
|
+
loadVoices(next);
|
|
193
|
+
}} /><//>
|
|
194
|
+
<//>
|
|
195
|
+
`,
|
|
196
|
+
t("keyHints.importName")
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
if (step === "voices") {
|
|
200
|
+
const top = Math.max(0, Math.min(selected - Math.floor(SHOWN / 2), found.length - SHOWN));
|
|
201
|
+
return frame(
|
|
202
|
+
t("voice.pickTitle"),
|
|
203
|
+
html`
|
|
204
|
+
<${Box} flexDirection="column">
|
|
205
|
+
<${Text} dimColor>${[key?.from === "env" && t("ai.keyFromEnv", { env: key.env }), t("voice.pickNote")].filter(Boolean).join(" ")}<//>
|
|
206
|
+
${voices?.length === 0 && !error && html`<${Box} marginTop=${1}><${Text}>${t("voice.noVoices")}<//><//>`}
|
|
207
|
+
${voices === null
|
|
208
|
+
? html`<${Box} marginTop=${1}><${Text} dimColor>${t("voice.loading")}<//><//>`
|
|
209
|
+
: voices.length > 0 && html`
|
|
210
|
+
<${Box} flexDirection="column" marginTop=${1}>
|
|
211
|
+
${(voices.length > SHOWN || query) && html`<${Box}><${Box} flexShrink=${0}><${Text} dimColor>${t("languages.search")} <//><//><${Text} wrap="truncate-start">${query}<//><${Box} flexShrink=${0}><${Text} inverse> <//><//><//>`}
|
|
212
|
+
<${Text} dimColor>${top > 0 ? " ↑" : " "}<//>
|
|
213
|
+
${found.slice(top, top + SHOWN).map((item, i) => {
|
|
214
|
+
const on = top + i === selected;
|
|
215
|
+
const extra = item.id === current?.id ? t("voice.current") : item.note;
|
|
216
|
+
return html`<${Text} key=${item.id} color=${on ? theme.accent : undefined} bold=${on} wrap="truncate-end">${on ? "❯" : " "} ${cutToWidth(item.name, panel - 12)} ${extra ? html`<${Text} dimColor>· ${extra}<//>` : ""}<//>`;
|
|
217
|
+
})}
|
|
218
|
+
<${Text} dimColor>${top + SHOWN < found.length ? " ↓" : " "}<//>
|
|
219
|
+
<//>
|
|
220
|
+
`}
|
|
221
|
+
<//>
|
|
222
|
+
`,
|
|
223
|
+
voices && voices.length > SHOWN ? t("keyHints.modelPick") : choiceHints("back")
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (step === "checking")
|
|
228
|
+
return frame(t("voice.pickTitle"), html`<${Box} marginTop=${1}><${Text} dimColor>${t("voice.checking", { label: voiceLabel(voice) })}<//><//>`, t("keyHints.back"));
|
|
229
|
+
|
|
230
|
+
// failed: what now (a key from the environment is changed there, not here)
|
|
231
|
+
const options = [{ label: t("ai.tryAgain") }, { label: t("voice.otherVoice") }, ...(key?.from === "env" ? [] : [{ label: t("ai.otherKey") }])];
|
|
232
|
+
return frame(
|
|
233
|
+
t("voice.failedTitle", { label: voiceLabel(voice) }),
|
|
234
|
+
html`<${Box} marginTop=${1}><${Choice} options=${options} selected=${selected} onMove=${setSelected} onPick=${(i) => (i === 0 ? check(voice) : i === 1 ? loadVoices() : go("key"))} onCancel=${back} /><//>`,
|
|
235
|
+
choiceHints("back"),
|
|
236
|
+
{ errorAbove: true }
|
|
237
|
+
);
|
|
238
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Fragment, useState } from "react";
|
|
2
|
+
import { Box, useInput } from "ink";
|
|
3
|
+
import { entry, language, LANGUAGES, t } from "../i18n.js";
|
|
4
|
+
import { FIRST_WORDS_LANGUAGES, hasFirstWords } from "../firstwords.js";
|
|
5
|
+
import { languageName, UNKNOWN } from "../language.js";
|
|
6
|
+
import { Choice, choiceHints } from "./Choice.js";
|
|
7
|
+
import { html, KeyHints, Text, theme, usePanelWidth } from "./kit.js";
|
|
8
|
+
import { LanguageSearch } from "./Languages.js";
|
|
9
|
+
|
|
10
|
+
export const WELCOME_STEPS = ["language", "home", "learning", "start"];
|
|
11
|
+
export const FIRST_WORDS_COUNT = 30;
|
|
12
|
+
|
|
13
|
+
// After the hatching, before the first question: the app's language, the one you speak, the one you're
|
|
14
|
+
// learning, and a way to start. Every answer is kept as it's given (onStep), so a welcome left halfway
|
|
15
|
+
// goes on from that step. `onPreviewLanguage` shows the texts in a language while it's only selected;
|
|
16
|
+
// onStart(how): "words" (the first words of the pair), "ai" (a list from AI help, set up first if it
|
|
17
|
+
// isn't), "tour", "import" (cards you have, /import) or "own" (a deck of your own, in /add).
|
|
18
|
+
// `tour`: whether there's still a tour to show (a deck of your own may have taken its place).
|
|
19
|
+
export function Welcome({ step, settings, tour = true, onPreviewLanguage, onStep, onStart }) {
|
|
20
|
+
const panel = usePanelWidth();
|
|
21
|
+
const codes = Object.keys(LANGUAGES);
|
|
22
|
+
const [selected, setSelected] = useState(() => Math.max(0, codes.indexOf(settings.language ?? language())));
|
|
23
|
+
const [startAt, setStartAt] = useState(0);
|
|
24
|
+
const home = settings.homeLanguage ?? language();
|
|
25
|
+
const learning = settings.learningLanguage === UNKNOWN ? null : settings.learningLanguage; // "something else" is kept as UNKNOWN
|
|
26
|
+
const counter = `${WELCOME_STEPS.indexOf(step) + 1}/${WELCOME_STEPS.length}`;
|
|
27
|
+
const hints = (text) => `${counter} · ${text}`;
|
|
28
|
+
|
|
29
|
+
// ← goes back on the last step too, as it does in the language lists before it
|
|
30
|
+
useInput((input, key) => {
|
|
31
|
+
if (step === "start" && key.leftArrow) onStep("learning");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (step === "home")
|
|
35
|
+
return html`<${LanguageSearch} key="home" title=${t("welcome.homeTitle")} note=${t("welcome.homeNote")} current=${home} currentNote=${settings.homeLanguage ? t("languages.current") : t("welcome.suggested")} step=${counter} onBack=${() => onStep("language")} onPick=${(code) => onStep("learning", { homeLanguage: code, ...(code === learning && { learningLanguage: null }) })} />`;
|
|
36
|
+
if (step === "learning")
|
|
37
|
+
return html`<${LanguageSearch} key="learning" title=${t("welcome.learningTitle")} note=${t("welcome.learningNote")} current=${settings.learningLanguage !== home ? settings.learningLanguage : null} exclude=${[home]} other=${t("welcome.notLanguage")} step=${counter} onBack=${() => onStep("home")} onPick=${(code) => onStep("start", { learningLanguage: code })} />`;
|
|
38
|
+
|
|
39
|
+
if (step === "start") {
|
|
40
|
+
const words = learning && hasFirstWords(home, learning);
|
|
41
|
+
const options = [
|
|
42
|
+
words && { id: "words", label: t("welcome.start.words", { count: FIRST_WORDS_COUNT }), note: t("welcome.start.wordsNote", { language: languageName(learning), home: languageName(home) }) },
|
|
43
|
+
learning && { id: "ai", label: t("welcome.start.ai"), note: t("welcome.start.aiNote") },
|
|
44
|
+
tour && { id: "tour", label: t("welcome.start.tour"), note: t("welcome.start.tourNote", { count: entry("tour.cards").length }) },
|
|
45
|
+
{ id: "import", label: t("welcome.start.import"), note: t("welcome.start.importNote") },
|
|
46
|
+
{ id: "own", label: t("welcome.start.own"), note: t("welcome.start.ownNote") }
|
|
47
|
+
].filter(Boolean);
|
|
48
|
+
// no ready-made words for this pair: said why, naming the language that has none
|
|
49
|
+
const names = () => new Intl.ListFormat(language(), { type: "conjunction" }).format(FIRST_WORDS_LANGUAGES.map((code) => languageName(code)));
|
|
50
|
+
const missing = !learning || words ? null : FIRST_WORDS_LANGUAGES.includes(learning) ? "home" : "learning";
|
|
51
|
+
const why = missing && (missing === "home" ? t("welcome.start.noWordsHome", { languages: names(), language: languageName(home) }) : t("welcome.start.noWords", { languages: names(), language: languageName(learning) }));
|
|
52
|
+
return html`
|
|
53
|
+
<${Fragment}>
|
|
54
|
+
<${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
|
|
55
|
+
<${Text} bold color=${theme.accent}>${t("welcome.startTitle")}<//>
|
|
56
|
+
<${Text} dimColor>${t("welcome.startNote")}<//>
|
|
57
|
+
<${Box} marginTop=${1}><${Choice} options=${options} selected=${Math.min(startAt, options.length - 1)} onMove=${setStartAt} onPick=${(i) => onStart(options[i].id)} onCancel=${() => onStep("learning")} /><//>
|
|
58
|
+
${why && html`<${Box} marginTop=${1}><${Text} dimColor>${why}<//><//>`}
|
|
59
|
+
<//>
|
|
60
|
+
<${Box} paddingX=${1}><${KeyHints} text=${hints(choiceHints("back"))} /><//>
|
|
61
|
+
<//>
|
|
62
|
+
`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// the app's language: each named in itself, and the texts follow the one selected
|
|
66
|
+
const move = (i) => {
|
|
67
|
+
setSelected(i);
|
|
68
|
+
onPreviewLanguage(codes[i]);
|
|
69
|
+
};
|
|
70
|
+
return html`
|
|
71
|
+
<${Fragment}>
|
|
72
|
+
<${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
|
|
73
|
+
<${Text} bold color=${theme.accent}>${t("welcome.languageTitle")}<//>
|
|
74
|
+
<${Text} dimColor>${t("welcome.languageNote")}<//>
|
|
75
|
+
<${Box} marginTop=${1}><${Choice} options=${codes.map((code) => ({ label: LANGUAGES[code] }))} selected=${selected} onMove=${move} onPick=${(i) => onStep("home", { language: codes[i] })} /><//>
|
|
76
|
+
<//>
|
|
77
|
+
<${Box} paddingX=${1}><${KeyHints} text=${hints(t("choice.keys"))} /><//>
|
|
78
|
+
<//>
|
|
79
|
+
`;
|
|
80
|
+
}
|