Add checklist creation for filtered game results
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s
This commit is contained in:
parent
0acf5ecf21
commit
962816bbb9
15 changed files with 414 additions and 40 deletions
|
|
@ -1,7 +1,9 @@
|
|||
// Rôle : copie des données filtrées au format compatible avec l'import checklist.
|
||||
// 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 { useState } from "react";
|
||||
import { useEffect, useId, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { Icon } from "../../components/Icon.jsx";
|
||||
import { lockBodyScroll } from "../../utils/bodyScrollLock.js";
|
||||
|
||||
async function copyText(value) {
|
||||
try {
|
||||
|
|
@ -44,8 +46,105 @@ export function formatChecklistImportSections(sections = []) {
|
|||
.join("\n\n");
|
||||
}
|
||||
|
||||
export function CopyChecklistItemsButton({ labels, sections, title = "Copier pour checklist" }) {
|
||||
function ChecklistResultsModal({ checklistTitle, content, linkedToolbox, text, onCreate, onClose }) {
|
||||
const titleId = useId();
|
||||
const descriptionId = useId();
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
function handleKeyDown(event) {
|
||||
if (event.key === "Escape" && !creating) onClose();
|
||||
}
|
||||
|
||||
const unlock = lockBodyScroll();
|
||||
document.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
unlock();
|
||||
document.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [creating, onClose]);
|
||||
|
||||
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 createPortal(
|
||||
<div className="confirm-modal-root results-checklist-modal-root">
|
||||
<div className="confirm-backdrop nds-modal-backdrop" onClick={() => !creating && onClose()} />
|
||||
<section className="confirm-modal nds-modal results-checklist-modal" role="dialog" aria-modal="true" aria-labelledby={titleId} aria-describedby={descriptionId}>
|
||||
<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()} autoFocus>
|
||||
<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>
|
||||
</section>
|
||||
</div>,
|
||||
document.body
|
||||
);
|
||||
}
|
||||
|
||||
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() {
|
||||
|
|
@ -55,17 +154,37 @@ export function CopyChecklistItemsButton({ labels, sections, title = "Copier pou
|
|||
window.setTimeout(() => setCopied(false), 1400);
|
||||
}
|
||||
|
||||
async function createChecklist() {
|
||||
if (!onCreateChecklist) return false;
|
||||
return onCreateChecklist({
|
||||
title: checklistTitle,
|
||||
...(sections ? { sections } : { items: labels || [] })
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
className="filter-reset-button results-copy-button nds-icon-button"
|
||||
data-size="xs"
|
||||
type="button"
|
||||
onClick={copyItems}
|
||||
disabled={!text}
|
||||
aria-label={title}
|
||||
title={copied ? "Copié" : title}
|
||||
>
|
||||
<Icon name="copy" />
|
||||
</button>
|
||||
<>
|
||||
<button
|
||||
className="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="copy" />
|
||||
</button>
|
||||
{filteredResults && open && (
|
||||
<ChecklistResultsModal
|
||||
checklistTitle={checklistTitle}
|
||||
content={content}
|
||||
linkedToolbox={linkedToolbox}
|
||||
text={text}
|
||||
onCreate={createChecklist}
|
||||
onClose={() => setOpen(false)}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue