From b74fc7913ee37bdd4bfded8a16ed5bf6b854c190 Mon Sep 17 00:00:00 2001 From: tisachi Date: Sat, 17 Oct 2026 07:00:23 +0200 Subject: [PATCH] Profil: parametry ciala jako zakladki + osobny div postepu i przywrocony Wstecz --- app/src/lib/body-measurements.ts | 7 + .../pages/profile/ProfileBodyParameters.tsx | 303 +++++++++--------- app/test/ProfileBodyParameters.test.tsx | 16 +- app/test/e2e/profile-mobile.spec.ts | 7 +- 4 files changed, 169 insertions(+), 164 deletions(-) diff --git a/app/src/lib/body-measurements.ts b/app/src/lib/body-measurements.ts index ee2dd73..2237620 100644 --- a/app/src/lib/body-measurements.ts +++ b/app/src/lib/body-measurements.ts @@ -20,6 +20,13 @@ export const MEASUREMENT_TYPES: MeasurementType[] = [ export const SIDE_LABELS: Record = { left: 'Lewy', right: 'Prawy' }; +/* Skrót do listów zwartych (zakładki): pełna nazwa jest w nagłówku panelu. */ +export const PART_SHORT: Record = { + 'Klatka piersiowa': 'Klatka', + Przedramię: 'Przedr.', + Waga: 'Waga', +}; + /* Rodzaj gramatyczny części ciała → poprawna fleksja przymiotnika strony. */ const PART_GENDERS: Record = { 'Klatka piersiowa': 'f', diff --git a/app/src/pages/profile/ProfileBodyParameters.tsx b/app/src/pages/profile/ProfileBodyParameters.tsx index a5ec557..8e885cd 100644 --- a/app/src/pages/profile/ProfileBodyParameters.tsx +++ b/app/src/pages/profile/ProfileBodyParameters.tsx @@ -1,5 +1,13 @@ import { useMemo, useRef, useState, type ReactNode } from 'react'; -import { Plus, Ruler, Scale, Trash2, type LucideIcon } from 'lucide-react'; +import { + Plus, + Ruler, + Scale, + Trash2, + TrendingDown, + TrendingUp, + type LucideIcon, +} from 'lucide-react'; import { addCircumference, addWeight, @@ -20,6 +28,7 @@ import { } from 'recharts'; import { fmtDate } from '../../lib/format'; import { + BackButton, Button, Card, ConfirmDialog, @@ -34,6 +43,7 @@ import { } from '../../components/ui'; import { MEASUREMENT_TYPES, + PART_SHORT, SIDE_LABELS, bodyPartOf, detectBodyPart, @@ -118,40 +128,74 @@ interface PartGroup { /** Wszystkie pomiary (pełna historia), rosnąco po dacie. */ points: GroupPoint[]; sidesPresent: Side[]; - /** Wykres dwuliniowy tylko gdy każda obecna strona ma ≥2 pomiary. */ - chartSided: boolean; +} + +/* Polski pluralis dla „pomiar”. */ +function pomN(n: number): string { + if (n === 1) return '1 pomiar'; + if (n % 10 >= 2 && n % 10 <= 4 && !(n % 100 >= 12 && n % 100 <= 14)) return `${n} pomiary`; + return `${n} pomiarów`; } const WEIGHT_PART = 'Waga'; -/** Delta najnowszego pomiaru vs poprzedni; przy ≥2 stronach liczona osobno per strona. */ -function groupDelta(g: PartGroup): { text: string; className: string } | null { - const signed = (d: number) => - `${d < 0 ? '−' : '+'}${Math.abs(d).toLocaleString('pl-PL', { - maximumFractionDigits: 1, - })} ${g.unit}`; - // waga: spadek = accent, wzrost = warn; obwody: bez oceny kierunku (neutralny) - const deltaClass: (d: number) => string = - g.unit === 'kg' ? (d) => (d < 0 ? 'text-accent-text' : 'text-warn') : () => 'text-ink'; +const fmtVal = (n: number) => n.toLocaleString('pl-PL', { maximumFractionDigits: 1 }); +const signed = (d: number) => `${d < 0 ? '−' : '+'}${fmtVal(Math.abs(d))}`; - if (g.chartSided) { - const parts = 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; - if (d === 0) return null; - return `${SIDE_LABELS[side]} ${signed(d)}`; - }) - .filter((t): t is string => t != null); - if (parts.length === 0) return null; - // strony: kierunki mogą się różnić → bez oceny koloru - return { text: `${parts.join(' · ')} vs poprzedni`, className: 'text-ink' }; - } +/** Skrót do zakładek: najkrótsza jednoznaczna nazwa (pełna jest w nagłówku panelu). */ +function shortPartName(part: string): string { + const known = MEASUREMENT_TYPES.some((t) => t.label === part); + return known ? (PART_SHORT[part] ?? part) : part.split(/\s+/)[0]; +} + +/* Postęp części: zmiana najnowszego pomiaru od pierwszego; spadek wagi = dobry kierunek. */ +function progressOf(g: PartGroup): { text: string; className: string } | null { if (g.points.length < 2) return null; - const d = g.points[g.points.length - 1].value - g.points[g.points.length - 2].value; + const d = g.points[g.points.length - 1].value - g.points[0].value; if (d === 0) return null; - return { text: `${signed(d)} vs poprzedni`, className: deltaClass(d) }; + const className = g.unit === 'kg' ? (d < 0 ? 'text-accent-text' : 'text-warn') : 'text-ink'; + 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, + primary, + primaryClass, + sub, +}: { + down: boolean; + primary: string; + primaryClass: string; + sub: string; +}) { + const Icon = down ? TrendingDown : TrendingUp; + return ( + +
+ + + +
+

Postęp

+

{primary}

+

{sub}

+
+
+
+ ); } /** Etykieta osi X: `17.10`; gdy historia przekracza rok — `17.10.26`. */ @@ -164,80 +208,21 @@ function axisDateLabel(iso: string, withYear: boolean): string { } /** - * Wspólna oś dat dla serii: `[{ date, lewy, prawy }]` (brak pomiaru = null). - * Dwie strony w tym samym momencie → dwa wiersze z rozróżnialną etykietą - * (`17.10 ·L` / `17.10 ·P`) — inaczej starszy pomiar nadpisywałby młodszy. + * Dane wykresu: kategorie osi = numery porządkowe pomiarów (unikalne i równe + * odległości), więc dwa pomiary z tego samego dnia nie scalają się w jeden + * punkt — wykres rysuje się zawsze, etykiety osi pokazują daty (tickFormatter). */ -function sideSeriesData(points: GroupPoint[], withYear: boolean) { - const byDate = new Map>>(); - for (const p of points) { - if (p.side == null) continue; - const row = byDate.get(p.date) ?? {}; - row[p.side] = p.value; - byDate.set(p.date, row); - } - const rows: { date: string; lewy: number | null; prawy: number | null }[] = []; - for (const [date, row] of [...byDate.entries()].sort((a, b) => a[0].localeCompare(b[0]))) { - const label = axisDateLabel(date, withYear); - if (row.left != null && row.right != null) { - // dwie strony w tym samym momencie → dwie kategorie osi, inaczej recharts - // scalyłby duplikat etykiety w jeden punkt i linia „gubiłaby” pomiary - rows.push({ date: `${label} ·L`, lewy: row.left, prawy: null }); - rows.push({ date: `${label} ·P`, lewy: null, prawy: row.right }); - } else if (row.left != null) { - rows.push({ date: label, lewy: row.left, prawy: null }); - } else if (row.right != null) { - rows.push({ date: label, lewy: null, prawy: row.right }); - } - } - return rows; +function chartData(points: GroupPoint[]) { + return points.map((p, idx) => ({ + idx: String(idx), + value: p.value, + lewy: p.side === 'left' ? p.value : null, + prawy: p.side === 'right' ? p.value : null, + })); } const SIDE_STROKE: Record = { left: 'var(--info)', right: 'var(--accent)' }; -/* Kafel podsumowania części ciała: nazwa + najnowsza wartość + delta; klik = panel poniżej. */ -function PartTile({ - group, - icon: Icon, - active, - onSelect, -}: { - group: PartGroup; - icon: LucideIcon; - active: boolean; - onSelect: () => void; -}) { - const latest = group.points[group.points.length - 1]; - const delta = groupDelta(group); - return ( - - ); -} - /* Panel wybranej części: nagłówek (nazwa + ostatni pomiar) + wykres pełnej historii + lista pomiarów. */ function MeasurementPanel({ group, @@ -253,33 +238,32 @@ function MeasurementPanel({ onDelete: (target: { kind: 'weight' | 'circumference'; id: string; label: string }) => void; }) { const latest = group.points[group.points.length - 1]; - // dwuliniowo tylko gdy każda strona ma min. 2 pomiary (patrz chartSided) - const sided = group.chartSided; + const sided = group.sidesPresent.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() > 300 * 86_400_000; - // jedna strona ma ≥2 pomiary, a druga mniej → wykres tylko dla tej strony, - // żeby pojedynczy pomiar drugiej strony nie „skakał” po linii - const chartSide: Side | null = sided - ? null - : (group.sidesPresent.find((s) => group.points.filter((p) => p.side === s).length >= 2) ?? - null); - const singlePoints = chartSide ? group.points.filter((p) => p.side === chartSide) : group.points; - // ten sam dzień mierzony dwa razy → unikalne kategorie osi (`17.10`, `17.10 ·2`), - // inaczej recharts scala duplikaty i wykres rysuje się dla jednego punktu - const singleData = (() => { - const seen = new Map(); - return singlePoints.map((p) => { - const base = axisDateLabel(p.date, withYear); - const n = (seen.get(base) ?? 0) + 1; - seen.set(base, n); - return { date: n > 1 ? `${base} ·${n}` : base, value: p.value }; - }); - })(); - const sidedData = sided ? sideSeriesData(group.points, withYear) : []; - const chartReady = sided ? sidedData.length >= 2 : singlePoints.length >= 2; + const data = chartData(group.points); + // kategorie osi = numery porządkowe → etykiety i tooltip tłumaczone na daty + const labelAt = (v: string | number) => { + const p = group.points[Number(v)]; + return p ? axisDateLabel(p.date, withYear) : ''; + }; + const chartReady = group.points.length >= 2; + // 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'; + const primaryClass = prog?.className ?? 'text-dim'; + const sub = + group.points.length < 2 + ? 'drugi pomiar pokaże zmianę' + : `${pomN(group.points.length)} · pierwszy → ostatni`; return ( @@ -303,13 +287,21 @@ function MeasurementPanel({ + + {chartReady ? ( {sided ? ( - + - + {group.sidesPresent.map((side) => ( ) : ( (() => { - const stroke = chartSide ? SIDE_STROKE[chartSide] : unitStroke(group.unit); + const stroke = unitStroke(group.unit); const areaId = `bp-area-${encodeURIComponent(group.part)}`; return ( - + @@ -353,7 +349,8 @@ function MeasurementPanel({ - + 1 && - sidesPresent.every((side) => points.filter((p) => p.side === side).length >= 2), }); } return list; @@ -749,6 +740,11 @@ export default function ProfileBodyParameters() { return ( <> + {/* podstrona: „Wstecz” do przeglądu profilu (na telefonie = „<” w topmenu) */} +
+ +
+ {active == null ? ( ) : ( <> - {/* pasek kaflów: najnowsze wartości wszystkich części; klik = panel poniżej */} -
- {groups.map((g) => ( - setSelected(g.part)} - /> - ))} -
+ {/* zakładki części: skrót + najnowsza wartość; strzałki, gdy się nie mieszczą */} + { + const latest = g.points[g.points.length - 1]; + return { + id: g.part, + label: ( + + {shortPartName(g.part)} + {fmtVal(latest.value)} + + ), + }; + })} + active={active.part} + onChange={(id) => setSelected(id)} + />
{ renderApp(); await loginToProfile(user); - // delta: 81.8 (15.08) − 82.5 (01.08) = −0,7 kg (spadek) — liczona z pełnej historii + // postęp: 81.8 (15.08) − 82.5 (01.08) = −0,7 kg (spadek) — liczony z pełnej historii expect(await screen.findByText('82.5 kg')).toBeInTheDocument(); - expect(screen.getByText('−0,7 kg vs poprzedni')).toBeInTheDocument(); + expect(screen.getByText('−0,7 kg od pierwszego')).toBeInTheDocument(); // 1 kontrolka = 1 miejsce: w treści brak przycisków dodawania — dodaje się z topmenu expect(screen.queryByRole('button', { name: /Dodaj wagę/ })).not.toBeInTheDocument(); @@ -293,9 +293,9 @@ describe('Profile — body parameter removal', () => { expect(await screen.findByText('82.5 kg')).toBeInTheDocument(); await user.click(selectPart('Talia')); expect(screen.getByText('10.07.2026')).toBeInTheDocument(); - // 10.08 w kafelu, nagłówku panelu i w wierszu listy - expect(screen.getAllByText('10.08.2026').length).toBe(3); - // delta Talii: 82 (10.08) − 80 (10.07) = +2 cm - expect(screen.getByText('+2 cm vs poprzedni')).toBeInTheDocument(); + // 10.08 w nagłówku panelu i w wierszu listy (zakładka pokazuje samą wartość) + expect(screen.getAllByText('10.08.2026').length).toBe(2); + // postęp Talii: 82 (10.08) − 80 (10.07) = +2 cm + expect(screen.getByText('+2 cm od pierwszego')).toBeInTheDocument(); }); }); diff --git a/app/test/e2e/profile-mobile.spec.ts b/app/test/e2e/profile-mobile.spec.ts index 0463cbc..60d01e8 100644 --- a/app/test/e2e/profile-mobile.spec.ts +++ b/app/test/e2e/profile-mobile.spec.ts @@ -82,7 +82,7 @@ test('mobile: dane osobowe — bottom sheet mięśni z zakładkami i wyszukiwark await expectNoHorizontalScroll(page, 'dane osobowe'); }); -test('mobile: parametry ciała — pasek kaflów, delta wagi i dodanie pomiaru z topmenu', async ({ +test('mobile: parametry ciała — zakładki części, postęp wagi i dodanie pomiaru z topmenu', async ({ page, }) => { const now = new Date(); @@ -101,8 +101,9 @@ test('mobile: parametry ciała — pasek kaflów, delta wagi i dodanie pomiaru z await page.getByRole('link', { name: /Parametry ciała/ }).click(); await expect(page.getByRole('heading', { name: 'Parametry ciała', level: 1 })).toBeVisible(); - // kafel „Waga” wybrany domyślnie: panel z ostatnim pomiarem + delta vs poprzedni (spadek) - await expect(page.getByRole('button', { name: /Waga.*−0,7 kg vs poprzedni/ })).toBeVisible(); + // zakładka „Waga” wybrana domyślnie: panel z ostatnim pomiarem + postęp (spadek) + await expect(page.getByRole('button', { name: /^Waga 81,8/ })).toBeVisible(); + await expect(page.getByText('−0,7 kg od pierwszego')).toBeVisible(); await expect(page.getByText('81.8 kg').first()).toBeVisible(); // brak przycisków dodawania w treści — 1 kontrolka = 1 miejsce (tylko topmenu)