Files
FitOS/admin/src/components/ExerciseProgressCards.tsx

181 lines
6.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState } from 'react';
import { ChevronDown, ChevronUp, TrendingDown, TrendingUp } from 'lucide-react';
import {
Bar,
BarChart,
CartesianGrid,
Line,
LineChart,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
} from 'recharts';
import type { ExerciseProgress } from '../api/types';
import { fmtDate, fmtVolume } from '../lib/format';
import { MUSCLE_NAMES } from '../lib/labels';
import { Badge, Tabs } from './ui';
const TOOLTIP_STYLE = {
background: 'var(--bg-card)',
border: '1px solid var(--border)',
borderRadius: 12,
color: 'var(--text)',
};
/**
* Lista postępów per ćwiczenie: wiersz (nazwa + 1RM + delta + objętość + PR),
* tap rozwija kartę z zakładkami 1RM/Objętość i jednym wykresem (renderowany
* tylko dla aktywnej zakładki — unik ostrzeżeń recharts/ResizeObserver).
* Współdzielone z detalem podopiecznego (spójnie).
*/
export default function ExerciseProgressCards({ exercises }: { exercises: ExerciseProgress[] }) {
const [openId, setOpenId] = useState<string | null>(null);
// aktywna zakładka per ćwiczenie (domyślnie 1RM)
const [tabs, setTabs] = useState<Record<string, 'onerm' | 'volume'>>({});
return (
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
{exercises.map((ex) => {
const last = ex.points[ex.points.length - 1];
const prev = ex.points[ex.points.length - 2];
const oneRmDelta =
last?.oneRm != null && prev?.oneRm != null ? last.oneRm - prev.oneRm : null;
const maxOneRm = ex.points.reduce(
(max, p) => (p.oneRm != null && p.oneRm > max ? p.oneRm : max),
0,
);
const isPr = last?.oneRm != null && last.oneRm === maxOneRm && ex.points.length > 1;
const open = openId === ex.exerciseId;
const tab = tabs[ex.exerciseId] ?? 'onerm';
const setTab = (t: 'onerm' | 'volume') => setTabs((m) => ({ ...m, [ex.exerciseId]: t }));
return (
<div
key={ex.exerciseId}
className="rounded-xl border border-line bg-(image:--card-grad) shadow-card"
>
{/* Wiersz: nazwa + mięsień (meta) + 1RM + delta + objętość + PR */}
<button
type="button"
onClick={() => setOpenId(open ? null : ex.exerciseId)}
aria-expanded={open}
className="w-full cursor-pointer rounded-xl px-4 py-3.5 text-left transition-colors hover:bg-line/20"
>
<div className="flex items-center justify-between gap-3">
<div className="min-w-0">
<div className="flex items-center gap-2">
<strong className="truncate text-[0.95rem]">{ex.name}</strong>
{isPr && <Badge color="green">PR</Badge>}
</div>
<div className="text-[0.8125rem] text-dim">
{MUSCLE_NAMES[ex.muscle] || ex.muscle}
</div>
</div>
<div className="flex shrink-0 items-center gap-3">
<div className="text-right">
<div className="font-display text-[1.15rem] font-bold leading-tight tabular-nums">
{last?.oneRm != null ? `${last.oneRm} kg` : '—'}
</div>
<div className="text-[0.75rem] text-dim">
{last?.volumeKg != null
? `objętość ${fmtVolume(last.volumeKg)}`
: 'brak objętości'}
</div>
</div>
{oneRmDelta != null && oneRmDelta !== 0 && (
<span
className={`flex items-center gap-1 text-[0.8125rem] font-semibold tabular-nums ${
oneRmDelta > 0 ? 'text-accent-text' : 'text-warn'
}`}
>
{oneRmDelta > 0 ? (
<TrendingUp className="h-3.5 w-3.5" />
) : (
<TrendingDown className="h-3.5 w-3.5" />
)}
{oneRmDelta > 0 ? '+' : '−'}
{Math.abs(oneRmDelta).toLocaleString('pl-PL')} kg
</span>
)}
{open ? (
<ChevronUp className="h-4 w-4 text-dim" />
) : (
<ChevronDown className="h-4 w-4 text-dim" />
)}
</div>
</div>
</button>
{/* Rozwinięta karta: zakładki 1RM/Objętość + jeden wykres */}
{open && (
<div className="border-t border-line px-4 pb-4">
<Tabs
tabs={[
{ id: 'onerm', label: '1RM' },
{ id: 'volume', label: 'Objętość' },
]}
active={tab}
onChange={(id) => setTab(id as 'onerm' | 'volume')}
/>
{tab === 'onerm' ? <OneRmChart ex={ex} /> : <VolumeChart ex={ex} />}
</div>
)}
</div>
);
})}
</div>
);
}
function OneRmChart({ ex }: { ex: ExerciseProgress }) {
const oneRmPoints = ex.points.filter((p) => p.oneRm != null);
if (oneRmPoints.length < 2) {
return (
<p className="mt-3 text-[0.875rem] text-dim">Za mało danych na wykres 1RM (min. 2 sesje).</p>
);
}
return (
<div className="mt-3">
<ResponsiveContainer width="100%" height={180}>
<LineChart data={oneRmPoints.map((p) => ({ date: fmtDate(p.date), oneRm: p.oneRm }))}>
<CartesianGrid stroke="var(--border)" />
<XAxis dataKey="date" stroke="var(--text-dim)" />
<YAxis stroke="var(--text-dim)" domain={['auto', 'auto']} />
<Tooltip contentStyle={TOOLTIP_STYLE} />
<Line
type="monotone"
dataKey="oneRm"
name="1RM (kg)"
stroke="var(--accent)"
strokeWidth={2}
dot={false}
/>
</LineChart>
</ResponsiveContainer>
</div>
);
}
function VolumeChart({ ex }: { ex: ExerciseProgress }) {
const volumePoints = ex.points.filter((p) => p.volumeKg != null);
if (volumePoints.length < 1) {
return (
<p className="mt-3 text-[0.875rem] text-dim">
Brak danych o objętości (serie bez obciążenia).
</p>
);
}
return (
<div className="mt-3">
<ResponsiveContainer width="100%" height={180}>
<BarChart data={volumePoints.map((p) => ({ date: fmtDate(p.date), volumeKg: p.volumeKg }))}>
<CartesianGrid stroke="var(--border)" />
<XAxis dataKey="date" stroke="var(--text-dim)" />
<YAxis stroke="var(--text-dim)" />
<Tooltip contentStyle={TOOLTIP_STYLE} />
<Bar dataKey="volumeKg" name="Objętość (kg)" fill="var(--info)" radius={[4, 4, 0, 0]} />
</BarChart>
</ResponsiveContainer>
</div>
);
}