"use client"; import { Reorder } from "framer-motion"; 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 { ActivityHeader, FeedbackPanel, Hint, PromptCard, type ActivityComponentProps, } from "./shared"; type OrderingActivity = Extract; export function Ordering({ activity, language, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps) { const { t } = useI18n(); const initial = useMemo(() => { const out = shuffle(activity.items); if (out.length > 1 && out.every((item, index) => item === activity.items[index])) { [out[0], out[1]] = [out[1], out[0]]; } return out; }, [activity]); const [items, setItems] = useState(initial); const [checked, setChecked] = useState(false); const [correct, setCorrect] = useState(false); const isRight = (list: string[]) => list.every((item, index) => item === activity.items[index]); const check = () => { if (checked) return; const result = isRight(items); setChecked(true); setCorrect(result); onAnswered(result, { userAnswer: items.join(" → ") }); }; const move = (index: number, direction: -1 | 1) => { if (checked) return; const target = index + direction; if (target < 0 || target >= items.length) return; const next = [...items]; [next[index], next[target]] = [next[target], next[index]]; setItems(next); }; return ( <>

{activity.instruction}

{checked ? t("ordering.correctOrder") : t("ordering.hint")}
{checked ? (
    {activity.items.map((item, index) => { const wasRight = items[index] === item; return (
  1. {index + 1} {item}
  2. ); })}
) : ( {items.map((item, index) => ( {index + 1} {item} ⠿ ))} )} {!checked ? ( ) : ( )} ); }