'use client' import React, { useState, useEffect, useCallback, useMemo } from 'react' import { useParams, useRouter } from 'next/navigation' import { useTheme } from '@/lib/ThemeContext' import { StarRating, accuracyToStars } from '@/components/gamification/StarRating' interface QAItem { id: string; question: string; answer: string } function getApiBase() { return '' } export default function MatchPage() { const { unitId } = useParams<{ unitId: string }>() const router = useRouter() const { isDark } = useTheme() const [allItems, setAllItems] = useState([]) const [isLoading, setIsLoading] = useState(true) const [round, setRound] = useState(0) const [selectedLeft, setSelectedLeft] = useState(null) const [matched, setMatched] = useState>(new Set()) const [wrongPair, setWrongPair] = useState(null) const [errors, setErrors] = useState(0) const [isComplete, setIsComplete] = useState(false) const glassCard = isDark ? 'bg-white/10 backdrop-blur-xl border border-white/10' : 'bg-white/80 backdrop-blur-xl border border-black/5' useEffect(() => { (async () => { try { const resp = await fetch(`${getApiBase()}/api/learning-units/${unitId}/qa`) if (resp.ok) { const d = await resp.json(); setAllItems(d.qa_items || []) } } catch {} finally { setIsLoading(false) } })() }, [unitId]) // Take 6 items per round const roundItems = useMemo(() => { const start = round * 6 return allItems.slice(start, start + 6) }, [allItems, round]) // Shuffled right column const shuffledRight = useMemo(() => { return [...roundItems].sort(() => Math.random() - 0.5) }, [roundItems]) const handleLeftTap = useCallback((id: string) => { if (matched.has(id)) return setSelectedLeft(id === selectedLeft ? null : id) setWrongPair(null) }, [selectedLeft, matched]) const handleRightTap = useCallback((id: string) => { if (!selectedLeft || matched.has(id)) return if (selectedLeft === id) { // Correct match setMatched(prev => new Set([...prev, id])) setSelectedLeft(null) // Check if round complete if (matched.size + 1 >= roundItems.length) { const nextStart = (round + 1) * 6 if (nextStart >= allItems.length) { setTimeout(() => setIsComplete(true), 500) } else { setTimeout(() => { setRound(r => r + 1) setMatched(new Set()) setSelectedLeft(null) }, 800) } } } else { // Wrong match setWrongPair(id) setErrors(e => e + 1) setTimeout(() => { setWrongPair(null) setSelectedLeft(null) }, 600) } }, [selectedLeft, matched, roundItems, round, allItems]) if (isLoading) { return
} const totalPairs = allItems.length const matchedTotal = round * 6 + matched.size return (
{/* Header */}

Zuordnen

{matchedTotal}/{totalPairs}
{isComplete ? (

Alle zugeordnet!

{errors} Fehler

) : (
{/* Left column: English */}

English

{roundItems.map(item => ( ))}
{/* Right column: German (shuffled) */}

Deutsch

{shuffledRight.map(item => ( ))}
)}
) }