'use client' import React, { useState, useEffect } from 'react' import { useParams, useRouter } from 'next/navigation' import { useTheme } from '@/lib/ThemeContext' import { AudioButton } from '@/components/learn/AudioButton' 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` } function getKlausurApiUrl() { if (typeof window === 'undefined') return 'http://localhost:8086' const { hostname, protocol } = window.location if (hostname === 'localhost') return 'http://localhost:8086' return `${protocol}//${hostname}/klausur-api` } export default function StoryPage() { const { unitId } = useParams<{ unitId: string }>() const router = useRouter() const { isDark } = useTheme() const [story, setStory] = useState<{ story_html: string; story_text: string; vocab_used: string[]; language: string } | null>(null) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const [language, setLanguage] = useState<'en' | 'de'>('en') const glassCard = isDark ? 'bg-white/10 backdrop-blur-xl border border-white/10' : 'bg-white/80 backdrop-blur-xl border border-black/5' const generateStory = async () => { setIsLoading(true) setError(null) try { // First get the QA data to extract vocabulary const qaResp = await fetch(`${getBackendUrl()}/api/learning-units/${unitId}/qa`) let vocabulary: { english: string; german: string }[] = [] if (qaResp.ok) { const qaData = await qaResp.json() // Convert QA items to vocabulary format vocabulary = (qaData.qa_items || []).map((item: any) => ({ english: item.question, german: item.answer, })) } if (vocabulary.length === 0) { setError('Keine Vokabeln gefunden.') setIsLoading(false) return } // Generate story const resp = await fetch(`${getBackendUrl()}/api/learning-units/${unitId}/generate-story`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ vocabulary, language, grade_level: '5-8' }), }) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) const data = await resp.json() setStory(data) } catch (err: any) { setError(err.message) } finally { setIsLoading(false) } } return (
{/* Header */}

Minigeschichte

{/* Content */}
{story ? ( <> {/* Story Card */}
{story.language === 'en' ? 'English Story' : 'Deutsche Geschichte'}
{/* Vocab used */}
Vokabeln verwendet: {story.vocab_used.length} / {story.vocab_used.length > 0 ? story.vocab_used.join(', ') : '-'}
{/* New Story Button */} ) : (
📖

Minigeschichte

Die KI schreibt eine kurze Geschichte mit deinen Vokabeln. Die Vokabelwoerter werden farbig hervorgehoben.

{error && (

{error}

)}
)}
) }