"use client"; /** * The answer card for a mastery question. * * Its sibling `AskUserOptions` exists to unblock a decision: it shows a label * and, under it, whatever else the model wanted to say about that choice. A * mastery question does not fit that — its options *are* bodies of text, and * they are numbered — so the generic card printed the letter twice, once as * the badge and once as the label, above the answer it was labelling. * * The look follows the rule the mastery surface already set for itself (see * `mastery-theme.css`): state is carried by *form*, not by a skin. So there is * no boxed panel, no filled rows and no coloured verdict banner — a question * is a rule down the left margin, a prompt, and lines of text you can pick. * The rule is what changes: neutral while it is the learner's move, primary or * destructive once the gate has ruled. It should read like a question on a * page, not like a form to fill in. */ import { memo, useCallback, useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { LearningText } from "@/components/common/LearningText"; import InlineMarkdown from "@/components/common/InlineMarkdown"; import { useCardSubmission } from "@/hooks/use-card-submission"; import { REPLY_NOT_DELIVERED } from "@/lib/ask-user-state"; import { MASTERY_ANSWER_REJECTED_MESSAGE, type MasteryGradeResult, type MasteryQuestion, } from "@/lib/mastery-question"; /** Difficulty is a three-point scale; anything else is not shown. */ const DIFFICULTY_LABELS: Record = { easy: "Easy", medium: "Medium", hard: "Hard", }; type OptionState = "idle" | "picked" | "correct" | "wrong"; function OptionRow({ option, index, state, disabled, onPick, }: { option: { label: string; body: string }; index: number; state: OptionState; disabled: boolean; onPick: () => void; }) { const letter = option.label || String.fromCharCode(65 + index); // The marker carries the state on its own: outline → filled, in whatever // colour the verdict gives it. The row never becomes a coloured block. const marker = state === "correct" || state === "picked" ? "border-[var(--primary)] bg-[var(--primary)] text-[var(--primary-foreground)]" : state === "wrong" ? "border-[var(--destructive)] bg-[var(--destructive)] text-[var(--destructive-foreground)]" : "border-[var(--border)] text-[var(--muted-foreground)] group-hover:border-[color-mix(in_srgb,var(--foreground)_35%,transparent)] group-hover:text-[var(--foreground)]"; const body = state === "wrong" ? "text-[var(--muted-foreground)]" : "text-[var(--foreground)]"; return ( ); } interface MasteryQuestionCardProps { question: MasteryQuestion; /** The gate's ruling, once it has been made. */ grade: MasteryGradeResult | null; /** The learner has already answered this card (this turn or an earlier one). */ answered: boolean; /** The learner dropped this question; the engine closed it ungraded. */ skipped?: boolean; /** What they answered, when the transcript knows it. */ submittedAnswer: string; /** Unique server rejection event; changes reopen an optimistically sent card. */ rejectionKey?: string; onSubmit: (payload: { text?: string; answers?: Array<{ questionId: string; text: string }>; }) => void | boolean | Promise; /** Drop this question and let the tutor move on. Absent where the surface * cannot start a turn of its own, which hides the control entirely. */ onSkip?: (questionId: string) => void | boolean | Promise; onChallenge?: (questionId: string) => void | boolean | Promise; } /** The same native controls, with submission owned by their group instead. */ interface GroupInput { locked: boolean; onChange: (questionId: string, answer: string, sourceAvailable: boolean) => void; } const SingleMasteryQuestionCard = memo(function SingleMasteryQuestionCard({ question, grade, answered, skipped, submittedAnswer, onSubmit, onSkip, onChallenge, groupInput, rejectionKey, }: MasteryQuestionCardProps & { groupInput?: GroupInput }) { const { t } = useTranslation(); const [picked, setPicked] = useState(() => grade?.final === false ? grade.learnerAnswer : ""); const [skipping, setSkipping] = useState(false); const [sourceUnavailable, setSourceUnavailable] = useState(false); const [freeText, setFreeText] = useState(() => grade?.final === false ? grade.learnerAnswer : ""); const [freeSelected, setFreeSelected] = useState(false); const acknowledgementKey = grade ? grade.receiptKey ?? JSON.stringify(grade) : undefined; const { sending, failed: sendFailed, submit } = useCardSubmission(onSubmit, rejectionKey, acknowledgementKey); const textareaRef = useRef(null); useEffect(() => { if (freeSelected) textareaRef.current?.focus(); }, [freeSelected]); const hasChoices = question.options.length > 0; const usesFreeText = freeSelected || !hasChoices; const answer = usesFreeText ? freeText.trim() : picked; // Skipping settles the card exactly as answering does: the engine closed the // question, so there is nothing left on it to send. const settled = (answered && grade?.final !== false) || skipped === true; const ungraded = grade?.result === "ungraded"; const locked = settled || sending || skipping || groupInput?.locked === true; const onGroupChange = groupInput?.onChange; useEffect(() => { onGroupChange?.(question.questionId, answer, !sourceUnavailable); }, [onGroupChange, question.questionId, answer, sourceUnavailable]); const handleSkip = useCallback(() => { if (locked || !onSkip) return; setSkipping(true); void Promise.resolve(onSkip(question.questionId)).then((accepted) => { // Same rule as an answer: only an explicit refusal reopens the card. if (accepted === false) setSkipping(false); }); }, [locked, onSkip, question.questionId]); const handleSubmit = useCallback(() => { if (locked || !answer || sourceUnavailable) return; void submit({ text: answer, answers: [{ questionId: question.questionId, text: answer }], }); }, [answer, locked, sourceUnavailable, submit, question.questionId]); // Which answer to treat as the learner's, preferring what the graded record // committed over what this browser happens to remember. const learnerAnswer = grade?.learnerAnswer || submittedAnswer || answer; const difficultyLabel = DIFFICULTY_LABELS[question.difficulty] ?? ""; const meta = [ question.objectiveName, difficultyLabel ? t(difficultyLabel) : "", question.attempt > 1 ? t("Attempt {{count}}", { count: question.attempt }) : "", ].filter(Boolean); // The rule down the left margin is the whole status indicator. const rule = ungraded ? "border-dashed border-[var(--border)]" : grade ? grade.isCorrect ? "border-[var(--primary)]" : "border-[var(--destructive)]" : skipped ? // Closed, but nothing was judged — a dashed rule says "set aside" // where a solid one would claim a verdict that was never made. "border-dashed border-[var(--border)]" : "border-[var(--border)]"; const optionState = (option: { label: string }): OptionState => { if (grade) { if (ungraded) return "idle"; if (option.label && option.label === grade.correctLabel) return "correct"; if (option.label && option.label === learnerAnswer && !grade.isCorrect) return "wrong"; return "idle"; } if (settled) return option.label === learnerAnswer ? "picked" : "idle"; return !freeSelected && option.label === picked ? "picked" : "idle"; }; return (
{meta.length > 0 ? (
{meta.join(" · ")}
) : null}
{question.visual &&

{grade?.independent === false || question.visual.hintsUsed || question.visual.answerCues !== "none" ? t("Guided visual practice — this does not demonstrate independent mastery.") : t("Visual practice with the original source.")}

{sourceUnavailable &&

{t("The source image is unavailable. Open the source or ask your tutor to inspect its current version.")}

} {question.visual.keyStatus !== "verified" &&

{t("The reference key is uncertain. This practice will remain ungraded.")}

} {question.visual.pixelsInspected === false &&

{t("Current model input has no verified source pixels. Retrieve the image again with a vision-capable model before grading.")}

} {question.visual.sources.map((source, index) =>
{/* Authenticated source endpoints need the browser's own cookies. */} {t("Original setSourceUnavailable(true)} className="max-h-80 max-w-full object-contain" />
{source.sourcePath}{source.page ? ` · ${t("Page")} ${source.page}` : ""}
)}
} {hasChoices ? (
{question.options.map((option, idx) => ( { setFreeSelected(false); setPicked(option.label); }} /> ))}
) : null} {question.allowFreeText && !settled ? (
{freeSelected || !hasChoices ? (