Remplacer un exercice en séance (lot 3b)

Lien « Machine prise ? » sur la carte, bibliothèque en mode choix
(#/bibliotheque?remplacer=A.3), proches d'abord ; replaces dans SessionLogged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pierre MARTIN
2026-11-18 14:38:14 +01:00
co-authored by Claude Opus 5.5
parent b385c4566b
commit 38e0283f3d
16 changed files with 262 additions and 20 deletions
+63 -8
View File
@@ -15,6 +15,8 @@ import { screenHeader } from "../layout.js";
import { href } from "../router.js";
import { copyText } from "../files.js";
import { toast } from "../feedback.js";
import { doneCount, draftMatches, parseReplace, replaceExercise, replaceQuery } from "../session-draft.js";
import { loadDraft, markReopen, saveDraft } from "../draft-storage.js";
/** @typedef {import("../../domain/library.js").LibraryCriteria} LibraryCriteria */
@@ -33,6 +35,24 @@ export function libraryScreen({ store, nav, query }) {
/** @type {LibraryCriteria} */
let criteria = parseCriteria(query);
const total = Object.keys(EXERCISES).length;
const pick = pickContext(store.getState(), query);
// Le mode choix reste dans l'adresse quand les filtres changent (ADR 0009).
const listHref = (/** @type {LibraryCriteria} */ c) =>
href.library([pick ? replaceQuery(pick.sessionKey, pick.index) : "", formatCriteria(c)].filter(Boolean).join("&"));
/** @param {string} ref */
const choose = (ref) => {
if (!pick) return;
replaceExercise(pick.draft, pick.index, ref, pick.item, store.getState());
saveDraft(pick.draft);
markReopen(pick.index);
nav.back(href.session(pick.sessionKey));
};
/** @param {string} ref @param {string} title @param {string} meta */
const entry = (ref, title, meta) => pick
? h("li", null, h("button", { type: "button", class: "row", on: { click: () => choose(ref) } },
h("span", { class: "row__main" }, h("span", { class: "row__title" }, title), h("span", { class: "row__meta" }, meta)),
h("span", { class: "mark" }, "Choisir")))
: row(href.libraryItem(ref), title, meta);
const search = h("input", {
class: "field__input", type: "search", id: "lib-q", placeholder: "squat, développé, pont…",
@@ -44,7 +64,7 @@ export function libraryScreen({ store, nav, query }) {
/** @param {LibraryCriteria} next */
const update = (next) => {
criteria = parseCriteria(formatCriteria(next)); // mêmes règles qu'à la lecture
nav.set(href.library(formatCriteria(criteria)));
nav.set(listHref(criteria));
paint();
};
@@ -90,9 +110,8 @@ export function libraryScreen({ store, nav, query }) {
count === 0 && generic.length === 0 && h("p", { class: "coach-note coach-note--plain" }, "Aucun exercice ne répond à ces critères. Retire un filtre, ou décris l'exercice à ton IA : elle peut l'ajouter comme exercice générique."),
groups.map((g) => [
h("h2", { class: "tape" }, g.label, h("span", { class: "tape__hint" }, String(g.items.length))),
h("ul", { class: "rows" }, g.items.map(({ ref, exercise }) => row(href.libraryItem(ref), exercise.name,
`${EQUIPMENT_LABELS[exercise.equipment]} · ${exercise.primary.map((m) => MUSCLES[m].name).join(", ")}`)))]),
generic.length > 0 && [
h("ul", { class: "rows" }, g.items.map(({ ref, exercise }) => entry(ref, exercise.name, metaOf(exercise))))]),
!pick && generic.length > 0 && [
h("h2", { class: "tape" }, "Mes exercices", h("span", { class: "tape__hint" }, "hors bibliothèque")),
h("ul", { class: "rows" }, generic.map((x) => row(href.libraryItem(x.id), x.name, "Exercice générique de ton programme")))]);
};
@@ -104,13 +123,26 @@ export function libraryScreen({ store, nav, query }) {
});
paint();
const near = pick ? [
...(pick.planned.id !== pick.currentId && EXERCISES[pick.planned.id] ? [pick.planned.id] : []),
// Pas un exercice déjà dans la séance : on ne remplace pas par un doublon.
...similarExercises(pick.planned.id, 12).filter((r) => !pick.draft.exercises.some((x) => x.id === r)).slice(0, 8),
] : [];
const node = h("div", { class: "screen" },
screenHeader({ back: { label: "Accueil", onClick: () => nav.back(href.home()) }, kicker: "Exercices", title: "Bibliothèque",
sub: "Le geste, les erreurs à éviter et un tutoriel pour chaque exercice." }),
pick
? screenHeader({ back: { label: `Séance ${pick.sessionKey}`, onClick: () => nav.back(href.session(pick.sessionKey)) },
kicker: `Séance ${pick.sessionKey} · exercice ${pick.index + 1}`, title: "Remplacer",
sub: `« ${pick.planned.name} » : choisis un exercice pour cette séance seulement. Séries, répétitions et repos restent ceux du programme.` })
: screenHeader({ back: { label: "Accueil", onClick: () => nav.back(href.home()) }, kicker: "Exercices", title: "Bibliothèque",
sub: "Le geste, les erreurs à éviter et un tutoriel pour chaque exercice." }),
near.length > 0 && [
h("h2", { class: "tape" }, "Proches", h("span", { class: "tape__hint" }, "mêmes muscles")),
h("ul", { class: "rows" }, near.map((ref) => entry(ref, EXERCISES[ref].name + (ref === pick?.planned.id ? " (prévu)" : ""), metaOf(EXERCISES[ref]))))],
pick && h("h2", { class: "tape" }, "Toute la bibliothèque"),
h("div", { class: "field" }, h("label", { class: "field__label", for: "lib-q" }, "Rechercher"), search),
filters,
results);
return { node, title: "Bibliothèque", destroy: () => clearTimeout(typing) };
return { node, title: pick ? "Remplacer un exercice" : "Bibliothèque", destroy: () => clearTimeout(typing) };
}
/**
@@ -171,11 +203,34 @@ export function libraryItemScreen({ store, nav, today, id }) {
h("h2", { class: "tape" }, "Exercices proches"),
h("ul", { class: "rows" }, near.map((ref) => {
const x = EXERCISES[ref];
return row(href.libraryItem(ref), x.name, `${EQUIPMENT_LABELS[x.equipment]} · ${x.primary.map((m) => MUSCLES[m].name).join(", ")}`);
return row(href.libraryItem(ref), x.name, metaOf(x));
}))]);
return { node, title: name };
}
/**
* Le mode choix (« remplacer=A.3 ») : valable seulement si le brouillon gardé
* porte sur cette séance et que l'exercice n'a encore aucune série faite.
* @param {import("../../domain/state.js").State} state
* @param {string} query
*/
function pickContext(state, query) {
const target = parseReplace(query);
if (!target) return null;
const session = state.program?.sessions.find((s) => s.key === target.sessionKey);
const draft = /** @type {any} */ (loadDraft());
if (!session || !Array.isArray(draft?.warmup) || !draftMatches(draft, session, draft.warmup.length)) return null;
const ex = draft.exercises[target.index];
if (!ex || doneCount(ex) > 0) return null;
return {
...target, draft, item: session.exercises[target.index], currentId: ex.id,
planned: ex.replaces ?? { id: ex.id, name: ex.name },
};
}
/** Ligne d'une liste : matériel et muscles principaux. @param {import("../../domain/exercises.js").LibraryExercise} x */
const metaOf = (x) => `${EQUIPMENT_LABELS[x.equipment]} · ${x.primary.map((m) => MUSCLES[m].name).join(", ")}`;
/** @param {string} link @param {string} title @param {string} meta */
function row(link, title, meta) {
return h("li", null, h("a", { class: "row", href: link },
+21 -5
View File
@@ -8,8 +8,8 @@ import { localDay, longDay, plural } from "../format.js";
import { estimatedMinutes } from "../../domain/progression.js";
import { actionBar, screenHeader } from "../layout.js";
import { href } from "../router.js";
import { buildSessionData, completion, createDraft, doneCount, draftMatches, draftTotals, setsInOrder } from "../session-draft.js";
import { clearDraft, loadDraft, saveDraft } from "../draft-storage.js";
import { buildSessionData, completion, createDraft, doneCount, draftMatches, draftTotals, replaceExercise, replaceQuery, setsInOrder, substituteItem } from "../session-draft.js";
import { clearDraft, loadDraft, saveDraft, takeReopen } from "../draft-storage.js";
import { exerciseCard } from "../exercise-card.js";
import { warmupCard, warmupSteps } from "../warmup-card.js";
import { confirmDialog, toast } from "../feedback.js";
@@ -76,18 +76,34 @@ export function sessionScreen({ store, key, date, nav, timer }) {
// ---- blocs ---------------------------------------------------------------------
const color = sessionColor(session.color);
const firstOpen = draft.exercises.findIndex((ex) => doneCount(ex) < ex.sets);
// Retour de la bibliothèque après un remplacement : on rouvre cette carte.
const reopen = takeReopen();
let index = 0;
const blocks = groupBlocks(session.exercises).map((block) => {
const shown = block.items.map((item, k) => {
const ex = draft.exercises[index + k];
return ex.replaces ? substituteItem(item, ex.id) : item;
});
const cards = block.items.map((item, k) => {
const i = index++;
return exerciseCard({
draft, ex: draft.exercises[i], item, sessionKey: session.key, number: String(i + 1),
draft, ex: draft.exercises[i], item: shown[k], sessionKey: session.key, number: String(i + 1),
letter: block.items.length > 1 ? String.fromCharCode(65 + k) : null,
open: resumed && i === firstOpen, store, timer, onChange,
open: (resumed && i === firstOpen) || i === reopen, store, timer, onChange,
onReplace: () => {
// Le brouillon est gardé avant de partir : la bibliothèque le modifie.
saveDraft(draft);
nav.go(href.library(replaceQuery(session.key, i)));
},
onRestore: () => {
replaceExercise(draft, i, /** @type {{id: string}} */ (draft.exercises[i].replaces).id, item, store.getState());
saveDraft(draft);
nav.go(location.hash, { replace: true });
},
});
});
if (block.items.length === 1) return cards[0];
const names = block.items.map((item) => resolveExercise(item).name).join(" + ");
const names = shown.map((item) => resolveExercise(item).name).join(" + ");
const rounds = Math.max(...block.items.map((item) => item.sets));
return h("section", { class: "superset", "aria-label": `Superset : ${names}` },
h("div", { class: "superset__head" },