Profil: strony mierzone jako dwa niezalezne divy s wlasnymi danymi

This commit is contained in:
tisachi
2026-10-17 07:18:38 +02:00
parent a43fcd56b2
commit 0c688c12c1
3 changed files with 134 additions and 109 deletions

View File

@@ -35,6 +35,7 @@ import {
H3,
Input,
Modal,
PageHeader,
Select,
Skeleton,
Tabs,
@@ -155,18 +156,6 @@ function progressOf(g: PartGroup): { text: string; className: string } | null {
return { text: `${signed(d)} ${g.unit} od pierwszego`, className };
}
/* Zmiana ostatniego pomiaru każdej strony vs poprzednik jej strony; 0 = bez zmian. */
function sideDeltas(g: PartGroup): { side: Side; text: string; zero: boolean }[] {
return g.sidesPresent
.map((side) => {
const pts = g.points.filter((p) => p.side === side);
if (pts.length < 2) return null;
const d = pts[pts.length - 1].value - pts[pts.length - 2].value;
return { side, text: `${signed(d)} ${g.unit}`, zero: d === 0 };
})
.filter((x): x is { side: Side; text: string; zero: boolean } => x != null);
}
/* Osobny div postępu wybranej części: kierunek zmiany + wynik (strony osobno, gdy łączone). */
function ProgressCard({
down,
@@ -298,7 +287,8 @@ function PartAreaChart({
);
}
/* Panel wybranej części: nagłówek (nazwa + ostatni pomiar) + wykres pełnej historii + lista pomiarów. */
/* Panel wybranej części. Strony mierzone (lewy/prawy) = dwa niezależne divy,
każdy z własnym nagłówkiem, postępem, wykresem i listą — lewy zawsze pierwszy. */
function MeasurementPanel({
group,
icon: Icon,
@@ -312,71 +302,118 @@ function MeasurementPanel({
onShowMore: () => void;
onDelete: (target: { kind: 'weight' | 'circumference'; id: string; label: string }) => void;
}) {
const latest = group.points[group.points.length - 1];
const sided = group.sidesPresent.length > 1;
if (!sided) {
return (
<Card>
<PartSection
title={group.part}
icon={Icon}
unit={group.unit}
points={group.points}
stroke={unitStroke(group.unit)}
chartName={group.part}
visible={visible}
onShowMore={onShowMore}
onDelete={onDelete}
/>
</Card>
);
}
const labels = sideLabelsFor(group.part);
const unsidePoints = group.points.filter((p) => p.side == null);
return (
<div className="flex flex-col gap-4">
{(['left', 'right'] as Side[])
.filter((s) => group.sidesPresent.includes(s))
.map((s) => (
<Card key={s}>
<PartSection
title={`${group.part} — ${labels[s].toLowerCase()}`}
icon={Icon}
unit={group.unit}
points={group.points.filter((p) => p.side === s)}
stroke={SIDE_STROKE[s]}
chartName={labels[s].toLowerCase()}
visible={visible}
onShowMore={onShowMore}
onDelete={onDelete}
/>
</Card>
))}
{unsidePoints.length > 0 && (
<Card>
<PartSection
title={group.part}
icon={Icon}
unit={group.unit}
points={unsidePoints}
stroke={unitStroke(group.unit)}
chartName={group.part}
visible={visible}
onShowMore={onShowMore}
onDelete={onDelete}
/>
</Card>
)}
</div>
);
}
/* Sekcja części/strony: nagłówek (nazwa + ostatni pomiar) + postęp + wykres historii + lista pomiarów. */
function PartSection({
title,
icon: Icon,
unit,
points,
stroke,
chartName,
visible,
onShowMore,
onDelete,
}: {
title: string;
icon: LucideIcon;
unit: 'kg' | 'cm';
points: GroupPoint[];
stroke: string;
chartName: string;
visible: number;
onShowMore: () => void;
onDelete: (target: { kind: 'weight' | 'circumference'; id: string; label: string }) => void;
}) {
const latest = points[points.length - 1];
// historia przekracza rok → oś X z rokiem (również przy przerwie >12 mies. w jednym roku)
const withYear =
new Date(group.points[group.points.length - 1].date).getTime() -
new Date(group.points[0].date).getTime() >
new Date(points[points.length - 1].date).getTime() - new Date(points[0].date).getTime() >
300 * 86_400_000;
// strony osobno (lewa zawsze pierwsza) — dwa niezależne wykresy jeden pod drugim
const seriesList: {
key: string;
caption: string;
name: string;
stroke: string;
points: GroupPoint[];
}[] = sided
? (['left', 'right'] as Side[])
.filter((s) => group.sidesPresent.includes(s))
.map((s) => ({
key: s,
caption: sideLabelsFor(group.part)[s],
name: sideLabelsFor(group.part)[s].toLowerCase(),
stroke: SIDE_STROKE[s],
points: group.points.filter((p) => p.side === s),
}))
: [
{
key: 'all',
caption: '',
name: group.part,
stroke: unitStroke(group.unit),
points: group.points,
},
];
// osobna strona: przy >1 strony wynik per strona, poza tym zmiana od pierwszego pomiaru
const deltas = sideDeltas(group);
const prog = progressOf(group);
const parts =
sided && deltas.length > 0
? deltas.map((d) => `${SIDE_LABELS[d.side]} ${d.zero ? 'bez zmian' : d.text}`).join(' · ')
: (prog?.text ?? '');
const primary = parts || 'bez zmian';
// postęp sekcji (dla strony: osobna, przy łączonej: per strona): zmiana od pierwszego pomiaru
const prog = progressOf({ part: title, unit, points, sidesPresent: [] });
const primary = prog?.text ?? 'bez zmian';
const primaryClass = prog?.className ?? 'text-dim';
const sub =
group.points.length < 2
points.length < 2
? 'drugi pomiar pokaże zmianę'
: `${pomN(group.points.length)} · pierwszy → ostatni`;
: `${pomN(points.length)} · pierwszy → ostatni`;
return (
<Card>
<>
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
<div className="flex items-center gap-2.5">
<span className="grid h-8 w-8 place-items-center rounded-[10px] bg-accent/10 text-accent-text">
<span
className="grid h-8 w-8 place-items-center rounded-[10px] bg-input"
style={{ color: stroke }}
>
<Icon className="h-4 w-4" />
</span>
<H3 className="mb-0">{group.part}</H3>
<H3 className="mb-0">{title}</H3>
</div>
<span className="flex flex-col items-end">
<span className="flex items-baseline gap-2">
<span className="font-display text-[1.35rem] font-bold leading-none tabular-nums">
{latest.value} {group.unit}
</span>
<span className="text-[0.75rem] text-dim">
{latest.side ? `${SIDE_LABELS[latest.side]} · ` : ''}
{fmtDate(latest.date)}
{latest.value} {unit}
</span>
<span className="text-[0.75rem] text-dim">{fmtDate(latest.date)}</span>
</span>
</span>
</div>
@@ -388,40 +425,27 @@ function MeasurementPanel({
sub={sub}
/>
{seriesList.map((s, i) => (
<div key={s.key} className={i > 0 ? 'mt-5' : undefined}>
{sided && (
<div className="mb-1 flex items-center gap-1.5 text-[0.875rem] text-dim">
<span
className="inline-block h-0.5 w-3 rounded-md"
style={{ background: s.stroke }}
/>
{s.caption}
</div>
)}
{s.points.length >= 2 ? (
<PartAreaChart
name={s.name}
points={s.points}
stroke={s.stroke}
unit={group.unit}
withYear={withYear}
height={sided ? 150 : 220}
/>
) : (
<p className="text-dim">Za mało danych na wykres (min. 2 pomiary).</p>
)}
</div>
))}
{points.length >= 2 ? (
<PartAreaChart
name={chartName}
points={points}
stroke={stroke}
unit={unit}
withYear={withYear}
height={220}
/>
) : (
<p className="text-dim">Za mało danych na wykres (min. 2 pomiary).</p>
)}
{/* lista: najnowsze 3 + „Pokaż więcej" (kolejne 3); pełna historia w wykresie */}
<div>
<ul className="mt-3 flex flex-col gap-1.5 sm:flex-row sm:overflow-x-auto sm:pb-1">
{[...group.points]
{[...points]
.reverse()
.slice(0, visible)
.map((p) => {
const kind = group.unit === 'kg' ? ('weight' as const) : ('circumference' as const);
const kind = unit === 'kg' ? ('weight' as const) : ('circumference' as const);
const sideLabel = p.side ? ` · ${SIDE_LABELS[p.side]}` : '';
return (
<li
@@ -431,7 +455,7 @@ function MeasurementPanel({
<div className="flex items-start justify-between gap-2">
<div className="min-w-0">
<strong className="text-ink">
{p.value} {group.unit}
{p.value} {unit}
</strong>
<div className="mt-0.5 text-[0.875rem] text-dim">
{fmtDate(p.date)}
@@ -445,7 +469,7 @@ function MeasurementPanel({
onDelete({
kind,
id: p.id,
label: `${p.name} ${p.value} ${group.unit} (${fmtDate(p.date)})`,
label: `${p.name} ${p.value} ${unit} (${fmtDate(p.date)})`,
})
}
className="p-1.5 -m-1.5 text-dim hover:text-danger cursor-pointer shrink-0"
@@ -457,13 +481,13 @@ function MeasurementPanel({
);
})}
</ul>
{group.points.length > visible && (
{points.length > visible && (
<Button variant="ghost" size="sm" className="mt-2" onClick={onShowMore}>
Pokaż więcej
</Button>
)}
</div>
</Card>
</>
);
}
@@ -626,7 +650,8 @@ export default function ProfileBodyParameters() {
// menu top: jedyna akcja strony (tworzenie pomiaru) — treść = dane i wybór części
useTopbar({
title: 'Parametry ciała',
// topbar = rodzic („Profil”), nazwa podstrony w PageHeader poniżej (h2)
title: 'Profil',
back: '/profile',
actions: [{ label: 'Dodaj pomiar', onClick: () => setAddOpen(true), icon: Plus }],
});
@@ -752,10 +777,8 @@ export default function ProfileBodyParameters() {
return (
<>
{/* podstrona: „Wstecz” do przeglądu profilu (na telefonie = „<” w topmenu) */}
<div className="mb-4">
<BackButton to="/profile" />
</div>
{/* podstrona: h2 z nazwą sekcji + „Wstecz” (na telefonie additionally „<” w topmenu) */}
<PageHeader title="Parametry ciała" actions={<BackButton to="/profile" />} />
{active == null ? (
<EmptyState

View File

@@ -118,9 +118,10 @@ async function loginToProfile(user: userEvent.User) {
expect(await screen.findByRole('heading', { name: 'Profil' })).toBeInTheDocument();
// body parameters live on their own subpage — open it via the tile
await user.click(await screen.findByRole('link', { name: /Parametry ciała/ }));
// wspólna nazwa sekcji: topbar (h1) = PageHeader (h2)
// topbar (h1) = rodzic „Profil”, nazwa podstrony niżej jako PageHeader (h2)
expect(await screen.findByRole('heading', { name: 'Profil', level: 1 })).toBeInTheDocument();
expect(
await screen.findByRole('heading', { name: 'Parametry ciała', level: 1 }),
await screen.findByRole('heading', { name: 'Parametry ciała', level: 2 }),
).toBeInTheDocument();
}
@@ -207,14 +208,14 @@ describe('Profile — body parameter removal', () => {
renderApp();
await loginToProfile(user);
// pasek kaflów: `Bicepcs` scalony z grupą Biceps — nie ma osobnego kafla
// pasek zakładek: `Bicepcs` scalony z grupą Biceps — nie ma osobnej zakładki
await user.click(selectPart('Biceps'));
expect(screen.queryByText('Bicepcs')).not.toBeInTheDocument();
// nagłówek panelu: najnowszy pomiar grupy (40 cm, 20.08)
// niezależne divy stron + zbiorczy dla pomiarów bez strony
expect(screen.getByRole('heading', { name: 'Biceps — lewy', level: 3 })).toBeInTheDocument();
expect(screen.getByRole('heading', { name: 'Biceps — prawy', level: 3 })).toBeInTheDocument();
// nagłówek sekcji zbiorczej: najnowszy pomiar bez strony (40 cm, 20.08)
expect(screen.getAllByText('40 cm').length).toBe(2); // nagłówek + wiersz listy
// legenda obu stron przy wykresie
expect(screen.getByText('Lewy')).toBeInTheDocument();
expect(screen.getByText('Prawy')).toBeInTheDocument();
// wiersze listy z oznaczeniem strony
expect(screen.getByText('15.08.2026 · Lewy')).toBeInTheDocument();
expect(screen.getByText('15.08.2026 · Prawy')).toBeInTheDocument();
@@ -236,11 +237,11 @@ describe('Profile — body parameter removal', () => {
await loginToProfile(user);
await user.click(selectPart('Udo'));
// nagłówek panelu (ostatni 56) + wiersz listy
// nagłówek sekcji strony (ostatni 56) + wiersz listy
expect((await screen.findAllByText('56 cm')).length).toBeGreaterThanOrEqual(2);
// dwa podpisyane wykresy: lewy zawsze przed prawym
const lewy = screen.getByText('Lewe');
const prawy = screen.getByText('Prawe');
// dwa niezależne divy: „Udo — lewe” zawsze przed „Udo — prawe”
const lewy = screen.getByRole('heading', { name: 'Udo — lewe', level: 3 });
const prawy = screen.getByRole('heading', { name: 'Udo — prawe', level: 3 });
expect(lewy.compareDocumentPosition(prawy) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
// strona z jednym pomiarem uczciwie sygnalizuje za mało danych
expect(screen.getByText(/Za mało danych na wykres/)).toBeInTheDocument();

View File

@@ -86,9 +86,10 @@ async function loginToProfile(user: userEvent.User) {
expect(await screen.findByRole('heading', { name: 'Profil' })).toBeInTheDocument();
// body parameters live on their own subpage — open it via the tile
await user.click(await screen.findByRole('link', { name: /Parametry ciała/ }));
// wspólna nazwa sekcji: topbar (h1) = PageHeader (h2)
// topbar (h1) = rodzic „Profil”, nazwa podstrony niżej jako PageHeader (h2)
expect(await screen.findByRole('heading', { name: 'Profil', level: 1 })).toBeInTheDocument();
expect(
await screen.findByRole('heading', { name: 'Parametry ciała', level: 1 }),
await screen.findByRole('heading', { name: 'Parametry ciała', level: 2 }),
).toBeInTheDocument();
}