Sharing a toolbox meant exporting a JSON file and passing it around by hand. A toolbox can now be sent from the site while staying local first: the browser compresses and encrypts the v2 export with AES-GCM, and the key lives only in the link fragment, so the server keeps for at most 24 hours a copy it cannot read. - Add the /api/shares service: upload, metadata, download and revoke, with files in SHARES_DIR, a purge every 10 minutes, and single-use links (the default) deleted only once fully downloaded. - Cap the server load: 50 MiB per share, total storage, free disk space, daily volume, concurrent and stalled uploads, reads per link, plus a wide per-connection cap kept in memory without storing IPs. - Limit each browser to 2 shares and 100 MiB per day and 2 active links, and show the existing link of a toolbox already shared. - Add the send dialog (card and game drawer) and the /receive page, which previews or directly imports through the existing import path. - Move shared HTTP helpers to services/http.js for both APIs. - Document the format, delivery, server layout and privacy wording. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
185 lines
7.6 KiB
JavaScript
185 lines
7.6 KiB
JavaScript
// Rôle : 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));
|
|
}
|
|
|
|
export function ToolboxCard({ toolbox, game, actions, reorder }) {
|
|
const gameCoverImage = getGameCardCover(game);
|
|
const customCoverImage = !gameCoverImage && toolbox.customCover ? toolbox.customCover : "";
|
|
const coverImage = gameCoverImage || customCoverImage || toolbox.icon || DEFAULT_TOOLBOX_ICON;
|
|
const hasRectCover = Boolean(gameCoverImage || customCoverImage);
|
|
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="send" /></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>
|
|
);
|
|
}
|