122 lines
6.6 KiB
JavaScript
122 lines
6.6 KiB
JavaScript
// Profil (ProfileUpdated) et mode (ModeChanged) : ce que l'IA doit savoir pour
|
|
// construire et faire évoluer le programme. Plus le thème, simple préférence
|
|
// d'affichage de l'appareil (pas un événement, ADR 0006 point 3).
|
|
|
|
import { h } from "../dom.js";
|
|
import { MODES } from "../../domain/vocabulary.js";
|
|
import { validateProfile } from "../../domain/profile.js";
|
|
import { screenHeader } from "../layout.js";
|
|
import { href } from "../router.js";
|
|
import { toast } from "../feedback.js";
|
|
import { getTheme, setTheme } from "../theme-pref.js";
|
|
|
|
/** @type {Record<string, {label: string, text: string}>} */
|
|
export const MODE_LABELS = {
|
|
progression: { label: "Progression", text: "Les charges montent dès que toutes les séries atteignent le haut de la fourchette, sans « dur » ni « échec »." },
|
|
maintenance: { label: "Maintien", text: "Garder le niveau : les charges restent stables." },
|
|
recovery: { label: "Récupération", text: "Reprise ou fatigue : rien ne monte, l'IA allège." },
|
|
};
|
|
|
|
/**
|
|
* Lit le formulaire. Les listes s'écrivent une ligne par élément.
|
|
* @param {{goal: string, context: string, equipment: string, constraints: string, heightCm: string, sessionMinutes: string}} form
|
|
* @returns {{profile: import("../../domain/profile.js").Profile, errors: string[]}}
|
|
*/
|
|
export function profileFromForm(form) {
|
|
const lines = (/** @type {string} */ t) => t.split(/\r?\n/).map((l) => l.replace(/^\s*[-•*]\s*/, "").trim()).filter(Boolean);
|
|
// Un nombre illisible devient NaN, que validateProfile refuse.
|
|
const height = form.heightCm.trim() === "" ? null : Math.round(Number(form.heightCm.replace(",", ".")));
|
|
const profile = {
|
|
goal: form.goal.trim(),
|
|
context: form.context.trim(),
|
|
equipment: lines(form.equipment),
|
|
constraints: lines(form.constraints),
|
|
heightCm: height,
|
|
sessionMinutes: Math.round(Number(form.sessionMinutes.replace(",", "."))),
|
|
};
|
|
/** @type {Record<string, string>} */
|
|
const plain = {
|
|
heightCm: "Taille : un nombre de centimètres entre 50 et 260, ou vide.",
|
|
sessionMinutes: "Durée d'une séance : de 10 à 240 minutes.",
|
|
};
|
|
const errors = validateProfile(profile).map((e) => Object.entries(plain).find(([k]) => e.includes(`'${k}'`))?.[1] ?? e);
|
|
return { profile, errors };
|
|
}
|
|
|
|
/**
|
|
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation}} ctx
|
|
* @returns {import("../router.js").Screen}
|
|
*/
|
|
export function profileScreen({ store, nav }) {
|
|
const p = store.getState().profile;
|
|
/** @param {string} id @param {string} label @param {HTMLElement} input @param {string} [hint] */
|
|
const field = (id, label, input, hint) => h("div", { class: "field" },
|
|
h("label", { class: "field__label", for: id }, label), input, hint && h("p", { class: "field__hint" }, hint));
|
|
const area = (/** @type {string} */ id, /** @type {string} */ value) => h("textarea", { id, class: "field__input field__input--area", value });
|
|
const goal = area("p-goal", p.goal);
|
|
const context = area("p-context", p.context);
|
|
const equipment = area("p-equipment", p.equipment.join("\n"));
|
|
const constraints = area("p-constraints", p.constraints.join("\n"));
|
|
const height = h("input", { id: "p-height", class: "field__input", type: "text", inputmode: "numeric", value: p.heightCm ?? "" });
|
|
const minutes = h("input", { id: "p-minutes", class: "field__input", type: "text", inputmode: "numeric", value: String(p.sessionMinutes) });
|
|
const status = h("p", { class: "status", role: "status", "aria-live": "polite" });
|
|
|
|
const save = () => {
|
|
const { profile, errors } = profileFromForm({
|
|
goal: goal.value, context: context.value, equipment: equipment.value, constraints: constraints.value,
|
|
heightCm: height.value, sessionMinutes: minutes.value,
|
|
});
|
|
if (errors.length) {
|
|
status.className = "status status--err";
|
|
status.textContent = errors.join(" ");
|
|
return;
|
|
}
|
|
store.dispatch("ProfileUpdated", { profile });
|
|
status.className = "status status--ok";
|
|
status.textContent = "Profil enregistré.";
|
|
};
|
|
|
|
// Le mode change d'un geste : seul l'état des boutons est mis à jour.
|
|
const modeText = h("p", { class: "muted" });
|
|
const modeBtns = MODES.map((m) => h("button", { type: "button", class: "segmented__opt", data: { mode: m } }, MODE_LABELS[m].label));
|
|
const paintMode = () => {
|
|
const mode = store.getState().mode;
|
|
modeBtns.forEach((b) => b.setAttribute("aria-pressed", String(b.dataset.mode === mode)));
|
|
modeText.textContent = MODE_LABELS[mode].text;
|
|
};
|
|
modeBtns.forEach((b) => b.addEventListener("click", () => {
|
|
const mode = /** @type {import("../../domain/vocabulary.js").Mode} */ (b.dataset.mode);
|
|
if (mode === store.getState().mode) return;
|
|
store.dispatch("ModeChanged", { mode });
|
|
paintMode();
|
|
toast(`Mode : ${MODE_LABELS[mode].label.toLowerCase()}.`);
|
|
}));
|
|
paintMode();
|
|
|
|
const themes = /** @type {const} */ ([["auto", "Système"], ["dark", "Sombre"], ["light", "Clair"]]);
|
|
const themeBtns = themes.map(([value, label]) => h("button", { type: "button", class: "segmented__opt", data: { theme: value } }, label));
|
|
const paintTheme = () => themeBtns.forEach((b) => b.setAttribute("aria-pressed", String(b.dataset.theme === getTheme())));
|
|
themeBtns.forEach((b) => b.addEventListener("click", () => { setTheme(/** @type {any} */ (b.dataset.theme)); paintTheme(); }));
|
|
paintTheme();
|
|
|
|
const node = h("div", { class: "screen" },
|
|
screenHeader({ back: { label: "Accueil", onClick: () => nav.back(href.home()) }, kicker: "Pour ton IA", title: "Profil", sub: "Ton IA lit ce profil pour construire et ajuster ton programme." }),
|
|
h("h2", { class: "tape" }, "Mode"),
|
|
h("div", { class: "segmented", role: "group", "aria-label": "Mode du programme" }, modeBtns),
|
|
modeText,
|
|
h("h2", { class: "tape" }, "Toi"),
|
|
field("p-goal", "Objectif", goal, "En une phrase."),
|
|
field("p-context", "Contexte", context, "Pratique passée, lieu d'entraînement."),
|
|
field("p-equipment", "Matériel", equipment, "Un élément par ligne."),
|
|
field("p-constraints", "Contraintes", constraints, "Sécurité, blessures, organisation. Une par ligne."),
|
|
h("div", { class: "field-pair" },
|
|
field("p-height", "Taille (cm)", height),
|
|
field("p-minutes", "Séance (min)", minutes)),
|
|
status,
|
|
h("button", { type: "button", class: "btn btn--primary btn--block", on: { click: save } }, "Enregistrer le profil"),
|
|
h("h2", { class: "tape" }, "Affichage"),
|
|
h("div", { class: "segmented", role: "group", "aria-label": "Thème" }, themeBtns),
|
|
h("p", { class: "muted" }, "Sombre par défaut : un écran OLED noir consomme moins."));
|
|
return { node, title: "Profil" };
|
|
}
|