"use client"; import { useEffect, useState } from "react"; import { useI18n } from "@/lib/i18n"; import type { Activity } from "@/lib/schemas"; import { SpeakButton } from "@/components/SpeakButton"; import { ActivityHeader, FeedbackPanel, Hint, OptionTile, PromptCard, QuestionText, type ActivityComponentProps, } from "./shared"; type MultipleChoiceActivity = Extract; export function MultipleChoice({ activity, active, language, onAnswered, onNext, isLast, nextLabel, }: ActivityComponentProps) { const { t } = useI18n(); const [selected, setSelected] = useState(null); const answered = selected !== null; const choose = (index: number) => { if (answered) return; setSelected(index); onAnswered(index === activity.correctIndex); }; useEffect(() => { if (!active || answered) return; const onKey = (event: KeyboardEvent) => { const target = event.target as HTMLElement | null; if (target && /^(INPUT|TEXTAREA)$/.test(target.tagName)) return; const index = Number.parseInt(event.key, 10) - 1; if (index >= 0 && index < activity.options.length) { event.preventDefault(); choose(index); } }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); // eslint-disable-next-line react-hooks/exhaustive-deps }, [active, answered, activity.options.length]); const stateFor = (index: number): "idle" | "correct" | "wrong" | "dim" => { if (!answered) return "idle"; if (index === activity.correctIndex) return "correct"; if (index === selected) return "wrong"; return "dim"; }; return ( <>
{activity.question}
{activity.options.map((option, index) => ( choose(index)} shortcut={String(index + 1)} > {option} ))}
{!answered ? {t("mc.tipKeys")} : null} {answered ? ( {t("activity.correctAnswer")}{" "} {activity.options[activity.correctIndex]} ) } /> ) : null} ); }