'use client' import React, { useState, useEffect, useCallback } from 'react' import { useParams, useRouter } from 'next/navigation' import { useTheme } from '@/lib/ThemeContext' import { FlashCard } from '@/components/learn/FlashCard' import { AudioButton } from '@/components/learn/AudioButton' interface QAItem { id: string question: string answer: string leitner_box: number correct_count: number incorrect_count: number } function getBackendUrl() { if (typeof window === 'undefined') return 'http://localhost:8001' const { hostname, protocol } = window.location if (hostname === 'localhost') return 'http://localhost:8001' return `${protocol}//${hostname}:8001` } export default function FlashcardsPage() { const { unitId } = useParams<{ unitId: string }>() const router = useRouter() const { isDark } = useTheme() const [items, setItems] = useState([]) const [currentIndex, setCurrentIndex] = useState(0) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const [stats, setStats] = useState({ correct: 0, incorrect: 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(() => { loadQA() }, [unitId]) const loadQA = async () => { setIsLoading(true) try { const resp = await fetch(`${getBackendUrl()}/api/learning-units/${unitId}/qa`) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) const data = await resp.json() setItems(data.qa_items || []) } catch (err: any) { setError(err.message) } finally { setIsLoading(false) } } const handleAnswer = useCallback(async (correct: boolean) => { const item = items[currentIndex] if (!item) return // Update Leitner progress try { await fetch( `${getBackendUrl()}/api/learning-units/${unitId}/leitner/update?item_id=${item.id}&correct=${correct}`, { method: 'POST' } ) } catch (err) { console.error('Leitner update failed:', err) } setStats((prev) => ({ correct: prev.correct + (correct ? 1 : 0), incorrect: prev.incorrect + (correct ? 0 : 1), })) if (currentIndex + 1 >= items.length) { setIsComplete(true) } else { setCurrentIndex((i) => i + 1) } }, [items, currentIndex, unitId]) if (isLoading) { return (
) } if (error) { return (

Fehler: {error}

) } return (
{/* Header */}

Karteikarten

{items.length} Karten
{/* Content */}
{isComplete ? (
{stats.correct > stats.incorrect ? '🎉' : '💪'}

Geschafft!

{stats.correct}

Richtig

{stats.incorrect}

Falsch

) : items.length > 0 ? (
handleAnswer(true)} onIncorrect={() => handleAnswer(false)} isDark={isDark} /> {/* Audio Button */}
) : (

Keine Karteikarten verfuegbar.

)}
) }