// Une proposition de programme : raison, source, aperçu de ce qui change par // rapport au programme courant, puis « Adopter » ou « Refuser ». // Glossaire : docs/glossaire/proposition.md import { append, h, setAccent } from "./dom.js"; import { inkOn, sessionColor } from "./color.js"; import { mediumDay, plural, restLabel } from "./format.js"; import { TRACKING_LABELS } from "./labels.js"; import { diffProfile, diffProgram } from "./program-diff.js"; import { toast } from "./feedback.js"; import { href } from "./router.js"; import { INTENSITY_LABELS, ZONE_LABELS } from "./home-model.js"; import { newFindings } from "../domain/checks.js"; /** @type {Record} */ export const SOURCE_LABELS = { ai: "Proposé par ton IA", link: "Reçu par un lien", import: "Venu d'un fichier importé", atelier: "Repris d'Atelier", example: "Programme d'exemple", app: "Modifié dans l'app", }; /** * Ce qu'est devenue une proposition, d'après le journal : son id est stable * (tiré de son contenu), rouvrir le même lien retrouve donc la même. * @param {readonly import("../domain/events.js").DomainEvent[]} events * @param {string} id * @returns {{event: Extract|null, outcome: "pending"|"adopted"|"rejected", at: string|null}} */ export function proposalOutcome(events, id) { const event = /** @type {any} */ (events.find((e) => e.id === id && e.type === "ProgramProposed") ?? null); for (const e of events) { if (e.type === "ProgramAdopted" && e.data.proposalId === id) return { event, outcome: "adopted", at: e.at }; if (e.type === "ProposalRejected" && e.data.proposalId === id) return { event, outcome: "rejected", at: e.at }; } return { event, outcome: "pending", at: null }; } /** * Une valeur lisible dans l'aperçu. * @param {string} field * @param {unknown} v */ function show(field, v) { if (v === null || v === undefined || v === "") return "—"; if (field === "rest" && typeof v === "number") return restLabel(v); if (field === "tracking") return TRACKING_LABELS[/** @type {keyof TRACKING_LABELS} */ (v)] ?? String(v); if (field === "zones" && Array.isArray(v)) return v.map((z) => ZONE_LABELS[z] ?? z).join(", "); if (field === "intensity") return INTENSITY_LABELS[String(v)] ?? String(v); if (field === "minutes") return `${v} min`; if (field === "inPlan") return v ? "oui" : "non"; if (Array.isArray(v)) return v.join(", "); return String(v); } /** @param {import("./program-diff.js").FieldChange} f */ const fieldLine = (f) => f.field === "order" ? h("li", { data: { change: "changed" } }, "Ordre des exercices revu") : h("li", { data: { change: "changed" } }, `${f.label} : `, h("del", null, show(f.field, f.from)), " → ", h("ins", null, show(f.field, f.to))); /** @param {import("../domain/program.js").ProgramExercise} x */ const target = (x) => `${x.sets} × ${x.reps}, repos ${restLabel(x.rest)}`; /** @type {Record} */ const CHANGE_WORD = { added: "Nouvelle", removed: "Retirée", changed: "Modifiée", same: "Inchangée" }; /** * L'aperçu des changements. * @param {import("../domain/program.js").Program|null} current * @param {import("../domain/program.js").Program} proposed * @param {import("../domain/profile.js").Profile} currentProfile * @param {import("../domain/profile.js").Profile|null} proposedProfile */ export function diffView(current, proposed, currentProfile, proposedProfile) { const d = diffProgram(current, proposed); const profile = proposedProfile ? diffProfile(currentProfile, proposedProfile) : []; const summary = d.isNew ? `${plural(proposed.sessions.length, "séance")}, ${plural(proposed.sessions.reduce((n, s) => n + s.exercises.length, 0), "exercice")}.` : d.identical ? "Identique à ton programme actuel." : [d.counts.added && `${d.counts.added} ajout${d.counts.added > 1 ? "s" : ""}`, d.counts.removed && `${d.counts.removed} retrait${d.counts.removed > 1 ? "s" : ""}`, d.counts.changed && `${d.counts.changed} modification${d.counts.changed > 1 ? "s" : ""}`].filter(Boolean).join(" · ") || "Petits changements."; const sessions = d.sessions.map((s) => { const plaque = h("span", { class: "plaque plaque--sm", "aria-hidden": "true" }, s.key); const color = sessionColor(s.color); setAccent(plaque, color, inkOn(color)); const lines = s.change === "same" ? [] : [ ...s.fields.map(fieldLine), ...s.exercises.filter((x) => d.isNew || s.change !== "changed" || x.change !== "same").map((x) => { if (d.isNew || s.change === "added") return h("li", { data: { change: "added" } }, `${x.name} · ${target(x.item)}`); if (x.change === "added") return h("li", { data: { change: "added" } }, `${x.name} · ${target(x.item)}`); if (x.change === "removed") return h("li", { data: { change: "removed" } }, x.name); return h("li", { data: { change: "changed" } }, x.name, " · ", x.fields.map((f, i) => [i ? ", " : "", `${f.label} `, h("del", null, show(f.field, f.from)), " → ", h("ins", null, show(f.field, f.to))])); }), ]; return h("li", { class: "diff__item" }, h("div", { class: "diff__head" }, plaque, h("span", { class: "diff__what" }, `Séance ${s.key} · ${s.name}`), !d.isNew && h("span", { class: "mark" }, CHANGE_WORD[s.change])), lines.length > 0 && h("ul", { class: "diff__lines" }, lines)); }); /** @param {string} title @param {import("./program-diff.js").ItemDiff[]} items */ const others = (title, items) => { const changed = items.filter((x) => d.isNew || x.change !== "same"); if (!changed.length) return null; return h("li", { class: "diff__item" }, h("div", { class: "diff__head" }, h("span", { class: "diff__what" }, title)), h("ul", { class: "diff__lines" }, changed.map((x) => x.change === "changed" ? h("li", { data: { change: "changed" } }, x.name, " · ", x.fields.map((f, i) => [i ? ", " : "", `${f.label} `, h("del", null, show(f.field, f.from)), " → ", h("ins", null, show(f.field, f.to))])) : h("li", { data: { change: d.isNew ? "added" : x.change } }, x.name)))); }; return h("div", { class: "proposal-diff" }, h("p", { class: "mark" }, d.isNew ? "Nouveau programme · " : "Par rapport à ton programme · ", summary), h("ul", { class: "diff" }, d.name && h("li", { class: "diff__item" }, h("ul", { class: "diff__lines" }, fieldLine(d.name))), sessions, others("Activités", d.activities), others("Cardio", d.cardio), others("Repères", d.rules), profile.length > 0 && h("li", { class: "diff__item" }, h("div", { class: "diff__head" }, h("span", { class: "diff__what" }, "Ton profil")), h("ul", { class: "diff__lines" }, profile.map((p) => p.added ? [...p.added.map((x) => h("li", { data: { change: "added" } }, `${p.label} : ${x}`)), ...(p.removed ?? []).map((x) => h("li", { data: { change: "removed" } }, `${p.label} : ${x}`))] : h("li", { data: { change: "changed" } }, `${p.label} : `, h("del", null, show(p.field, p.from)), " → ", h("ins", null, show(p.field, p.to)))))))); } /** * Les alertes que la proposition ajoute au programme en cours (lot 5b) : à lire * avant d'adopter, sans rien bloquer. * @param {import("../domain/program.js").Program|null} current * @param {import("../domain/program.js").Program} proposed * @param {import("../domain/profile.js").Profile} profile */ export function findingsView(current, proposed, profile) { const findings = newFindings(current, proposed, profile); if (!findings.length) return null; return h("div", { class: "findings", role: "note" }, h("p", { class: "mini-label" }, "À vérifier"), h("ul", { class: "findings__list" }, findings.map((f) => h("li", { data: { code: f.code } }, f.text))), h("p", { class: "muted" }, "Tu peux l'adopter quand même, ou demander une version corrigée à ton IA.")); } /** * Une proposition en attente, avec ses deux actions. * @param {{proposal: import("../domain/state.js").Proposal, store: import("./store.js").Store, nav?: import("./router.js").Navigation, headingLevel?: "h1"|"h2"}} o */ export function proposalCard({ proposal, store, nav, headingLevel = "h2" }) { const state = store.getState(); const p = proposal.program; const adopt = () => { store.dispatch("ProgramAdopted", { program: p, source: proposal.source, proposalId: proposal.id, ...(proposal.reason ? { reason: proposal.reason } : {}) }); if (proposal.profile) store.dispatch("ProfileUpdated", { profile: proposal.profile }); toast(`Programme « ${p.name} » adopté.`); nav?.go(href.home(), { replace: true }); }; const reject = () => { store.dispatch("ProposalRejected", { proposalId: proposal.id }); toast("Proposition refusée."); nav?.go(href.home(), { replace: true }); }; const id = `prop-${proposal.id}`; return h("section", { class: "block block--today proposal", "aria-labelledby": id }, h("p", { class: "kicker" }, "À valider · ", SOURCE_LABELS[proposal.source] ?? proposal.source, " · ", mediumDay(proposal.at.slice(0, 10))), h(headingLevel, { class: headingLevel === "h1" ? "title" : "block__title", id }, p.name), p.subtitle && h("p", null, p.subtitle), proposal.reason && h("p", { class: "coach-note" }, proposal.reason), diffView(state.program, p, state.profile, proposal.profile), findingsView(state.program, p, proposal.profile ?? state.profile), h("div", { class: "btn-stack" }, h("button", { type: "button", class: "btn btn--primary btn--block btn--lg", on: { click: adopt } }, "Adopter"), h("button", { type: "button", class: "btn btn--ghost btn--block", on: { click: reject } }, "Refuser"))); } /** * L'écran d'une proposition, retrouvée par son id (lien rouvert, accueil). * @param {{store: import("./store.js").Store, nav: import("./router.js").Navigation, id: string}} ctx * @returns {import("./router.js").Screen} */ export function proposalScreen({ store, nav, id }) { const node = h("div", { class: "screen" }); const back = h("button", { type: "button", class: "back", on: { click: () => nav.back(href.home()) } }, "← Accueil"); const pending = store.getState().proposals.find((x) => x.id === id); if (pending) { node.append(h("header", { class: "screen-head" }, back, h("p", { class: "kicker" }, "Proposition"), h("h1", { class: "title" }, "À valider")), proposalCard({ proposal: pending, store, nav })); return { node, title: "Proposition" }; } const { event, outcome, at } = proposalOutcome(store.getEvents(), id); if (!event) { node.append(h("header", { class: "screen-head" }, back, h("h1", { class: "title" }, "Proposition introuvable")), h("p", { class: "empty" }, "Elle n'est pas dans ton journal.")); return { node, title: "Proposition introuvable" }; } const state = store.getState(); const reopen = () => { const fresh = store.dispatch("ProgramProposed", { ...event.data }); nav.go(href.proposal(fresh.id), { replace: true }); }; append(node, [ h("header", { class: "screen-head" }, back, h("p", { class: "kicker" }, SOURCE_LABELS[event.data.source] ?? event.data.source), h("h1", { class: "title" }, outcome === "adopted" ? "Déjà adoptée" : "Déjà refusée")), h("p", { class: "coach-note coach-note--plain" }, outcome === "adopted" ? `Tu as adopté cette proposition le ${mediumDay(/** @type {string} */ (at).slice(0, 10))}. Ce lien ne change plus rien.` : `Tu as refusé cette proposition le ${mediumDay(/** @type {string} */ (at).slice(0, 10))}. Le lien est le même : c'est la même proposition.`), h("section", { class: "block" }, h("h2", { class: "block__title" }, event.data.program.name), event.data.reason && h("p", { class: "coach-note" }, event.data.reason), diffView(state.program, event.data.program, state.profile, event.data.profile ?? null)), outcome === "rejected" && h("div", { class: "action-bar" }, h("div", { class: "action-bar__in" }, h("button", { type: "button", class: "btn btn--primary btn--block btn--lg", on: { click: reopen } }, "La réexaminer")))]); return { node, title: "Proposition" }; }