Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md (setup and step-by-step recipes) and a README in each main folder, with an index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to JSDoc @file blocks, define a three-level header template in website/AGENTS.md and check the header in tests/file-headers.test.mjs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
4.9 KiB
JavaScript
90 lines
4.9 KiB
JavaScript
/**
|
||
* @file Fournit le cadre commun des tableaux de bord joueur et administration (menu de sections et encart
|
||
* associé, compte, chiffres clés).
|
||
*/
|
||
|
||
import { createContext, Fragment, useContext, useEffect, useId, useRef, useState } from "react";
|
||
import { createPortal } from "react-dom";
|
||
|
||
const AsideSlotContext = createContext(null);
|
||
|
||
// Menu de sections : colonne toujours ouverte dans le tableau de bord large ; en demi-écran, un bouton
|
||
// qui affiche la section courante ouvre la liste, refermée au choix d'une entrée, par Échap ou par un clic ailleurs.
|
||
// Une entrée [id, libellé, compteur, sous-entrées] peut porter des sous-entrées { id, label, href, current }, en retrait sous elle.
|
||
function DashboardMenu({ label, groups, active, hrefFor }) {
|
||
const [open, setOpen] = useState(false);
|
||
const menuRef = useRef(null);
|
||
const toggleRef = useRef(null);
|
||
const navId = useId();
|
||
const items = groups.flat();
|
||
const activeItem = items.find(([id]) => id === active);
|
||
const activeChild = activeItem?.[3]?.find((child) => child.current);
|
||
const activeLabel = activeItem ? (activeChild ? `${activeItem[1]} › ${activeChild.label}` : activeItem[1]) : label;
|
||
const pending = items.reduce((total, [, , count]) => total + (count || 0), 0);
|
||
|
||
useEffect(() => {
|
||
if (!open) return undefined;
|
||
const closeOutside = (event) => { if (!menuRef.current?.contains(event.target)) setOpen(false); };
|
||
document.addEventListener("pointerdown", closeOutside);
|
||
return () => document.removeEventListener("pointerdown", closeOutside);
|
||
}, [open]);
|
||
|
||
const closeOnEscape = (event) => {
|
||
if (event.key !== "Escape" || !open) return;
|
||
setOpen(false);
|
||
toggleRef.current?.focus();
|
||
};
|
||
|
||
return <div ref={menuRef} className="nebula-dashboard-menu" onKeyDown={closeOnEscape}>
|
||
<button ref={toggleRef} className="nebula-dashboard-menu-toggle" type="button" aria-expanded={open} aria-controls={navId} onClick={() => setOpen((value) => !value)}>
|
||
<span className="nebula-dashboard-menu-icon" aria-hidden="true" />
|
||
<span className="nebula-dashboard-menu-current"><span className="sr-only">{label} : </span>{activeLabel}</span>
|
||
{pending > 0 && !open && <span className="nds-badge" data-variant="gold">{pending}</span>}
|
||
</button>
|
||
<nav id={navId} className="nebula-dashboard-nav" aria-label={label} data-open={open || undefined} onClick={(event) => { if (event.target.closest("a")) setOpen(false); }}>
|
||
{groups.map((group) => <div key={group[0][0]} className="nebula-dashboard-nav-group">
|
||
{group.map(([id, itemLabel, count, children]) => <Fragment key={id}>
|
||
<a className="nebula-dashboard-nav-item" href={hrefFor(id)} aria-current={active === id && !activeChild ? "page" : undefined} data-expanded={children?.length ? true : undefined}>{itemLabel}{count > 0 && <span className="nds-badge" data-variant="gold">{count}</span>}</a>
|
||
{children?.length > 0 && <div className="nebula-dashboard-nav-sub">
|
||
{children.map((child) => <a key={child.id} className="nebula-dashboard-nav-item" data-level="sub" href={child.href} aria-current={child.current ? "page" : undefined}>{child.label}</a>)}
|
||
</div>}
|
||
</Fragment>)}
|
||
</div>)}
|
||
</nav>
|
||
</div>;
|
||
}
|
||
|
||
export function CardsDashboardLayout({ label, groups, active, hrefFor, switchLink = null, busy = false, children }) {
|
||
const [asideSlot, setAsideSlot] = useState(null);
|
||
return <div className="nebula-dashboard-shell">
|
||
<div className="nebula-dashboard-aside">
|
||
{switchLink && <a className="nds-button" data-size="sm" data-variant="secondary" href={switchLink.href}>{switchLink.label}</a>}
|
||
<DashboardMenu label={label} groups={groups} active={active} hrefFor={hrefFor} />
|
||
<div ref={setAsideSlot} className="nebula-dashboard-aside-slot" />
|
||
</div>
|
||
<div className="nebula-dashboard-section" data-section={active} aria-busy={busy || undefined}>
|
||
<AsideSlotContext.Provider value={asideSlot}>{children}</AsideSlotContext.Provider>
|
||
</div>
|
||
</div>;
|
||
}
|
||
|
||
// Place un encart propre à la section sous le menu, tout en gardant son état dans le panneau qui le déclare.
|
||
export function DashboardAside({ children }) {
|
||
const slot = useContext(AsideSlotContext);
|
||
return slot ? createPortal(children, slot) : null;
|
||
}
|
||
|
||
export function CardsAccount({ player, onLogout }) {
|
||
return <div className="nebula-dashboard-account">
|
||
<span className="nebula-dashboard-account-player">{player.avatarUrl ? <img src={player.avatarUrl} alt="" referrerPolicy="no-referrer" /> : <i aria-hidden="true">✦</i>}<span>{player.displayName}</span></span>
|
||
{onLogout && <button className="nds-button" data-size="sm" data-variant="secondary" type="button" onClick={onLogout}>Déconnexion</button>}
|
||
</div>;
|
||
}
|
||
|
||
export function DashboardStat({ value, label, detail }) {
|
||
return <article className="nds-panel nebula-dashboard-stat">
|
||
<span className="nebula-dashboard-stat-label">{label}</span>
|
||
<strong>{value}</strong>
|
||
{detail && <small>{detail}</small>}
|
||
</article>;
|
||
}
|