// Accueil. Par priorité : une proposition en attente, sinon l'onboarding s'il // n'y a pas encore de programme, sinon le HUD (niveau, série), la carte du // jour tirée du planning, les séances, le cardio, les repères et la grille des // autres écrans. import { h, paintWhenIdle, setAccent } from "../dom.js"; import { inkOn, sessionColor } from "../color.js"; import { longDay, plural, shortDay } from "../format.js"; import { dayName, lastDoneByKey, loggedForSlot, nextByRotation, slotLabel, todayModel } from "../home-model.js"; import { actionBar, note } from "../layout.js"; import { href } from "../router.js"; import { onboarding } from "../onboarding.js"; import { proposalCard } from "../proposal-card.js"; import { estimatedMinutes } from "../../domain/progression.js"; import { hud } from "../game.js"; import { toast } from "../feedback.js"; /** @typedef {import("../../domain/state.js").State} State */ /** @typedef {import("../../domain/planning.js").Slot} Slot */ /** @typedef {import("../store.js").Store} Store */ /** * Un programme a-t-il été adopté ? Il n'y a pas de programme par défaut * (ADR 0008) : tant qu'aucun ProgramAdopted n'est au journal, l'accueil montre * l'onboarding. * @param {State} state */ export const hasProgram = (state) => !!state.program && state.programVersions.length > 0; /** * @param {{store: Store, nav: import("../router.js").Navigation, today: () => string, synced?: () => boolean}} ctx * synced : la synchro est-elle activée sur cet appareil ? * @returns {import("../router.js").Screen} */ export function homeScreen({ store, nav, today, synced = () => false }) { const node = h("div", { class: "screen screen--home" }); const paint = () => node.replaceChildren(homeContent(store, nav, today(), synced())); paint(); return { node, title: "", destroy: store.subscribe(paintWhenIdle(node, paint)) }; } /** * Plaque d'une séance : sa lettre sur sa couleur. * @param {string} key * @param {string|undefined} color * @param {string} [cls] */ export function plaque(key, color, cls = "plaque") { const el = h("span", { class: cls, "aria-hidden": "true" }, key); const c = sessionColor(color); setAccent(el, c, inkOn(c)); return el; } /** * @param {Store} store * @param {import("../router.js").Navigation} nav * @param {string} today * @param {boolean} synced * @returns {Node} */ function homeContent(store, nav, today, synced) { const state = store.getState(); if (state.proposals.length) { const [first, ...others] = state.proposals; return h("div", null, h("header", { class: "screen-head" }, h("p", { class: "kicker" }, "IA-Fit"), h("h1", { class: "title" }, "Un programme t'attend")), proposalCard({ proposal: first, store }), others.length > 0 && h("p", { class: "muted" }, `Et ${plural(others.length, "autre proposition")} ensuite.`)); } if (!hasProgram(state)) return onboarding({ store }); const program = /** @type {import("../../domain/program.js").Program} */ (state.program); const model = todayModel(state, today); const last = lastDoneByKey(state); /** @type {Node|null} */ let primary = null; /** @param {Slot} slot @param {string} date */ const markDone = (slot, date) => { const data = loggedForSlot(slot, program, date); if (state.skippedDays.includes(date)) store.dispatch("DayUnskipped", { date }); store.dispatch("SessionLogged", data); toast(`${data.name} : fait.`); }; // ---- carte du jour ----------------------------------------------------------- const kicker = h("p", { class: "kicker" }, "Aujourd'hui · ", longDay(today)); /** @type {import("../dom.js").Child[]} */ let body = []; if (!model.planned) { const next = nextByRotation(state); if (next) { body = [ h("div", { class: "block__head" }, plaque(next.key, next.color), h("div", null, h("h2", { class: "block__title" }, next.name), h("p", { class: "block__meta" }, `${plural(next.exercises.length, "exercice")} · ${estimatedMinutes(next)} min · ${last[next.key] ? "faite le " + shortDay(last[next.key]) : "jamais faite"}`))), h("p", { class: "muted" }, "Pas de planning : la séance faite le moins récemment. "), h("a", { class: "btn btn--block", href: href.week() }, "Planifier ma semaine"), ]; primary = h("a", { class: "btn btn--primary btn--block btn--lg", href: href.session(next.key) }, `Commencer · séance ${next.key}`); } } else if (model.status === "rest") { body = [h("h2", { class: "block__title" }, "Repos"), h("p", { class: "block__meta" }, nextText(model.next, program))]; } else if (model.status === "done") { body = [h("h2", { class: "block__title" }, "C'est fait."), h("p", { class: "block__meta" }, nextText(model.next, program))]; } else if (model.status === "skipped") { body = [ h("h2", { class: "block__title" }, "Passée"), h("p", { class: "block__meta" }, `Marquée non faite. ${nextText(model.next, program)}`), h("button", { type: "button", class: "btn btn--block", on: { click: () => store.dispatch("DayUnskipped", { date: today }) } }, "Annuler, je la fais"), ]; } else if (model.slot) { const slot = model.slot; const label = slotLabel(slot, program); const session = slot.kind === "strength" ? program.sessions.find((s) => s.key === slot.sessionKey) : null; body = [ h("div", { class: "block__head" }, session ? plaque(session.key, session.color) : h("span", { class: "plaque plaque--empty", "aria-hidden": "true" }, slot.kind === "cardio" ? "C" : "A"), h("div", null, h("h2", { class: "block__title" }, session ? session.name : label.title), h("p", { class: "block__meta" }, session ? `Séance ${session.key} · ${plural(session.exercises.length, "exercice")} · ${estimatedMinutes(session)} min` : label.detail))), session?.subtitle && h("p", null, session.subtitle), slot.kind === "cardio" && program.cardio?.[slot.cardioIndex ?? -1]?.text && h("p", { class: "muted" }, program.cardio[slot.cardioIndex ?? -1].text), h("button", { type: "button", class: "btn btn--ghost btn--block", on: { click: () => store.dispatch("DaySkipped", { date: today }) } }, "Pas aujourd'hui"), ]; primary = session ? h("a", { class: "btn btn--primary btn--block btn--lg", href: href.session(session.key) }, "Commencer") : h("button", { type: "button", class: "btn btn--primary btn--block btn--lg", on: { click: () => markDone(slot, today) } }, "Marquer comme fait"); } const late = model.late.length > 0 && h("div", { class: "block__sep" }, h("p", { class: "mark" }, "En retard cette semaine"), h("ul", { class: "rows" }, model.late.map((slot) => { const label = slotLabel(slot, program); return h("li", { class: "row" }, h("span", { class: "row__main" }, h("span", { class: "row__title" }, label.title + (label.detail ? ` · ${label.detail}` : "")), h("span", { class: "row__meta" }, `Prévue ${dayName(slot.date)}`)), slot.kind === "strength" ? h("a", { class: "btn btn--sm", href: href.session(/** @type {string} */ (slot.sessionKey)) }, "Faire") : h("button", { type: "button", class: "btn btn--sm", on: { click: () => markDone(slot, slot.date) } }, "Marquer faite")); }))); const progress = model.planned && h("div", { class: "block__sep" }, h("p", { class: "mark" }, `Semaine · ${model.done}/${model.cells.length} fait`, model.skipped ? ` · ${model.skipped} manqué${model.skipped > 1 ? "s" : ""}` : "", model.extra ? ` · ${model.extra} en plus` : ""), h("span", { class: "cells", "aria-hidden": "true" }, model.cells.map((c) => h("i", { data: { state: c.state } })))); const doneToday = model.doneToday.length > 0 && h("p", { class: "muted" }, "Déjà fait aujourd'hui : ", model.doneToday.map((s) => s.name).join(", "), "."); const todayBlock = h("section", { class: "block block--today", "aria-label": "Aujourd'hui" }, kicker, ...body, otherActivity(store, today), doneToday, late, progress); return h("div", null, hud(state, today), h("header", { class: "visually-hidden" }, h("h1", null, program.name)), todayBlock, h("h2", { class: "tape" }, "Séances"), h("ul", { class: "rows" }, program.sessions.map((s) => h("li", null, h("a", { class: "row", href: href.session(s.key) }, plaque(s.key, s.color), h("span", { class: "row__main" }, h("span", { class: "row__title" }, s.name), h("span", { class: "row__meta" }, `${plural(s.exercises.length, "exercice")} · ${estimatedMinutes(s)} min · ${last[s.key] ? "faite le " + shortDay(last[s.key]) : "jamais faite"}`)), h("span", { class: "chevron", "aria-hidden": "true" }))))), program.cardio?.length ? [h("h2", { class: "tape" }, "Cardio"), program.cardio.map((c) => note(c.name, c.text))] : null, program.rules?.length ? [h("h2", { class: "tape" }, "Repères"), program.rules.map((r) => note(r.name, r.text))] : null, h("h2", { class: "tape" }, "Tout le reste"), h("nav", { class: "grid-nav", "aria-label": "Autres écrans" }, tile(href.week(), "Semaine", "jours, planning, agenda"), tile(href.history(), "Historique", plural(state.sessions.length, "séance")), tile(href.weight(), "Poids", state.weights.length ? `${String(state.weights[state.weights.length - 1].kg).replace(".", ",")} kg` : "première pesée"), tile(href.library(), "Bibliothèque", "exercices, gestes"), tile(href.analysis(), "Analyses", "charges, assiduité"), tile(href.progress(), "Succès", "niveau, records"), tile(href.activities(), "Activités", plural(program.activities?.length ?? 0, "activité")), tile(href.profile(), "Profil", "objectif, mode"), tile(href.data(), "Données", "sauvegarde, IA"), tile(href.sync(), "Synchro", synced ? "appareils, IA · activée" : "appareils, IA")), h("p", { class: "foot" }, `${program.name} · `, synced ? "synchronisé, chiffré." : "tes données restent sur cet appareil."), primary && actionBar(primary)); } /** @param {string} link @param {string} name @param {string} meta */ function tile(link, name, meta) { return h("a", { class: "tile", href: link }, h("span", { class: "tile__name" }, name), h("span", { class: "tile__meta" }, meta)); } /** * @param {Slot|null} next * @param {import("../../domain/program.js").Program} program */ function nextText(next, program) { if (!next) return "Plus rien de prévu cette semaine."; const l = slotLabel(next, program); return `Ensuite : ${dayName(next.date)}, ${l.title.toLowerCase()}${l.detail ? " · " + l.detail : ""}.`; } /** * « Autre activité » : enregistrer n'importe quelle séance, cardio ou activité, * à une date choisie avant. * @param {Store} store * @param {string} today */ function otherActivity(store, today) { const program = /** @type {import("../../domain/program.js").Program} */ (store.getState().program); const panel = h("div", { class: "other", hidden: true }); const toggle = h("button", { type: "button", class: "btn btn--ghost btn--block", "aria-expanded": "false" }, "Autre activité"); toggle.addEventListener("click", () => { panel.hidden = !panel.hidden; toggle.setAttribute("aria-expanded", String(!panel.hidden)); }); const date = h("input", { id: "other-date", class: "field__input", type: "date", max: today, value: today }); const day = () => (/^\d{4}-\d{2}-\d{2}$/.test(date.value) && date.value <= today ? date.value : today); /** @param {"cardio"|"activity"} kind @param {string} ref @param {string} name */ const log = (kind, ref, name) => { const d = day(); if (store.getState().sessions.some((s) => s.kind === kind && s.ref === ref && s.date === d)) { toast(`${name} est déjà enregistré à cette date.`); return; } if (store.getState().skippedDays.includes(d)) store.dispatch("DayUnskipped", { date: d }); store.dispatch("SessionLogged", { date: d, kind, ref, name, warmup: null, exercises: [] }); toast(`${name} : enregistré le ${shortDay(d)}.`); }; const session = (/** @type {string} */ key) => { location.hash = href.session(key, day() === today ? null : day()); }; panel.append( h("div", { class: "field field--inline" }, h("label", { class: "field__label", for: "other-date" }, "Le"), date), h("div", { class: "choice-list" }, program.sessions.map((s) => h("button", { type: "button", class: "choice", on: { click: () => session(s.key) } }, `Séance ${s.key} · ${s.name}`)), (program.cardio ?? []).map((c) => h("button", { type: "button", class: "choice", on: { click: () => log("cardio", c.name, c.name) } }, `Cardio · ${c.name}`)), (program.activities ?? []).map((a) => h("button", { type: "button", class: "choice", on: { click: () => log("activity", a.id, a.name) } }, a.name)))); return h("div", null, toggle, panel); }