sokko-g/website/src/features/cards/admin/AdminPanel.jsx
Shinuwa 34b412d968 Give Sokko TCG's interface files headers that explain their role, callers and pitfalls
Document the page, API client, offline cache, card renderer, quest pages and
admin dashboard: how stale responses are ignored, where the CSRF token lives,
which requests are idempotent and what the admin checks really protect. Fix
the Broken Mirror headers that still described two levels instead of three,
and the cards README that still described the stale-response bug as open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:56:58 +01:00

121 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file Tableau de bord d'administration de Sokko TCG (`#/cards/admin/...`) : navigation entre les sections, puis
* la section demandée (vue d'ensemble, collections, codes, boutique, Guilde, atelier visuel, historique).
*
* Contexte : chargé à la demande par CardsPage.jsx, seulement pour un administrateur. Il lit le tableau de bord
* (`GET /api/cards/admin/dashboard`) et envoie les modifications par `runMutation` de CardsPage.
* Points d'entrée : `AdminPanel` ; `ADMIN_NAVIGATION` (sections et libellés).
* Utilisé par : CardsPage.jsx.
* Dépend de : useAdminDashboard.js, un fichier par section (Admin*.jsx), catalogLookup.js.
* Attention :
* - Le serveur vérifie les droits de chaque action ; cacher un bouton ne protège rien.
* - Après une modification du catalogue, `onCatalogChange` remplace le catalogue publié de toute l'application.
* - `SECTION_ALIASES` garde les anciennes adresses : ne pas en retirer.
* - Les libellés de l'administration sont écrits dans le JSX, pas dans site.json.
*/
import { useEffect, useState } from "react";
import { CardsAccount, CardsDashboardLayout } from "../CardsDashboard.jsx";
import { formatRelative } from "./adminFormat.js";
import { AdminCollections } from "./AdminCollections.jsx";
import { AdminDistributions } from "./AdminDistributions.jsx";
import { AdminGuild } from "./AdminGuild.jsx";
import { GUILD_QUESTS } from "./guildQuests.js";
import { AdminHistory } from "./AdminHistory.jsx";
import { AdminOverview } from "./AdminOverview.jsx";
import { AdminShop } from "./AdminShop.jsx";
import { AdminVisualLab } from "./AdminVisualLab.jsx";
import { useCatalogLookup } from "./catalogLookup.js";
import { useAdminDashboard } from "./useAdminDashboard.js";
// Les sections qui agissent sur le jeu, l'Atelier visuel à part (un bac à sable où rien n'est enregistré), puis le journal de ces actions.
const ADMIN_NAVIGATION = [
[["overview", "Vue d’ensemble"], ["collections", "Collections"], ["codes", "Codes cadeaux"], ["shop", "Boutique"], ["guild", "Guilde"]],
[["visual-lab", "Atelier visuel"]],
[["history", "Historique"]]
];
const SECTION_IDS = new Set(ADMIN_NAVIGATION.flat().map(([id]) => id));
// Anciennes adresses conservées pour les favoris existants.
const SECTION_ALIASES = { players: "overview", cards: "collections", catalog: "collections", packs: "collections", distributions: "codes", "mythic-styles": "visual-lab", "opening-effects": "visual-lab" };
function resolveAdminSection(section) {
const resolved = SECTION_ALIASES[section] || section;
return SECTION_IDS.has(resolved) ? resolved : "overview";
}
function useNow(intervalMs) {
const [now, setNow] = useState(() => Date.now());
useEffect(() => {
const timer = window.setInterval(() => setNow(Date.now()), intervalMs);
return () => window.clearInterval(timer);
}, [intervalMs]);
return now;
}
export function AdminPanel({ catalog, state, runMutation, busy, section, subsection = "", onCatalogChange, backLabel = "Retour au jeu", onLogout = null }) {
const activeSection = resolveAdminSection(section);
const lookup = useCatalogLookup(catalog);
const { dashboard, loading, error, refresh } = useAdminDashboard();
const now = useNow(30_000);
async function mutateCatalog(action) {
const result = await runMutation(action);
if (result?.catalog) {
onCatalogChange?.(result.catalog);
void refresh();
}
return result;
}
// Les campagnes vivent dans les données du tableau de bord : on les recharge après chaque modification.
async function mutateCampaigns(action) {
const result = await runMutation(action);
if (result) void refresh();
return result;
}
// Un enregistrement recharge les données, qui recréent le formulaire du réglage : ses champs restent verrouillés jusque-là,
// sinon une saisie commencée entre les deux serait effacée.
const settingsBusy = busy || loading;
// Dans la Guilde, le menu déplie ses quêtes.
const navigation = activeSection === "guild"
? ADMIN_NAVIGATION.map((group) => group.map(([id, label]) => (id === "guild"
? [id, label, 0, GUILD_QUESTS.map((quest) => ({ id: quest.id, label: quest.label, href: `#/cards/admin/guild/${quest.id}`, current: subsection === quest.id }))]
: [id, label])))
: ADMIN_NAVIGATION;
const shared = { catalog, lookup, state, dashboard, dashboardLoading: loading, refreshDashboard: refresh, runMutation, busy, settingsBusy, now };
return <section className="nebula-dashboard" aria-labelledby="nebula-admin-title">
<header className="nds-panel nebula-admin-heading">
<div className="nebula-admin-title">
<span className="nebula-admin-title-icon" aria-hidden="true"><span className="ui-icon nav-icon-cards" /></span>
<div>
<p className="eyebrow">Sokko TCG · Administration</p>
<h1 id="nebula-admin-title">Tableau de bord Sokko TCG</h1>
<p className="nebula-admin-refresh">
<span role="status">{loading ? "Actualisation…" : dashboard?.generatedAt ? `Données mises à jour ${formatRelative(dashboard.generatedAt, Math.max(now, dashboard.generatedAt))}` : "Chargement des données…"}</span>
<button className="nds-button" data-size="xs" data-variant="secondary" type="button" disabled={loading} onClick={refresh}>Actualiser</button>
</p>
</div>
</div>
<CardsAccount player={state.player} onLogout={onLogout} />
</header>
{error && <p className="nds-inline-notice" data-variant="danger" role="alert">{error}</p>}
<CardsDashboardLayout
label="Sections de l’administration"
groups={navigation}
active={activeSection}
hrefFor={(id) => `#/cards/admin/${id}`}
switchLink={{ href: "#/cards", label: backLabel }}
busy={loading}
>
{activeSection === "overview" && <AdminOverview {...shared} />}
{activeSection === "collections" && <AdminCollections {...shared} mutateCatalog={mutateCatalog} collectionId={subsection} />}
{activeSection === "visual-lab" && <AdminVisualLab catalog={catalog} lookup={lookup} />}
{activeSection === "history" && <AdminHistory catalog={catalog} refreshedAt={dashboard?.generatedAt} />}
{activeSection === "codes" && <AdminDistributions {...shared} mutateCampaigns={mutateCampaigns} />}
{activeSection === "shop" && <AdminShop {...shared} busy={settingsBusy} />}
{activeSection === "guild" && <AdminGuild {...shared} busy={settingsBusy} tab={subsection} />}
</CardsDashboardLayout>
</section>;
}