dracosh 0.1.0 → 0.2.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.
Files changed (51) hide show
  1. package/README.md +42 -20
  2. package/assets/sounds/badge-1.wav +0 -0
  3. package/assets/sounds/badge-2.wav +0 -0
  4. package/assets/sounds/badge-3.wav +0 -0
  5. package/assets/sounds/badge-4.wav +0 -0
  6. package/data/tips.json +31 -1361
  7. package/package.json +1 -1
  8. package/src/alerts.js +4 -3
  9. package/src/backup.js +95 -0
  10. package/src/cli.js +97 -16
  11. package/src/commands.js +7 -7
  12. package/src/judge.js +33 -12
  13. package/src/library.js +127 -0
  14. package/src/lock.js +63 -0
  15. package/src/manage.js +75 -0
  16. package/src/pause.js +23 -0
  17. package/src/progress.js +145 -53
  18. package/src/quiet.js +1 -1
  19. package/src/scheduler.js +38 -26
  20. package/src/session.js +128 -30
  21. package/src/settings.js +4 -9
  22. package/src/starter.js +38 -0
  23. package/src/store.js +55 -16
  24. package/src/tips.js +7 -24
  25. package/src/ui/AddCard.js +172 -0
  26. package/src/ui/AnswerInput.js +8 -5
  27. package/src/ui/App.js +211 -71
  28. package/src/ui/BadgeUnlock.js +50 -0
  29. package/src/ui/Badges.js +75 -17
  30. package/src/ui/Choice.js +33 -0
  31. package/src/ui/Companion.js +20 -35
  32. package/src/ui/Decks.js +49 -0
  33. package/src/ui/DieRoll.js +104 -0
  34. package/src/ui/Evolution.js +6 -5
  35. package/src/ui/Hatch.js +5 -4
  36. package/src/ui/Header.js +7 -6
  37. package/src/ui/Help.js +68 -0
  38. package/src/ui/Mascot.js +88 -38
  39. package/src/ui/Missed.js +6 -5
  40. package/src/ui/Preview.js +92 -17
  41. package/src/ui/Reset.js +72 -0
  42. package/src/ui/Settings.js +15 -11
  43. package/src/ui/Stats.js +10 -7
  44. package/src/ui/Summary.js +12 -5
  45. package/src/ui/TheOne.js +121 -0
  46. package/src/ui/icons.js +156 -24
  47. package/src/ui/kit.js +58 -1
  48. package/src/vacation.js +7 -2
  49. package/src/words.js +21 -14
  50. package/assets/sounds/badge.wav +0 -0
  51. package/src/ui/Tip.js +0 -25
@@ -0,0 +1,172 @@
1
+ import { Fragment, useEffect, useState } from "react";
2
+ import { Box, Text } from "ink";
3
+ import { MAX_DECK_NAME } from "../library.js";
4
+ import { SLASH_ANSWER, SLASH_FRONT } from "../manage.js";
5
+ import { AnswerInput } from "./AnswerInput.js";
6
+ import { Choice, CHOICE_KEYS } from "./Choice.js";
7
+ import { html, KeyHints, theme, usePanelWidth } from "./kit.js";
8
+
9
+ const FIELDS = {
10
+ deck: { label: "Name your deck", hint: "Polish, Biology, Capitals… whatever you want to learn" },
11
+ front: { label: "Front", hint: "the word or question, e.g. kot" },
12
+ back: { label: "Back", hint: "the answer; separate several with commas, e.g. cat, kitty" },
13
+ example: { label: "Example", hint: "optional: a sentence that helps you remember. Enter skips it" }
14
+ };
15
+
16
+ const splitAnswers = (text) => text.split(",").map((part) => part.trim()).filter(Boolean);
17
+
18
+ const NEW_DECK = "+ New deck";
19
+
20
+ // /add: a small form inside the app. You pick one of your decks or start a new one (your first one
21
+ // replaces the tour), then add cards one after another until Esc. `actions` reads and writes the library.
22
+ // `startNew` skips the deck list and goes straight to a new deck ("+ New deck" in /decks).
23
+ export function AddCard({ current, actions, onDone, startNew = false }) {
24
+ const panel = usePanelWidth();
25
+ const [step, setStep] = useState("loading"); // loading | pick | deck | ways | front | back | example | saving
26
+ const [decks, setDecks] = useState([]);
27
+ const [picked, setPicked] = useState(0);
28
+ const [ways, setWays] = useState(0);
29
+ const [bothWays, setBothWays] = useState(false); // the deck's front is an answer too
30
+ const [deck, setDeck] = useState(null);
31
+ const [newDeck, setNewDeck] = useState(null); // { name, removed } once a first deck was started here
32
+ const [draft, setDraft] = useState({ front: "", back: [] });
33
+ const [message, setMessage] = useState("");
34
+ const [error, setError] = useState("");
35
+ const [added, setAdded] = useState(0);
36
+
37
+ useEffect(() => {
38
+ actions.ownDecks().then(
39
+ (own) => {
40
+ setDecks(own);
41
+ setPicked(Math.max(0, own.findIndex((d) => d.name === current)));
42
+ setStep(own.length && !startNew ? "pick" : "deck");
43
+ },
44
+ (err) => setError(err.message)
45
+ );
46
+ }, []);
47
+
48
+ // `dropped`: a card was half written (its front typed) when Esc ended /add
49
+ const finish = () => onDone({ deck: newDeck?.name ?? null, removed: newDeck?.removed ?? [], added, dropped: Boolean(draft.front) && (step === "back" || step === "example") });
50
+
51
+ async function startDeck(bothWays) {
52
+ setStep("saving");
53
+ try {
54
+ const { deck: created, removed } = await actions.startDeck({ name: deck, bothWays });
55
+ setDeck(created.name);
56
+ setBothWays(bothWays);
57
+ setNewDeck({ name: created.name, removed });
58
+ setMessage(`Your deck "${created.name}" is ready. Now its first card:`);
59
+ setStep("front");
60
+ } catch (err) {
61
+ setError(err.message);
62
+ setStep("deck");
63
+ }
64
+ }
65
+
66
+ async function save(example) {
67
+ setStep("saving");
68
+ try {
69
+ const { duplicate } = await actions.addCardTo(deck, { ...draft, example });
70
+ if (duplicate) setError(`"${duplicate.front}" is already in ${deck}.`);
71
+ else {
72
+ setAdded((n) => n + 1);
73
+ setMessage(`Added "${draft.front}" to ${deck}. Next card, or Esc to finish.`);
74
+ }
75
+ } catch (err) {
76
+ setError(err.message);
77
+ }
78
+ setDraft({ front: "", back: [] });
79
+ setStep("front");
80
+ }
81
+
82
+ function submit(text) {
83
+ const value = text.trim();
84
+ setError("");
85
+ setMessage("");
86
+ if (step === "deck") {
87
+ if (!value) return setError("Your deck needs a name.");
88
+ if (Array.from(value).length > MAX_DECK_NAME) return setError(`Keep the name to ${MAX_DECK_NAME} characters or fewer.`);
89
+ return actions.existingDeck(value).then((existing) => {
90
+ if (existing) return setError(`There is already a deck called "${existing.name}".`);
91
+ setDeck(value);
92
+ setStep("ways");
93
+ }, (err) => setError(err.message));
94
+ }
95
+ if (step === "front") {
96
+ if (!value) return setError("A card needs a front.");
97
+ if (bothWays && value.startsWith("/")) return setError(SLASH_FRONT);
98
+ return actions.existingCard(deck, value).then((existing) => {
99
+ if (existing) return setError(`"${existing.front}" is already in ${deck}.`);
100
+ setDraft({ front: value, back: [] });
101
+ setStep("back");
102
+ }, (err) => setError(err.message));
103
+ }
104
+ if (step === "back") {
105
+ const answers = splitAnswers(value);
106
+ if (!answers.length) return setError("A card needs an answer.");
107
+ if (answers.some((a) => a.startsWith("/"))) return setError(SLASH_ANSWER);
108
+ setDraft((d) => ({ ...d, back: answers }));
109
+ return setStep("example");
110
+ }
111
+ if (step === "example") save(value);
112
+ }
113
+
114
+ function pickDeck(index) {
115
+ if (index === decks.length) return setStep("deck");
116
+ setDeck(decks[index].name);
117
+ setBothWays(decks[index].bothWays);
118
+ setStep("front");
119
+ }
120
+
121
+ const title = step === "pick" ? "Add cards to…" : step === "deck" || step === "ways" ? (decks.length ? "New deck" : "Start your first deck") : `Add a card · ${deck}`;
122
+ const field = FIELDS[step];
123
+ return html`
124
+ <${Fragment}>
125
+ <${Box} flexDirection="column" borderStyle="round" borderColor=${theme.accent} paddingX=${2} width=${panel}>
126
+ <${Text} bold color=${theme.accent}>${title}<//>
127
+ ${message && html`<${Box} marginTop=${1}><${Text} color=${theme.good}>${message}<//><//>`}
128
+ ${draft.front && step !== "front" && html`<${Box} marginTop=${1}><${Text} dimColor>${draft.front}${draft.back.length ? ` → ${draft.back.join(", ")}` : ""}<//><//>`}
129
+ ${field &&
130
+ html`
131
+ <${Box} flexDirection="column" marginTop=${1}>
132
+ <${Text} bold>${field.label}<//>
133
+ <${Text} dimColor>${field.hint}<//>
134
+ <${Box} marginTop=${1}><${AnswerInput} key=${step} plain onSubmit=${submit} onExit=${finish} onEdit=${() => setError("")} /><//>
135
+ <//>
136
+ `}
137
+ ${step === "pick" &&
138
+ html`
139
+ <${Box} marginTop=${1}>
140
+ <${Choice}
141
+ options=${[...decks.map((d) => ({ label: d.name, note: `${d.cards} card${d.cards === 1 ? "" : "s"}` })), { label: NEW_DECK }]}
142
+ selected=${picked}
143
+ onMove=${setPicked}
144
+ onPick=${pickDeck}
145
+ onCancel=${finish}
146
+ />
147
+ <//>
148
+ `}
149
+ ${step === "ways" &&
150
+ html`
151
+ <${Box} flexDirection="column" marginTop=${1}>
152
+ <${Text} bold>How should "${deck}" be asked?<//>
153
+ <${Box} marginTop=${1}>
154
+ <${Choice}
155
+ options=${[
156
+ { label: "Both ways", note: "for words: kot → cat, and cat → kot", key: "y" },
157
+ { label: "One way only", note: "for questions: front → back", key: "n" }
158
+ ]}
159
+ selected=${ways}
160
+ onMove=${setWays}
161
+ onPick=${(i) => startDeck(i === 0)}
162
+ onCancel=${finish}
163
+ />
164
+ <//>
165
+ <//>
166
+ `}
167
+ ${error && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>${error}<//><//>`}
168
+ <//>
169
+ <${Box} paddingX=${1}><${KeyHints} text=${step === "pick" || step === "ways" ? `${CHOICE_KEYS} · esc cancel` : "enter next · esc " + (added || newDeck ? "done" : "cancel")} /><//>
170
+ <//>
171
+ `;
172
+ }
@@ -18,17 +18,19 @@ function Palette({ matches, selected }) {
18
18
  `;
19
19
  }
20
20
 
21
- // everything after the command name: "/snooze 30m" → "30m"
21
+ // everything after the command name: "/pause 30m" → "30m"
22
22
  const args = (value) => value.trim().split(/\s+/).slice(1).join(" ");
23
23
 
24
24
  // Answer line with a movable cursor (←/→, Ctrl+A/E). Typing "/" opens a command palette:
25
25
  // ↑/↓ choose, Tab completes, Enter runs.
26
26
  // `commandOnly` is the between-questions variant: it starts at "/" and Esc or erasing the "/" closes it.
27
- export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, commands = COMMANDS, initial = "", commandOnly = false }) {
27
+ // `plain` is a text field with no commands at all ("and/or" is just text there).
28
+ export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, commands = COMMANDS, initial = "", commandOnly = false, plain = false }) {
28
29
  const [value, setValue] = useState(initial);
29
30
  const [cursor, setCursor] = useState(initial.length);
30
31
  const [selected, setSelected] = useState(0);
31
- const matches = value.startsWith("/") ? matchCommands(value, commands) : [];
32
+ const isCommand = !plain && value.startsWith("/");
33
+ const matches = isCommand ? matchCommands(value, commands) : [];
32
34
  const pick = Math.min(selected, Math.max(matches.length - 1, 0));
33
35
 
34
36
  const edit = (next, at = next.length) => {
@@ -41,7 +43,8 @@ export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, com
41
43
  useInput((input, key) => {
42
44
  if (key.escape) return commandOnly ? onCancel() : value ? edit("") : onExit();
43
45
  if (key.return) {
44
- if (!value.startsWith("/")) return onSubmit(value);
46
+ // no command starts like this: on a card it's simply the answer (some answers begin with "/")
47
+ if (!isCommand || (!matches.length && !commandOnly)) return onSubmit(value);
45
48
  const [token, ...rest] = value.trim().split(/\s+/);
46
49
  edit("");
47
50
  return onCommand(matches[pick]?.name ?? token, rest.join(" "));
@@ -76,7 +79,7 @@ export function AnswerInput({ onSubmit, onCommand, onExit, onEdit, onCancel, com
76
79
  <${Text}>${before}<${Text} inverse>${at}<//>${after}<//>
77
80
  <//>
78
81
  ${matches.length > 0 && html`<${Palette} matches=${matches} selected=${pick} />`}
79
- ${value.startsWith("/") && matches.length === 0 && html`<${Box} marginTop=${1}><${Text} color=${theme.bad}>unknown command<//><//>`}
82
+ ${isCommand && matches.length === 0 && html`<${Box} marginTop=${1}>${commandOnly ? html`<${Text} color=${theme.bad}>unknown command<//>` : html`<${Text} dimColor>no command like that · enter sends it as your answer<//>`}<//>`}
80
83
  <//>
81
84
  `;
82
85
  }