sokko-g/website/src/features/cards/team/RacePanel.jsx
Shinuwa 166790245f Turn the comments that describe functions into JSDoc so the editor shows them on hover
The 669 // comments sitting right above a function, method or hook become
/** */ blocks, unchanged in wording; eslint directives stay right above the
line they target. website/AGENTS.md now asks for JSDoc on function comments
and // inside function bodies. ExpeditionPlanner.jsx grew past 300 lines and
gets its full header, as the header test requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 11:51:47 +01:00

117 lines
5.9 KiB
JavaScript

/**
* @file Page de la Course des courants : choix du coureur, course en direct, course interrompue et résultat.
*/
import { useEffect, useRef, useState } from "react";
import { finishRace, loadTeam, recordRaceBoosts, resetRace, startRace } from "../cardsApi.js";
import { RaceLive } from "./RaceLive.jsx";
import { RaceResult } from "./RaceResult.jsx";
import { RaceCircuit } from "./RaceTrack.jsx";
import { TeamCompanion } from "./TeamCompanion.jsx";
export function RacePanel({ catalog, runMutation, busy, content, teamContent, statLabels }) {
const [team, setTeam] = useState(null);
const [loadError, setLoadError] = useState("");
const [reloadKey, setReloadKey] = useState(0);
const [notice, setNotice] = useState("");
// Course lancée sur cette page : elle s'anime, puis laisse la place au résultat.
const [live, setLive] = useState(null);
const [finished, setFinished] = useState(null);
const titleRef = useRef(null);
useEffect(() => {
let active = true;
loadTeam()
.then((result) => { if (active) { setTeam(result.team); setLoadError(""); } })
.catch((error) => { if (active) setLoadError(error.message); });
return () => { active = false; };
}, [reloadKey]);
const showResult = !live && team?.race?.status === "finished";
const wasLive = useRef(false);
useEffect(() => {
if (live) wasLive.current = true;
else if (wasLive.current && showResult) titleRef.current?.focus();
}, [live, showResult]);
if (loadError) return <section className="nds-empty-state" role="alert"><h2>{content.title}</h2><p>{loadError}</p><button className="nds-button" data-variant="secondary" type="button" onClick={() => setReloadKey((key) => key + 1)}>{teamContent.retry}</button></section>;
if (!team) return <section className="nds-empty-state" aria-busy="true"><h2>{content.title}</h2></section>;
/** Les refus propres à la course restent dans ce panneau ; runMutation garde le reste (erreurs réseau, solde). */
async function play(action) {
setNotice("");
const result = await runMutation(async () => {
try {
return await action();
} catch (error) {
if (/^(RACE|TEAM)_/.test(String(error.code || ""))) return { raceError: error };
throw error;
}
});
if (result?.raceError) {
setNotice(result.raceError.message);
setLive(null);
setReloadKey((key) => key + 1);
}
return result?.team || null;
}
async function start(cardId) {
const next = await play(() => startRace(team.day, cardId));
if (!next?.race) return;
setTeam(next);
setFinished(null);
setLive(next.race);
}
/** L'arrivée du joueur est envoyée aussitôt ; le résultat s'affiche quand tous les coureurs sont arrivés à l'écran. */
async function finish(boosts) {
const next = await play(() => finishRace(boosts));
if (next) setFinished(next);
return next;
}
async function over() {
const next = finished || await finish(null);
if (next) setTeam(next);
setLive(null);
}
/** Développement : la course du jour s'efface, l'équipe reste. */
async function rerun() {
const next = await play(() => resetRace());
if (!next) return;
setLive(null);
setFinished(null);
setTeam(next);
}
async function finishInterrupted() {
const next = await finish(null);
if (next) setTeam(next);
}
const { race } = team;
const interrupted = !live && race?.status === "running";
const choosing = !live && !race && team.status === "ready" && team.raceEnabled;
return <section className="nebula-race" aria-labelledby="nebula-race-title">
<div className="nds-section-heading nebula-section-heading">
<div><p className="eyebrow">{content.eyebrow}</p><h2 id="nebula-race-title">{content.title}</h2></div>
<div className="nebula-team-heading-actions">{team.devReset && <button className="nds-button" data-size="sm" data-variant="danger" type="button" disabled={busy} onClick={rerun}>{content.devReset}</button>}<a className="nds-button" data-size="sm" data-variant="secondary" href="#/cards/guild">{content.back}</a></div>
</div>
{notice && <p className="nds-inline-notice" data-variant="warning" role="status">{notice}</p>}
{live && <RaceLive key={live.day} catalog={catalog} race={live} companions={team.companions} content={content} teamContent={teamContent} statLabels={statLabels}
onBoost={(boosts) => { recordRaceBoosts(boosts).catch(() => {}); }} onFinish={finish} onOver={over} />}
{interrupted && <div className="nds-panel nebula-race-interrupted"><h3>{content.interruptedTitle}</h3><p>{content.interruptedText}</p><button className="nds-button" data-variant="primary" type="button" disabled={busy} onClick={finishInterrupted}>{content.interruptedAction}</button></div>}
{showResult && <RaceResult catalog={catalog} race={race} nextAt={team.nextAt} content={content} teamContent={teamContent} statLabels={statLabels} titleRef={titleRef} />}
{!live && !race && !choosing && <div className="nds-empty-state"><p>{!team.enabled ? teamContent.closed : !team.raceEnabled ? content.closed : content.needTeam}</p>{team.raceEnabled && <a className="nds-button" data-variant="primary" href="#/cards/team">{content.recruitFirst}</a>}</div>}
{choosing && <>
<RaceCircuit zones={team.zones} content={content} statLabels={statLabels} />
<section className="nebula-race-choice" aria-labelledby="nebula-race-choice-title">
<h3 id="nebula-race-choice-title">{content.chooseTitle}</h3>
<p className="nebula-race-intro">{content.intro}</p>
<ul className="nebula-team-list">{team.companions.map((companion) => <li key={companion.cardId}><TeamCompanion catalog={catalog} companion={companion} content={{ ...teamContent, recruitLabel: content.chooseLabel }} statLabels={statLabels} disabled={busy} actionLabel={content.chooseAction} onSelect={() => start(companion.cardId)} /></li>)}</ul>
</section>
</>}
</section>;
}