Files
ia-fit/app/ui/screens/analysis.js
T

115 lines
7.0 KiB
JavaScript

// Analyses simples (ADR 0006, point 6) : assiduité des douze dernières
// semaines, et pour chaque exercice la courbe de la charge et des répétitions.
// Calculs du domaine : analysis.js.
import { h } from "../dom.js";
import { addDays, daysBetween, mondayOf } from "../../domain/dates.js";
import { attendance, exerciseHistory } from "../../domain/analysis.js";
import { dec1, kg, mediumDay, plural, shortDay } from "../format.js";
import { screenHeader } from "../layout.js";
import { href } from "../router.js";
import { barChart, lineChart } from "../chart.js";
import { EFFORT_LABELS } from "../labels.js";
/** Semaines affichées dans l'assiduité. */
export const ATTENDANCE_WEEKS = 12;
/**
* Les exercices faits au moins une fois, du plus récent au plus ancien.
* @param {import("../../domain/state.js").State} state
* @returns {{id: string, name: string, count: number, last: string}[]}
*/
export function exercisesDone(state) {
/** @type {Map<string, {id: string, name: string, count: number, last: string}>} */
const map = new Map();
for (const s of state.sessions) {
if (s.kind !== "strength") continue;
for (const x of s.exercises) {
if (!x.sets.length) continue;
const cur = map.get(x.id) ?? { id: x.id, name: x.name, count: 0, last: s.date };
cur.count++;
if (s.date >= cur.last) { cur.last = s.date; cur.name = x.name; }
map.set(x.id, cur);
}
}
return [...map.values()].sort((a, b) => (a.last < b.last ? 1 : a.last > b.last ? -1 : a.name.localeCompare(b.name)));
}
/**
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string, exercise: string|null}} ctx
* @returns {import("../router.js").Screen}
*/
export function analysisScreen({ store, nav, today, exercise }) {
return exercise ? exerciseScreen({ store, nav, exercise }) : overview({ store, nav, today });
}
/** @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string}} ctx */
function overview({ store, nav, today }) {
const state = store.getState();
const to = today();
const from = addDays(mondayOf(to), -7 * (ATTENDANCE_WEEKS - 1));
const { weeks, totals } = attendance(state, from, to);
const list = exercisesDone(state);
const node = h("div", { class: "screen" },
screenHeader({ back: { label: "Accueil", onClick: () => nav.back(href.home()) }, kicker: "Ton parcours", title: "Analyses" }),
h("h2", { class: "tape" }, "Assiduité", h("span", { class: "tape__hint" }, `${ATTENDANCE_WEEKS} semaines`)),
h("div", { class: "stats" },
h("div", { class: "stat" }, h("span", { class: "stat__value" }, String(totals.strength)), h("span", { class: "stat__label" }, "force")),
h("div", { class: "stat" }, h("span", { class: "stat__value" }, String(totals.cardio)), h("span", { class: "stat__label" }, "cardio")),
h("div", { class: "stat" }, h("span", { class: "stat__value" }, String(totals.activity)), h("span", { class: "stat__label" }, "activités")),
h("div", { class: "stat" }, h("span", { class: "stat__value" }, dec1(totals.total / ATTENDANCE_WEEKS)), h("span", { class: "stat__label" }, "par semaine"))),
h("figure", { class: "chart analysis-gap" },
barChart({
values: weeks.map((w) => w.total),
label: `Séances par semaine : ${weeks.map((w) => `semaine du ${shortDay(w.monday)}, ${w.total}`).join(" ; ")}`,
labels: [shortDay(weeks[0].monday), "cette semaine"],
}),
h("figcaption", { class: "chart__caption" }, "Séances par semaine, toutes natures. En couleur : la semaine en cours.")),
h("h2", { class: "tape" }, "Par exercice"),
list.length
? h("ul", { class: "rows" }, list.map((x) => h("li", null, h("a", { class: "row", href: href.analysis(x.id) },
h("span", { class: "row__main" }, h("span", { class: "row__title" }, x.name), h("span", { class: "row__meta" }, `${x.count} fois · dernière le ${shortDay(x.last)}`)),
h("span", { class: "chevron", "aria-hidden": "true" })))))
: h("p", { class: "empty" }, "Aucun exercice fait pour l'instant."));
return { node, title: "Analyses" };
}
/** @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, exercise: string}} ctx */
function exerciseScreen({ store, nav, exercise }) {
const state = store.getState();
const points = exerciseHistory(state, exercise);
const name = exercisesDone(state).find((x) => x.id === exercise)?.name ?? exercise;
const back = { label: "Analyses", onClick: () => nav.back(href.analysis()) };
const withLoad = points.filter((p) => p.loadKg !== null && p.loadKg > 0);
const withReps = points.filter((p) => p.bestReps !== null);
const origin = points[0]?.date;
/** @param {typeof points} pts @param {(p: typeof points[number]) => number} y @param {string} what @param {(v: number) => string} format */
const chart = (pts, y, what, format) => pts.length > 1
? h("figure", { class: "chart" }, lineChart({
points: pts.map((p) => ({ x: daysBetween(/** @type {string} */ (origin), p.date), y: y(p) })),
label: `${what}, du ${mediumDay(pts[0].date)} au ${mediumDay(pts[pts.length - 1].date)}`,
format, xLabels: [shortDay(pts[0].date), shortDay(pts[pts.length - 1].date)], minSpan: 2,
}))
: h("p", { class: "muted" }, "Deux séances suffisent pour une courbe.");
const best = withLoad.length ? Math.max(...withLoad.map((p) => /** @type {number} */ (p.loadKg))) : null;
const node = h("div", { class: "screen" },
screenHeader({ back, kicker: plural(points.length, "séance"), title: name }),
h("div", { class: "stats" },
h("div", { class: "stat" }, h("span", { class: "stat__value" }, best !== null ? kg(best) : "—", best !== null && h("small", null, "kg")), h("span", { class: "stat__label" }, "meilleure charge")),
h("div", { class: "stat" }, h("span", { class: "stat__value" }, withReps.length ? String(Math.max(...withReps.map((p) => /** @type {number} */ (p.bestReps)))) : "—"), h("span", { class: "stat__label" }, "meilleure série"))),
withLoad.length > 0 && [h("h2", { class: "tape" }, "Charge"), chart(withLoad, (p) => /** @type {number} */ (p.loadKg), "Charge en kg", (v) => dec1(v))],
h("h2", { class: "tape" }, "Répétitions", h("span", { class: "tape__hint" }, "meilleure série")),
chart(withReps, (p) => /** @type {number} */ (p.bestReps), "Répétitions de la meilleure série", (v) => String(Math.round(v))),
h("h2", { class: "tape" }, "Séance par séance"),
h("ul", { class: "rows" }, [...points].reverse().map((p) => h("li", { class: "row" },
h("span", { class: "row__main" },
h("span", { class: "row__title" }, mediumDay(p.date)),
h("span", { class: "row__meta" }, [
`séance ${p.sessionRef}`, plural(p.sets, "série"),
p.bestReps !== null ? `meilleure ${p.bestReps}` : null,
p.effort ? EFFORT_LABELS[p.effort].label.toLowerCase() : null,
].filter(Boolean).join(" · "))),
h("span", { class: "row__side num analysis__kg" }, p.loadKg ? `${kg(p.loadKg)} kg` : p.seconds ? `${p.seconds} s` : "")))));
return { node, title: name };
}