Study cards: from-your-notes, read aloud, timed runs
From your notes: the generator now stores a verbatim source quote with every activity (schema + prompt + a server-side clamp so a chatty model cannot drop a card), and the feedback panel shows the sentence that carries it with the quote highlighted green. Decks made before the change, and quotes the model trimmed, fall back to a local IDF-weighted match over the saved notes text (lib/notes-source.ts): nothing is shown unless at least two distinct terms land. The passage hides in exam mode with the other reveals. Read aloud: a Web Speech API speaker control on the text a learner reads (flashcard faces, questions, statements, sentences, instructions) via lib/speech.ts and components/SpeakButton.tsx — one utterance app-wide, a voice matched to the deck's language, and no button at all where the browser has no speech synthesis. It used to sit inside the flashcard's flip button (nested buttons, tapping it flipped the card); it now lives next to the hint row. Timed runs: the feed chrome carries a timer chip (off by default) that gives each card 30 seconds; a card that runs out is recorded as a miss through the same path as a wrong answer (history + SRS agree), shows a toast, and the feed moves straight on with a fresh budget. handleAnswered ignores a second result per card, so a timeout plus a manual answer cannot record twice. The generator's type spec now names what each type suits (language decks lean on flashcards/matching/fill blank, ordering needs a real sequence, short answer reasoning), so the deck-aware guidance covers all seven types. New activities-test suite (24 checks: quote path, local path, the green mark, the speech stub, the timed run on Playwright's fake clock), registered in run-all; phase2 pins that the notes passage stays hidden in exam mode; en+cs keys for every new string; no new dependencies. Version 0.6.1, SW v83.
This commit is contained in:
parent
786d6cf1ae
commit
796e2e9fe9
26 changed files with 1068 additions and 51 deletions
|
|
@ -6,6 +6,7 @@ import { useI18n } from "@/lib/i18n";
|
|||
import type { Activity } from "@/lib/schemas";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui";
|
||||
import { SpeakButton } from "@/components/SpeakButton";
|
||||
|
||||
import {
|
||||
ActivityHeader,
|
||||
|
|
@ -67,7 +68,7 @@ export function isFillBlankCorrect(userValue: string, answer: string): boolean {
|
|||
});
|
||||
}
|
||||
|
||||
export function FillBlank({ activity, active, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<FillBlankActivity>) {
|
||||
export function FillBlank({ activity, active, language, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<FillBlankActivity>) {
|
||||
const { t } = useI18n();
|
||||
const [value, setValue] = useState("");
|
||||
const [chosen, setChosen] = useState<string | null>(null);
|
||||
|
|
@ -141,6 +142,9 @@ export function FillBlank({ activity, active, onAnswered, onNext, isLast, nextLa
|
|||
)}
|
||||
{after}
|
||||
</p>
|
||||
<div className="mt-3 flex justify-end">
|
||||
<SpeakButton text={activity.sentence} language={language} />
|
||||
</div>
|
||||
</PromptCard>
|
||||
|
||||
{activity.options ? (
|
||||
|
|
@ -181,6 +185,7 @@ export function FillBlank({ activity, active, onAnswered, onNext, isLast, nextLa
|
|||
|
||||
{result !== null ? (
|
||||
<FeedbackPanel
|
||||
activity={activity}
|
||||
correct={result}
|
||||
explanation={activity.explanation}
|
||||
xp={activity.xp}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { useState } from "react";
|
|||
import { useI18n } from "@/lib/i18n";
|
||||
import type { Activity } from "@/lib/schemas";
|
||||
import { Button } from "@/components/ui";
|
||||
import { SpeakButton } from "@/components/SpeakButton";
|
||||
|
||||
import {
|
||||
ActivityHeader,
|
||||
|
|
@ -19,7 +20,7 @@ import {
|
|||
|
||||
type FlashcardActivity = Extract<Activity, { type: "flashcard" }>;
|
||||
|
||||
export function Flashcard({ activity, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<FlashcardActivity>) {
|
||||
export function Flashcard({ activity, language, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<FlashcardActivity>) {
|
||||
const { t } = useI18n();
|
||||
const reduceMotion = useReducedMotion();
|
||||
const [flipped, setFlipped] = useState(false);
|
||||
|
|
@ -46,7 +47,10 @@ export function Flashcard({ activity, onAnswered, onNext, isLast, nextLabel }: A
|
|||
return (
|
||||
<>
|
||||
<ActivityHeader type="flashcard" topic={activity.topic} />
|
||||
<Hint>{flipped ? t("flashcard.didYouKnow") : t("flashcard.tapToFlip")}</Hint>
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<Hint>{flipped ? t("flashcard.didYouKnow") : t("flashcard.tapToFlip")}</Hint>
|
||||
<SpeakButton text={flipped ? activity.back : activity.front} language={language} />
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
|
|
@ -100,6 +104,7 @@ export function Flashcard({ activity, onAnswered, onNext, isLast, nextLabel }: A
|
|||
|
||||
{result !== null ? (
|
||||
<FeedbackPanel
|
||||
activity={activity}
|
||||
correct={result}
|
||||
explanation={activity.explanation}
|
||||
xp={activity.xp}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { useMemo, useState } from "react";
|
|||
import { useI18n } from "@/lib/i18n";
|
||||
import type { Activity } from "@/lib/schemas";
|
||||
import { cn, shuffle } from "@/lib/utils";
|
||||
import { SpeakButton } from "@/components/SpeakButton";
|
||||
|
||||
import {
|
||||
ActivityHeader,
|
||||
|
|
@ -16,7 +17,7 @@ import {
|
|||
|
||||
type MatchingActivity = Extract<Activity, { type: "matching" }>;
|
||||
|
||||
export function Matching({ activity, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<MatchingActivity>) {
|
||||
export function Matching({ activity, language, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<MatchingActivity>) {
|
||||
const { t, plural } = useI18n();
|
||||
const terms = useMemo(
|
||||
() => shuffle(activity.pairs.map((pair, index) => ({ index, text: pair.term }))),
|
||||
|
|
@ -67,7 +68,10 @@ export function Matching({ activity, onAnswered, onNext, isLast, nextLabel }: Ac
|
|||
}
|
||||
/>
|
||||
<PromptCard className="py-4">
|
||||
<p className="text-lg font-bold text-frost-950 dark:text-frost-50">{activity.instruction}</p>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<p className="text-lg font-bold text-frost-950 dark:text-frost-50">{activity.instruction}</p>
|
||||
<SpeakButton text={activity.instruction} language={language} />
|
||||
</div>
|
||||
<Hint>
|
||||
{done
|
||||
? t("matching.allMatched")
|
||||
|
|
@ -133,6 +137,7 @@ export function Matching({ activity, onAnswered, onNext, isLast, nextLabel }: Ac
|
|||
|
||||
{complete ? (
|
||||
<FeedbackPanel
|
||||
activity={activity}
|
||||
correct={misses === 0}
|
||||
explanation={activity.explanation}
|
||||
xp={activity.xp}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { useEffect, useState } from "react";
|
|||
|
||||
import { useI18n } from "@/lib/i18n";
|
||||
import type { Activity } from "@/lib/schemas";
|
||||
import { SpeakButton } from "@/components/SpeakButton";
|
||||
|
||||
import {
|
||||
ActivityHeader,
|
||||
|
|
@ -20,6 +21,7 @@ type MultipleChoiceActivity = Extract<Activity, { type: "multiple_choice" }>;
|
|||
export function MultipleChoice({
|
||||
activity,
|
||||
active,
|
||||
language,
|
||||
onAnswered,
|
||||
onNext,
|
||||
isLast,
|
||||
|
|
@ -62,7 +64,10 @@ export function MultipleChoice({
|
|||
<>
|
||||
<ActivityHeader type="multiple_choice" topic={activity.topic} />
|
||||
<PromptCard>
|
||||
<QuestionText>{activity.question}</QuestionText>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<QuestionText>{activity.question}</QuestionText>
|
||||
<SpeakButton text={activity.question} language={language} />
|
||||
</div>
|
||||
</PromptCard>
|
||||
|
||||
<div className="flex flex-col gap-2.5" role="group" aria-label={t("activity.answerOptions")}>
|
||||
|
|
@ -83,6 +88,7 @@ export function MultipleChoice({
|
|||
|
||||
{answered ? (
|
||||
<FeedbackPanel
|
||||
activity={activity}
|
||||
correct={selected === activity.correctIndex}
|
||||
explanation={activity.explanation}
|
||||
xp={activity.xp}
|
||||
|
|
|
|||
55
components/activities/NotesSource.tsx
Normal file
55
components/activities/NotesSource.tsx
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
"use client";
|
||||
|
||||
import { useMemo } from "react";
|
||||
|
||||
import { NotesIcon } from "@/components/icons";
|
||||
import { useI18n } from "@/lib/i18n";
|
||||
import { highlightSegments, notesSource } from "@/lib/notes-source";
|
||||
import type { Activity } from "@/lib/schemas";
|
||||
|
||||
import { useNoHints, useNotesText } from "./context";
|
||||
|
||||
/**
|
||||
* "From your notes": the passage an activity's answer comes from, with the
|
||||
* matching words highlighted green.
|
||||
*
|
||||
* The quote is the one the generator stored with the activity (verbatim from
|
||||
* the notes); decks made before that, or quotes the model trimmed, fall back
|
||||
* to a local, IDF-weighted sentence match over the same notes text. It sits
|
||||
* inside the feedback panel, so it only appears once the learner has answered,
|
||||
* and it disappears in exam mode with the other reveals.
|
||||
*/
|
||||
export function NotesSource({ activity }: { activity: Activity }) {
|
||||
const { t } = useI18n();
|
||||
const notes = useNotesText();
|
||||
const noHints = useNoHints();
|
||||
const match = useMemo(() => (noHints ? null : notesSource(notes, activity)), [noHints, notes, activity]);
|
||||
if (!match) return null;
|
||||
const pieces = highlightSegments(match.text, match.spans);
|
||||
return (
|
||||
<div className="mt-3 rounded-[var(--radius-control)] border border-[var(--glass-border)] bg-[var(--surface-strong)] p-3">
|
||||
<p className="mb-1 flex items-center gap-1.5 text-[10.5px] font-bold uppercase tracking-wider text-[var(--text-muted)]">
|
||||
<NotesIcon className="size-3.5" />
|
||||
{t("activity.source")}
|
||||
</p>
|
||||
<p
|
||||
className="text-[13px] leading-relaxed text-[var(--text)]"
|
||||
data-notes-source
|
||||
data-notes-quote={match.quote ? "1" : "0"}
|
||||
>
|
||||
{pieces.map((piece, index) =>
|
||||
piece.hit ? (
|
||||
<mark
|
||||
key={index}
|
||||
className="rounded bg-emerald-200/85 px-0.5 text-emerald-950 dark:bg-emerald-400/35 dark:text-emerald-50"
|
||||
>
|
||||
{piece.text}
|
||||
</mark>
|
||||
) : (
|
||||
<span key={index}>{piece.text}</span>
|
||||
),
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import { useMemo, useState } from "react";
|
|||
import { useI18n } from "@/lib/i18n";
|
||||
import type { Activity } from "@/lib/schemas";
|
||||
import { cn, shuffle } from "@/lib/utils";
|
||||
import { SpeakButton } from "@/components/SpeakButton";
|
||||
import { Button } from "@/components/ui";
|
||||
|
||||
import {
|
||||
|
|
@ -18,7 +19,7 @@ import {
|
|||
|
||||
type OrderingActivity = Extract<Activity, { type: "ordering" }>;
|
||||
|
||||
export function Ordering({ activity, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<OrderingActivity>) {
|
||||
export function Ordering({ activity, language, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<OrderingActivity>) {
|
||||
const { t } = useI18n();
|
||||
const initial = useMemo(() => {
|
||||
const out = shuffle(activity.items);
|
||||
|
|
@ -54,7 +55,10 @@ export function Ordering({ activity, onAnswered, onNext, isLast, nextLabel }: Ac
|
|||
<>
|
||||
<ActivityHeader type="ordering" topic={activity.topic} />
|
||||
<PromptCard className="py-4">
|
||||
<p className="text-lg font-bold text-frost-950 dark:text-frost-50">{activity.instruction}</p>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<p className="text-lg font-bold text-frost-950 dark:text-frost-50">{activity.instruction}</p>
|
||||
<SpeakButton text={activity.instruction} language={language} />
|
||||
</div>
|
||||
<Hint>
|
||||
{checked ? t("ordering.correctOrder") : t("ordering.hint")}
|
||||
</Hint>
|
||||
|
|
@ -128,6 +132,7 @@ export function Ordering({ activity, onAnswered, onNext, isLast, nextLabel }: Ac
|
|||
</Button>
|
||||
) : (
|
||||
<FeedbackPanel
|
||||
activity={activity}
|
||||
correct={correct}
|
||||
explanation={activity.explanation}
|
||||
xp={activity.xp}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import type { Activity } from "@/lib/schemas";
|
|||
import { cn } from "@/lib/utils";
|
||||
import { Button, Spinner } from "@/components/ui";
|
||||
import { ModelErrorActions } from "@/components/ModelErrorActions";
|
||||
import { SpeakButton } from "@/components/SpeakButton";
|
||||
|
||||
import {
|
||||
ActivityHeader,
|
||||
|
|
@ -82,7 +83,10 @@ export function ShortAnswer({ activity, active, language, onAnswered, onNext, is
|
|||
<>
|
||||
<ActivityHeader type="short_answer" topic={activity.topic} />
|
||||
<PromptCard>
|
||||
<QuestionText>{activity.question}</QuestionText>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<QuestionText>{activity.question}</QuestionText>
|
||||
<SpeakButton text={activity.question} language={language} />
|
||||
</div>
|
||||
</PromptCard>
|
||||
|
||||
<label className="sr-only" htmlFor={`answer-${activity.id}`}>
|
||||
|
|
@ -186,6 +190,7 @@ export function ShortAnswer({ activity, active, language, onAnswered, onNext, is
|
|||
</div>
|
||||
|
||||
<FeedbackPanel
|
||||
activity={activity}
|
||||
correct={state.correct}
|
||||
explanation={activity.explanation}
|
||||
xp={activity.xp}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { useState } from "react";
|
|||
import { useI18n } from "@/lib/i18n";
|
||||
import type { Activity } from "@/lib/schemas";
|
||||
import { Button } from "@/components/ui";
|
||||
import { SpeakButton } from "@/components/SpeakButton";
|
||||
|
||||
import {
|
||||
ActivityHeader,
|
||||
|
|
@ -17,7 +18,7 @@ import {
|
|||
|
||||
type TrueFalseActivity = Extract<Activity, { type: "true_false" }>;
|
||||
|
||||
export function TrueFalse({ activity, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<TrueFalseActivity>) {
|
||||
export function TrueFalse({ activity, language, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<TrueFalseActivity>) {
|
||||
const { t } = useI18n();
|
||||
const [result, setResult] = useState<boolean | null>(null);
|
||||
const x = useMotionValue(0);
|
||||
|
|
@ -65,6 +66,9 @@ export function TrueFalse({ activity, onAnswered, onNext, isLast, nextLabel }: A
|
|||
<p className="pt-6 text-center text-xl font-bold leading-snug text-frost-950 sm:text-2xl dark:text-frost-50">
|
||||
“{activity.statement}”
|
||||
</p>
|
||||
<div className="mt-3 flex justify-end">
|
||||
<SpeakButton text={activity.statement} language={language} />
|
||||
</div>
|
||||
</PromptCard>
|
||||
</motion.div>
|
||||
</div>
|
||||
|
|
@ -80,6 +84,7 @@ export function TrueFalse({ activity, onAnswered, onNext, isLast, nextLabel }: A
|
|||
</div>
|
||||
) : (
|
||||
<FeedbackPanel
|
||||
activity={activity}
|
||||
correct={result}
|
||||
explanation={activity.explanation}
|
||||
xp={activity.xp}
|
||||
|
|
|
|||
36
components/activities/context.tsx
Normal file
36
components/activities/context.tsx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
"use client";
|
||||
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
/**
|
||||
* The two ambient facts an activity card needs from the screen around it:
|
||||
*
|
||||
* - the deck's own notes text, so the feedback panel can show the passage an
|
||||
* answer came from (NotesSource). Absent on screens with no notes (the
|
||||
* homework guide's checkpoints);
|
||||
* - whether reveals are off (exam mode), which hides the hints *and* that
|
||||
* passage — both give the answer away.
|
||||
*
|
||||
* Its own module (not shared.tsx) because NotesSource needs these hooks while
|
||||
* shared.tsx renders NotesSource; a cycle there would be fragile.
|
||||
*/
|
||||
|
||||
const NotesTextContext = createContext<string | undefined>(undefined);
|
||||
|
||||
export function NotesTextProvider({ value, children }: { value?: string; children: React.ReactNode }) {
|
||||
return <NotesTextContext.Provider value={value}>{children}</NotesTextContext.Provider>;
|
||||
}
|
||||
|
||||
export function useNotesText(): string | undefined {
|
||||
return useContext(NotesTextContext);
|
||||
}
|
||||
|
||||
const NoHintsContext = createContext(false);
|
||||
|
||||
export function NoHintsProvider({ value, children }: { value: boolean; children: React.ReactNode }) {
|
||||
return <NoHintsContext.Provider value={value}>{children}</NoHintsContext.Provider>;
|
||||
}
|
||||
|
||||
export function useNoHints(): boolean {
|
||||
return useContext(NoHintsContext);
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ import { NoHintsProvider, type ActivityComponentProps } from "./shared";
|
|||
|
||||
export type { ActivityComponentProps } from "./shared";
|
||||
export { TYPE_META } from "./shared";
|
||||
export { NotesTextProvider, useNotesText } from "./context";
|
||||
|
||||
/**
|
||||
* Renders an activity from JSON based on its `type` field.
|
||||
|
|
|
|||
|
|
@ -2,13 +2,17 @@
|
|||
|
||||
import { motion } from "framer-motion";
|
||||
import { useAppReducedMotion as useReducedMotion } from "@/lib/motion-pref";
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
import { useI18n } from "@/lib/i18n";
|
||||
import type { Activity } from "@/lib/schemas";
|
||||
import { useIsPhone } from "@/lib/use-phone";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui";
|
||||
import { useNoHints } from "./context";
|
||||
import { NotesSource } from "./NotesSource";
|
||||
|
||||
export { NoHintsProvider, useNoHints, NotesTextProvider, useNotesText } from "./context";
|
||||
export { NotesSource } from "./NotesSource";
|
||||
|
||||
export type ActivityComponentProps<A extends Activity = Activity> = {
|
||||
activity: A;
|
||||
|
|
@ -150,6 +154,7 @@ export function FeedbackPanel({
|
|||
nextLabel,
|
||||
headline,
|
||||
detail,
|
||||
activity,
|
||||
children,
|
||||
}: {
|
||||
correct: boolean;
|
||||
|
|
@ -161,6 +166,8 @@ export function FeedbackPanel({
|
|||
nextLabel?: string;
|
||||
headline?: string;
|
||||
detail?: React.ReactNode;
|
||||
/** The card being answered, so the panel can point back into the notes. */
|
||||
activity?: Activity;
|
||||
children?: React.ReactNode;
|
||||
}) {
|
||||
const { t } = useI18n();
|
||||
|
|
@ -210,6 +217,8 @@ export function FeedbackPanel({
|
|||
{explanation}
|
||||
</p>
|
||||
|
||||
{activity ? <NotesSource activity={activity} /> : null}
|
||||
|
||||
{children}
|
||||
|
||||
<div className="mt-4 flex items-center justify-end">
|
||||
|
|
@ -230,17 +239,5 @@ export function Hint({ children }: { children: React.ReactNode }) {
|
|||
return <p className="hint-line text-sm text-frost-600 dark:text-frost-300">{children}</p>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exam mode sets this through ActivityRenderer's `noHints` prop so the
|
||||
* affordances that give the answer away disappear without touching every
|
||||
* activity component.
|
||||
*/
|
||||
const NoHintsContext = createContext(false);
|
||||
|
||||
export function NoHintsProvider({ value, children }: { value: boolean; children: React.ReactNode }) {
|
||||
return <NoHintsContext.Provider value={value}>{children}</NoHintsContext.Provider>;
|
||||
}
|
||||
|
||||
export function useNoHints(): boolean {
|
||||
return useContext(NoHintsContext);
|
||||
}
|
||||
/* The no-hints / notes-text contexts live in ./context: NotesSource reads them
|
||||
while this file renders NotesSource, so keeping them apart avoids a cycle. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue