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>
202 lines
8.1 KiB
JavaScript
202 lines
8.1 KiB
JavaScript
/**
|
|
* @file Affiche une toolbox dans la liste, avec actions rapides et icône personnalisable.
|
|
*/
|
|
|
|
/* eslint-disable react-refresh/only-export-components -- expose aussi les helpers publics de rendu des cartes toolbox. */
|
|
import { CompactDropdown } from "../../components/CompactDropdown.jsx";
|
|
import { Icon } from "../../components/Icon.jsx";
|
|
import { ToolboxCoverEditor } from "./ToolboxCoverEditor.jsx";
|
|
import {
|
|
DEFAULT_TOOLBOX_ICON,
|
|
normalizeToolboxIcon,
|
|
TOOLBOX_ICONS
|
|
} from "./storage/toolboxStorage.js";
|
|
import { useRef, useState } from "react";
|
|
|
|
export function getGameCardCover(game) {
|
|
return game?.images?.cardCover || game?.image || "";
|
|
}
|
|
|
|
export function getGameCardBackground(game) {
|
|
return game?.cover || "var(--gradient-nebula)";
|
|
}
|
|
|
|
export function formatDate(value) {
|
|
return new Intl.DateTimeFormat("fr-FR", { dateStyle: "medium", timeStyle: "short" }).format(new Date(value));
|
|
}
|
|
|
|
/**
|
|
* Couverture d'une toolbox : celle du jeu lié, sinon l'image choisie, sinon son icône. `rect` distingue une image (bandeau)
|
|
* d'une icône (à centrer sur le dégradé).
|
|
*/
|
|
export function getToolboxCover(toolbox, game) {
|
|
const gameCoverImage = getGameCardCover(game);
|
|
const customCoverImage = !gameCoverImage && toolbox.customCover ? toolbox.customCover : "";
|
|
return {
|
|
src: gameCoverImage || customCoverImage || toolbox.icon || DEFAULT_TOOLBOX_ICON,
|
|
rect: Boolean(gameCoverImage || customCoverImage),
|
|
custom: Boolean(customCoverImage)
|
|
};
|
|
}
|
|
|
|
export function ToolboxCard({ toolbox, game, actions, reorder }) {
|
|
const cover = getToolboxCover(toolbox, game);
|
|
const customCoverImage = cover.custom ? cover.src : "";
|
|
const coverImage = cover.src;
|
|
const hasRectCover = cover.rect;
|
|
const className = [
|
|
"card",
|
|
"nds-card",
|
|
"toolbox-card",
|
|
reorder.isItemDragging(toolbox.id) ? "is-dragging" : "",
|
|
reorder.isItemDropTarget(toolbox.id) ? "is-drop-target" : "",
|
|
reorder.getDropPlacement("item", toolbox.id) === "after" ? "drop-after" : ""
|
|
].filter(Boolean).join(" ");
|
|
|
|
return (
|
|
<article className={className} data-variant="interactive" {...reorder.getItemProps({ itemId: toolbox.id })}>
|
|
<button
|
|
className="toolbox-card-drag-handle nds-icon-button"
|
|
data-size="md"
|
|
data-variant="secondary"
|
|
type="button"
|
|
onPointerDown={(event) => reorder.itemReorder.startDrag(event, toolbox.id)}
|
|
aria-label={`Déplacer ${toolbox.name}`}
|
|
title="Déplacer"
|
|
>
|
|
<Icon name="drag" />
|
|
</button>
|
|
<a
|
|
className={`card-cover nds-card-cover toolbox-card-cover-link ${hasRectCover ? "toolbox-card-cover" : "toolbox-icon-cover"}`}
|
|
href={`#/toolbox/${toolbox.id}`}
|
|
aria-label={`Ouvrir ${toolbox.name}`}
|
|
title={`Ouvrir ${toolbox.name}`}
|
|
style={{ "--game-cover": getGameCardBackground(game), background: hasRectCover ? "" : "var(--gradient-nebula)" }}
|
|
>
|
|
<img src={coverImage} alt={game ? game.title : customCoverImage ? `Image de ${toolbox.name}` : `Icône de ${toolbox.name}`} loading="lazy" />
|
|
</a>
|
|
<div className="card-body nds-card-body">
|
|
<p className="eyebrow">{game ? game.title : "Toolbox libre"}</p>
|
|
<h2>{toolbox.name}</h2>
|
|
<small>Modifiée le {formatDate(toolbox.updatedAt)}</small>
|
|
<div className="card-actions nds-card-actions nds-toolbar">
|
|
<a className="card-icon-button nds-icon-button" data-size="md" href={`#/toolbox/${toolbox.id}`} aria-label={`Ouvrir ${toolbox.name}`} title="Ouvrir"><Icon name="open" /></a>
|
|
<button className="card-icon-button nds-icon-button" data-size="md" onClick={() => actions.exportToolbox(toolbox.id)} aria-label={`Exporter ${toolbox.name}`} title="Exporter"><Icon name="export" /></button>
|
|
{actions.sharingEnabled && <button className="card-icon-button nds-icon-button" data-size="md" onClick={() => actions.openShareToolbox(toolbox.id)} aria-label={`Envoyer ${toolbox.name}`} title="Envoyer"><Icon name="share" /></button>}
|
|
<button className="card-icon-button nds-icon-button" data-size="md" data-variant="danger" onClick={() => actions.setConfirmModal({
|
|
title: "Supprimer la toolbox",
|
|
message: `Supprimer "${toolbox.name}" et ses données locales ?`,
|
|
confirmLabel: "Supprimer",
|
|
danger: true,
|
|
onResolve: (confirmed) => confirmed && actions.deleteToolbox(toolbox.id)
|
|
})} aria-label={`Supprimer ${toolbox.name}`} title="Supprimer"><Icon name="trash" /></button>
|
|
</div>
|
|
</div>
|
|
</article>
|
|
);
|
|
}
|
|
|
|
export function ToolboxIconPicker({ toolbox, onChange, tutorialTarget = "" }) {
|
|
const icon = normalizeToolboxIcon(toolbox.icon);
|
|
const triggerImage = toolbox.customCover || icon;
|
|
const fileInputRef = useRef(null);
|
|
const [coverEditor, setCoverEditor] = useState(null);
|
|
|
|
function closeCoverEditor() {
|
|
setCoverEditor(null);
|
|
if (fileInputRef.current) fileInputRef.current.value = "";
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<input
|
|
ref={fileInputRef}
|
|
type="file"
|
|
accept="image/*"
|
|
hidden
|
|
onChange={(event) => {
|
|
const file = event.target.files?.[0];
|
|
if (!file?.type?.startsWith("image/")) return;
|
|
setCoverEditor({ file });
|
|
}}
|
|
/>
|
|
<CompactDropdown
|
|
className="toolbox-icon-picker"
|
|
menuClassName="toolbox-icon-picker-menu"
|
|
label="Icônes de toolbox"
|
|
renderTrigger={({ open, toggle }) => (
|
|
<button
|
|
className={`toolbox-icon-picker-button nds-large-icon-picker-trigger ${toolbox.customCover ? "has-custom-cover" : ""} ${open ? "active" : ""}`}
|
|
type="button"
|
|
onClick={toggle}
|
|
aria-label="Changer l'image de la toolbox"
|
|
aria-expanded={open}
|
|
aria-pressed={open}
|
|
data-toolbox-tutorial-target={tutorialTarget || undefined}
|
|
title="Changer l'image"
|
|
>
|
|
<img src={triggerImage} alt="" aria-hidden="true" />
|
|
</button>
|
|
)}
|
|
>
|
|
{({ close }) => (
|
|
<>
|
|
{TOOLBOX_ICONS.map((path) => (
|
|
<button
|
|
key={path}
|
|
className={`nds-icon-button ${!toolbox.customCover && path === icon ? "active" : ""}`}
|
|
data-size="md"
|
|
type="button"
|
|
onClick={() => {
|
|
onChange(path, "");
|
|
close();
|
|
}}
|
|
aria-label={`Utiliser l'icône ${path.split("/").pop().replace(/\.[^.]+$/, "")}`}
|
|
aria-pressed={!toolbox.customCover && path === icon}
|
|
title={path.split("/").pop().replace(/\.[^.]+$/, "")}
|
|
>
|
|
<img src={path} alt="" aria-hidden="true" loading="lazy" />
|
|
</button>
|
|
))}
|
|
<button
|
|
className={`toolbox-cover-menu-action nds-button ${toolbox.customCover ? "active" : ""}`}
|
|
data-size="sm"
|
|
data-variant="secondary"
|
|
type="button"
|
|
onClick={() => {
|
|
fileInputRef.current?.click();
|
|
close();
|
|
}}
|
|
aria-label={toolbox.customCover ? "Remplacer l'image de card" : "Ajouter une image de card"}
|
|
aria-pressed={Boolean(toolbox.customCover)}
|
|
title={toolbox.customCover ? "Remplacer l'image de card" : "Ajouter une image de card"}
|
|
>
|
|
<Icon name="picture" />
|
|
<span>Image personnalisée</span>
|
|
</button>
|
|
</>
|
|
)}
|
|
</CompactDropdown>
|
|
{coverEditor && (
|
|
<ToolboxCoverEditor
|
|
file={coverEditor.file}
|
|
existingCover={coverEditor.existingCover || toolbox.customCover || ""}
|
|
onClose={closeCoverEditor}
|
|
onSave={(customCover) => {
|
|
onChange(icon, customCover);
|
|
closeCoverEditor();
|
|
}}
|
|
/>
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function ToolboxGameIcon({ game }) {
|
|
const gameCoverImage = getGameCardCover(game);
|
|
return (
|
|
<div className="toolbox-game-icon" title={game.title} aria-label={`Jeu associé : ${game.title}`}>
|
|
<img src={gameCoverImage} alt="" aria-hidden="true" />
|
|
</div>
|
|
);
|
|
}
|