96 lines
3.3 KiB
TypeScript
96 lines
3.3 KiB
TypeScript
import { RotateCcw, Send } from 'lucide-react';
|
|
import { useEffect, useState } from 'react';
|
|
import { fetchNextLesson } from '../api/lessons';
|
|
import { submitAttempt } from '../api/practice';
|
|
import type { AttemptResponse, Exercise, Lesson } from '../api/types';
|
|
import { AudioRecorder } from '../components/AudioRecorder';
|
|
import { PronunciationText } from '../components/PronunciationText';
|
|
import { ScoreBadge } from '../components/ScoreBadge';
|
|
|
|
export function PracticePage() {
|
|
const [lesson, setLesson] = useState<Lesson | null>(null);
|
|
const [exercise, setExercise] = useState<Exercise | null>(null);
|
|
const [audio, setAudio] = useState<Blob | null>(null);
|
|
const [result, setResult] = useState<AttemptResponse | null>(null);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
useEffect(() => {
|
|
loadNext();
|
|
}, []);
|
|
|
|
async function loadNext() {
|
|
setError('');
|
|
setAudio(null);
|
|
setResult(null);
|
|
try {
|
|
const next = await fetchNextLesson();
|
|
setLesson(next.lesson);
|
|
setExercise(next.exercise);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Uebung konnte nicht geladen werden');
|
|
}
|
|
}
|
|
|
|
async function submit() {
|
|
if (!exercise || !audio) return;
|
|
setLoading(true);
|
|
setError('');
|
|
try {
|
|
setResult(await submitAttempt(exercise.id, audio));
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Bewertung fehlgeschlagen');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
if (error) return <p className="form-error">{error}</p>;
|
|
if (!lesson || !exercise) return <p className="muted">Lade Uebung...</p>;
|
|
|
|
return (
|
|
<div className="view-stack">
|
|
<header className="view-header">
|
|
<div>
|
|
<p className="eyebrow">{lesson.title}</p>
|
|
<h1>Aussprache</h1>
|
|
</div>
|
|
<button className="ghost-button" onClick={loadNext}>
|
|
<RotateCcw size={18} /> Andere Uebung
|
|
</button>
|
|
</header>
|
|
|
|
<section className="panel practice-panel">
|
|
<p className="muted">{exercise.prompt_text}</p>
|
|
<PronunciationText targetText={exercise.target_text} words={result?.attempt.word_scores} />
|
|
<div className="action-row">
|
|
<AudioRecorder onRecorded={setAudio} disabled={loading} />
|
|
<button className="primary-button" disabled={!audio || loading} onClick={submit}>
|
|
<Send size={18} /> Bewerten
|
|
</button>
|
|
</div>
|
|
{audio && !result && <p className="muted">Aufnahme bereit. Jetzt bewerten.</p>}
|
|
</section>
|
|
|
|
{result && (
|
|
<section className="result-grid">
|
|
<div className="panel result-panel">
|
|
<h2>Bewertung</h2>
|
|
<div className="score-grid">
|
|
<ScoreBadge label="Gesamt" value={result.attempt.overall_score} />
|
|
<ScoreBadge label="Accuracy" value={result.attempt.accuracy_score} />
|
|
<ScoreBadge label="Fluency" value={result.attempt.fluency_score} />
|
|
<ScoreBadge label="Completeness" value={result.attempt.completeness_score} />
|
|
</div>
|
|
</div>
|
|
<div className="panel result-panel">
|
|
<h2>Lehrerfeedback</h2>
|
|
<p>{result.teacher.teacher_feedback}</p>
|
|
<p className="muted">{result.teacher.next_recommendation}</p>
|
|
</div>
|
|
</section>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|