173 lines
6.7 KiB
JavaScript
173 lines
6.7 KiB
JavaScript
// Rôle : copie ou crée en checklist les données visibles des pages de jeu.
|
|
/* eslint-disable react-refresh/only-export-components -- expose aussi les formatters publics testés pour l'import checklist. */
|
|
import { useId, useState } from "react";
|
|
import { Icon } from "../../components/Icon.jsx";
|
|
import { Modal } from "../../components/Modal.jsx";
|
|
|
|
async function copyText(value) {
|
|
try {
|
|
await navigator.clipboard.writeText(value);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function notify(message) {
|
|
window.dispatchEvent(new CustomEvent("sokkog:notify", { detail: { message } }));
|
|
}
|
|
|
|
export function formatChecklistImportItems(labels) {
|
|
return labels
|
|
.map((entry) => {
|
|
if (typeof entry === "object" && entry !== null) {
|
|
const label = String(entry.label || entry.name || "").trim();
|
|
const quantity = Number.parseInt(entry.quantity ?? entry.quantite ?? 1, 10);
|
|
return label ? { label, quantity: Number.isFinite(quantity) ? quantity : 1 } : null;
|
|
}
|
|
const label = String(entry || "").trim();
|
|
return label ? { label, quantity: 1 } : null;
|
|
})
|
|
.filter(Boolean)
|
|
.map((entry) => `${entry.label}:${entry.quantity}`)
|
|
.join("\n");
|
|
}
|
|
|
|
export function formatChecklistImportSections(sections = []) {
|
|
return sections
|
|
.map((section) => {
|
|
const title = String(section?.title || "").trim();
|
|
const items = formatChecklistImportItems(section?.items || []);
|
|
if (!items) return "";
|
|
return title ? `# ${title}\n${items}` : items;
|
|
})
|
|
.filter(Boolean)
|
|
.join("\n\n");
|
|
}
|
|
|
|
function ChecklistResultsModal({ checklistTitle, content, linkedToolbox, text, onCreate, onClose }) {
|
|
const titleId = useId();
|
|
const descriptionId = useId();
|
|
const [creating, setCreating] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
async function copyItems() {
|
|
setError("");
|
|
if (!await copyText(text)) {
|
|
setError(content.resultsCopyError || "Impossible de copier les résultats dans le presse-papiers.");
|
|
return;
|
|
}
|
|
notify(content.resultsCopied || "Copié dans le presse-papiers.");
|
|
onClose();
|
|
}
|
|
|
|
async function createChecklist() {
|
|
if (!linkedToolbox || creating) return;
|
|
setCreating(true);
|
|
setError("");
|
|
try {
|
|
if (await onCreate()) onClose();
|
|
else setError(content.resultsCreateError || "Impossible de créer la checklist.");
|
|
} catch {
|
|
setError(content.resultsCreateError || "Impossible de créer la checklist.");
|
|
} finally {
|
|
setCreating(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Modal rootClassName="confirm-modal-root results-checklist-modal-root" dialogClassName="confirm-modal nds-modal results-checklist-modal" labelledBy={titleId} describedBy={descriptionId} onRequestClose={onClose} canClose={!creating}>
|
|
<header className="results-checklist-modal-header">
|
|
<h2 id={titleId}>{content.resultsModalTitle || "Utiliser les résultats visibles"}</h2>
|
|
<button className="nds-icon-button" data-size="sm" type="button" disabled={creating} onClick={onClose} aria-label={content.resultsClose || "Fermer"} title={content.resultsClose || "Fermer"}>
|
|
<Icon name="close" />
|
|
</button>
|
|
</header>
|
|
<p id={descriptionId} className="results-checklist-modal-description">
|
|
{content.resultsModalDescription || "Seuls les éléments actuellement visibles sur la page seront utilisés, après application des filtres."}
|
|
</p>
|
|
<div className="results-checklist-modal-summary">
|
|
<span>{content.resultsChecklistLabel || "Checklist"}</span>
|
|
<strong>{checklistTitle}</strong>
|
|
</div>
|
|
{linkedToolbox ? (
|
|
<div className="results-checklist-modal-target">
|
|
<span>{content.resultsTargetLabel || "Toolbox cible"}</span>
|
|
<strong>{linkedToolbox.name}</strong>
|
|
</div>
|
|
) : (
|
|
<p className="results-checklist-modal-notice nds-inline-notice" data-variant="warning" role="status">
|
|
<Icon name="link" />
|
|
<span>{content.resultsNoToolbox || "Liez une toolbox à cette page pour créer directement la checklist."}</span>
|
|
</p>
|
|
)}
|
|
{error && <p className="results-checklist-modal-error nds-inline-notice" data-variant="danger" role="alert"><Icon name="close" /><span>{error}</span></p>}
|
|
<footer className="nds-modal-footer results-checklist-modal-actions">
|
|
<button className="nds-button" data-size="sm" type="button" disabled={creating} onClick={() => void copyItems()} data-modal-initial-focus="">
|
|
<Icon name="copy" />{content.resultsCopyLabel || "Copier le format texte"}
|
|
</button>
|
|
<button className="nds-button" data-size="sm" data-variant="primary" type="button" disabled={!linkedToolbox || creating} onClick={() => void createChecklist()}>
|
|
<span className="module-icon" aria-hidden="true"><span className="module-icon-svg module-icon-checklist" /></span>
|
|
{creating ? content.resultsCreatingLabel || "Création…" : content.resultsCreateLabel || "Créer dans la toolbox"}
|
|
</button>
|
|
</footer>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
export function CopyChecklistItemsButton({
|
|
labels,
|
|
sections,
|
|
title = "Copier pour checklist",
|
|
filteredResults = false,
|
|
checklistTitle = "Checklist",
|
|
linkedToolbox,
|
|
content = {},
|
|
onCreateChecklist
|
|
}) {
|
|
const [copied, setCopied] = useState(false);
|
|
const [open, setOpen] = useState(false);
|
|
const text = sections ? formatChecklistImportSections(sections) : formatChecklistImportItems(labels);
|
|
|
|
async function copyItems() {
|
|
if (!text || !await copyText(text)) return;
|
|
setCopied(true);
|
|
notify("Copié dans le presse-papiers.");
|
|
window.setTimeout(() => setCopied(false), 1400);
|
|
}
|
|
|
|
async function createChecklist() {
|
|
if (!onCreateChecklist) return false;
|
|
return onCreateChecklist({
|
|
title: checklistTitle,
|
|
...(sections ? { sections } : { items: labels || [] })
|
|
});
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<button
|
|
className={filteredResults ? "results-copy-button nds-button" : "filter-reset-button results-copy-button nds-icon-button"}
|
|
data-size="xs"
|
|
type="button"
|
|
onClick={filteredResults ? () => setOpen(true) : copyItems}
|
|
disabled={!text}
|
|
aria-label={title}
|
|
title={copied ? "Copié" : title}
|
|
>
|
|
<Icon name={filteredResults ? "checklist" : "copy"} />
|
|
{filteredResults && <span>{content.resultsActionLabel || "Résultats vers checklist"}</span>}
|
|
</button>
|
|
{filteredResults && open && (
|
|
<ChecklistResultsModal
|
|
checklistTitle={checklistTitle}
|
|
content={content}
|
|
linkedToolbox={linkedToolbox}
|
|
text={text}
|
|
onCreate={createChecklist}
|
|
onClose={() => setOpen(false)}
|
|
/>
|
|
)}
|
|
</>
|
|
);
|
|
}
|