import { useState, useEffect } from 'react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, } from 'recharts'; import { api } from '../../api/client'; import { useExerciseStore } from '../../stores/exerciseStore'; import type { SessionLog } from '../../types'; export function ExerciseChart() { const { exercises, fetchExercises } = useExerciseStore(); const [selectedId, setSelectedId] = useState(null); const [chartData, setChartData] = useState<{ date: string; weight: number }[]>([]); const [loading, setLoading] = useState(false); useEffect(() => { fetchExercises(); }, [fetchExercises]); useEffect(() => { if (!selectedId) { setChartData([]); return; } setLoading(true); api.exercises .history(selectedId, 50) .then((logs: SessionLog[]) => { // Gruppiere nach Datum, nehme max Gewicht pro Tag const byDate = new Map(); for (const log of logs) { const date = new Date(log.logged_at).toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', }); const current = byDate.get(date) || 0; if (log.weight_kg > current) { byDate.set(date, log.weight_kg); } } const data = Array.from(byDate.entries()) .map(([date, weight]) => ({ date, weight })) .reverse(); setChartData(data); }) .catch(() => setChartData([])) .finally(() => setLoading(false)); }, [selectedId]); return (

Gewichtsverlauf

{loading ? (
Laden...
) : chartData.length === 0 ? (
{selectedId ? 'Keine Daten vorhanden' : 'Wähle eine Übung aus'}
) : (
[`${value} kg`, 'Max. Gewicht']} />
)}
); }