192 lines
11 KiB
JavaScript
192 lines
11 KiB
JavaScript
// Rôle : fournit les modales globales liées aux toolboxes et confirmations.
|
|
import { useEffect, useId, useState } from "react";
|
|
import { flushSync } from "react-dom";
|
|
import { Icon } from "./Icon.jsx";
|
|
import { ImportButton } from "./ImportButton.jsx";
|
|
import { Modal } from "./Modal.jsx";
|
|
import { ToastPositionSwitch } from "./ToastPositionSwitch.jsx";
|
|
|
|
export function ConfirmModal({ title, message, confirmLabel = "Confirmer", cancelLabel = "Annuler", danger = false, onClose }) {
|
|
const titleId = useId();
|
|
const messageId = useId();
|
|
return (
|
|
<Modal labelledBy={titleId} describedBy={messageId} onRequestClose={() => onClose(false)}>
|
|
<header><h2 id={titleId}>{title}</h2></header>
|
|
<p id={messageId}>{message}</p>
|
|
<footer className="nds-modal-footer">
|
|
<button className="nds-button" data-size="sm" data-modal-initial-focus={danger ? "" : undefined} onClick={() => onClose(false)}>{cancelLabel}</button>
|
|
<button className="confirm-danger nds-button" data-size="sm" data-variant={danger ? "danger" : "primary"} onClick={() => onClose(true)}>{confirmLabel}</button>
|
|
</footer>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
export function NotificationToast({ id, message, durationMs = 6000, actionLabel = "", onAction, onClose }) {
|
|
useEffect(() => {
|
|
if (durationMs <= 0) return undefined;
|
|
const timeoutId = window.setTimeout(() => onClose(id), durationMs);
|
|
return () => window.clearTimeout(timeoutId);
|
|
}, [durationMs, id, onClose]);
|
|
|
|
return (
|
|
<div className={`notification-toast nds-toast${actionLabel && onAction ? " has-action" : ""}`} role="status">
|
|
<span>{message}</span>
|
|
{actionLabel && onAction && <button className="nds-button notification-toast-action" data-size="xs" data-variant="primary" type="button" onClick={() => {
|
|
flushSync(() => onClose(id));
|
|
onAction();
|
|
}}>{actionLabel}</button>}
|
|
<button className="nds-icon-button" data-size="xs" type="button" onClick={() => onClose(id)} aria-label="Fermer l'alerte" title="Fermer">
|
|
<Icon name="close" />
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function CreateToolboxModal({ gameId, initialName = "", onClose }) {
|
|
const [name, setName] = useState(initialName);
|
|
return (
|
|
<Modal dialogClassName="confirm-modal nds-modal toolbox-create-modal" labelledBy="toolbox-create-title" onRequestClose={() => onClose("")}>
|
|
<header><h2 id="toolbox-create-title">{gameId ? "Créer et associer une toolbox" : "Nouvelle toolbox"}</h2></header>
|
|
<form onSubmit={(event) => {
|
|
event.preventDefault();
|
|
if (name.trim()) onClose(name.trim());
|
|
}}>
|
|
<label className="field"><span>Nom</span><input className="nds-input" data-size="sm" name="name" type="text" autoComplete="off" placeholder="Nom de la toolbox" required value={name} onChange={(event) => setName(event.target.value)} data-modal-initial-focus="" /></label>
|
|
<footer className="nds-modal-footer"><button className="nds-button" data-size="sm" type="button" onClick={() => onClose("")}>Annuler</button><button className="nds-button" data-size="sm" data-variant="primary" type="submit">Créer</button></footer>
|
|
</form>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
export function LinkToolboxModal({ toolboxes, selectedId, onClose }) {
|
|
const [toolboxId, setToolboxId] = useState(selectedId);
|
|
return (
|
|
<Modal dialogClassName="confirm-modal nds-modal toolbox-link-modal" labelledBy="toolbox-link-title" onRequestClose={() => onClose(null)}>
|
|
<header><h2 id="toolbox-link-title">Lier une toolbox</h2></header>
|
|
<form onSubmit={(event) => {
|
|
event.preventDefault();
|
|
onClose(toolboxId);
|
|
}}>
|
|
<label className="field"><span>Toolbox associée</span><select className="nds-select" data-size="sm" value={toolboxId} onChange={(event) => setToolboxId(event.target.value)} data-modal-initial-focus=""><option value="">Aucune</option>{toolboxes.map((toolbox) => <option key={toolbox.id} value={toolbox.id}>{toolbox.name}</option>)}</select></label>
|
|
<footer className="nds-modal-footer"><button className="nds-button" data-size="sm" type="button" onClick={() => onClose(null)}>Annuler</button><button className="nds-button" data-size="sm" data-variant="primary" type="submit">Valider</button></footer>
|
|
</form>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
export function ToolboxManagementModal({ content, toastPosition, setToastPosition, actions, onClose }) {
|
|
const titleId = useId();
|
|
const descriptionId = useId();
|
|
|
|
return (
|
|
<Modal
|
|
rootClassName="confirm-modal-root toolbox-management-modal-root"
|
|
dialogClassName="confirm-modal nds-modal toolbox-management-modal themed-scrollbar"
|
|
labelledBy={titleId}
|
|
describedBy={descriptionId}
|
|
onRequestClose={onClose}
|
|
>
|
|
<header className="toolbox-management-header">
|
|
<div>
|
|
<p className="eyebrow">{content.management.eyebrow}</p>
|
|
<h2 id={titleId}>{content.management.title}</h2>
|
|
</div>
|
|
<button className="nds-icon-button" data-size="sm" type="button" onClick={onClose} aria-label={content.management.close} title={content.management.close}><Icon name="close" /></button>
|
|
</header>
|
|
<p id={descriptionId} className="toolbox-management-description">{content.management.description}</p>
|
|
<section className="toolbox-management-section toolbox-management-preferences" aria-labelledby="toolbox-management-preferences-title">
|
|
<div>
|
|
<p className="eyebrow">{content.preferencesLabel}</p>
|
|
<h3 id="toolbox-management-preferences-title">{content.toastPositionLabel}</h3>
|
|
</div>
|
|
<ToastPositionSwitch
|
|
position={toastPosition}
|
|
onChange={setToastPosition}
|
|
initialFocus
|
|
labels={{
|
|
title: content.toastPositionLabel,
|
|
left: content.toastPositionLeft,
|
|
right: content.toastPositionRight
|
|
}}
|
|
/>
|
|
</section>
|
|
<section className="toolbox-management-section" aria-labelledby="toolbox-management-data-title">
|
|
<div>
|
|
<h3 id="toolbox-management-data-title">{content.management.dataTitle}</h3>
|
|
<p>{content.management.dataDescription}</p>
|
|
</div>
|
|
<div className="toolbox-management-actions nds-toolbar">
|
|
<button className="nds-button" data-size="sm" type="button" onClick={actions.exportAllToolboxes}><Icon name="export" />{content.exportAll}</button>
|
|
<ImportButton className="nds-button" data-size="sm" label={content.importAll} icon="import" onFile={actions.importAllToolboxes} />
|
|
</div>
|
|
</section>
|
|
<details className="toolbox-storage-details">
|
|
<summary>{content.management.storageDetailsTitle}</summary>
|
|
<div className="toolbox-storage-details-content">
|
|
<p>{content.storageHelp.text}</p>
|
|
<ul>{content.storageHelp.items.map((item) => <li key={item}>{item}</li>)}</ul>
|
|
</div>
|
|
</details>
|
|
<footer className="nds-modal-footer toolbox-management-footer">
|
|
<button className="nds-button" data-size="sm" type="button" onClick={onClose}>{content.management.close}</button>
|
|
</footer>
|
|
</Modal>
|
|
);
|
|
}
|
|
|
|
export function AutoBackupModal({ backup, content, onClose }) {
|
|
const folderName = backup.config.directoryHandle?.name || content.noFolder;
|
|
const lastBackup = backup.config.lastBackup
|
|
? new Date(backup.config.lastBackup).toLocaleString()
|
|
: content.never;
|
|
|
|
return (
|
|
<Modal rootClassName="confirm-modal-root auto-backup-modal-root" dialogClassName="confirm-modal nds-modal auto-backup-modal themed-scrollbar" labelledBy="auto-backup-title" onRequestClose={onClose} canClose={!backup.busy}>
|
|
<header className="auto-backup-modal-header">
|
|
<div>
|
|
<p className="eyebrow">{content.eyebrow}</p>
|
|
<h2 id="auto-backup-title">{content.title}</h2>
|
|
</div>
|
|
<button className="nds-icon-button" data-size="sm" type="button" disabled={backup.busy} onClick={onClose} aria-label={content.close} title={content.close}><Icon name="close" /></button>
|
|
</header>
|
|
<p className="auto-backup-description">{content.description}</p>
|
|
{!backup.supported ? <p className="nds-inline-notice" data-variant="warning" role="status">{content.unsupported}</p> : (
|
|
<section className="auto-backup-settings" aria-label={content.title}>
|
|
<div className="auto-backup-status-grid">
|
|
<div className="auto-backup-folder">
|
|
<div><span className="eyebrow">{content.folderLabel}</span><strong>{folderName}</strong></div>
|
|
<button className="nds-button" data-size="sm" type="button" disabled={backup.busy} onClick={() => void backup.chooseDirectory()}>{backup.config.directoryHandle ? content.changeFolder : content.chooseFolder}</button>
|
|
</div>
|
|
<div className="auto-backup-last">
|
|
<span className="eyebrow">{content.lastBackup}</span>
|
|
<strong>{lastBackup}</strong>
|
|
</div>
|
|
</div>
|
|
<label className="auto-backup-toggle">
|
|
<input className="native-switch-input" type="checkbox" checked={backup.config.enabled} disabled={backup.busy} onChange={(event) => void backup.setEnabled(event.target.checked)} />
|
|
<span className="switch-track" aria-hidden="true"><span className="switch-thumb" /></span>
|
|
<span className="auto-backup-toggle-copy"><strong>{content.enabled}</strong><small>{content.toggleHelp}</small><small>{content.retention}</small></span>
|
|
</label>
|
|
{backup.config.directoryHandle && (backup.permission === "prompt" || backup.permission === "denied") && <p className="nds-inline-notice" data-variant="warning" role="status">{content.permissionRequired}</p>}
|
|
{backup.error && <p className="nds-inline-notice" data-variant="danger" role="alert">{backup.error}</p>}
|
|
<footer className="nds-modal-footer auto-backup-modal-footer">
|
|
{backup.config.directoryHandle && (backup.permission === "prompt" || backup.permission === "denied") && <button className="nds-button" data-size="sm" data-variant="primary" type="button" disabled={backup.busy} onClick={() => void backup.requestPermission()}>{content.reauthorize}</button>}
|
|
<button className="nds-button" data-size="sm" data-variant="primary" type="button" disabled={backup.busy || !backup.config.directoryHandle || backup.permission !== "granted"} onClick={() => void backup.saveNow()}>{content.saveNow}</button>
|
|
</footer>
|
|
</section>
|
|
)}
|
|
<section className="auto-backup-compatibility" aria-labelledby="auto-backup-compatibility-title">
|
|
<strong id="auto-backup-compatibility-title">{content.compatibleLabel}</strong>
|
|
<ul>{content.browsers.map((browser) => <li key={browser}>{browser}</li>)}</ul>
|
|
</section>
|
|
<section className="auto-backup-permission-guide" aria-labelledby="auto-backup-permission-title">
|
|
<strong id="auto-backup-permission-title">{content.permissionTitle}</strong>
|
|
<p>{content.permissionDescription}</p>
|
|
<ol>{content.permissionSteps.map((step) => <li key={step}>{step}</li>)}</ol>
|
|
</section>
|
|
<footer className="nds-modal-footer auto-backup-close-footer">
|
|
<button className="nds-button" data-size="sm" type="button" disabled={backup.busy} onClick={onClose}>{content.close}</button>
|
|
</footer>
|
|
</Modal>
|
|
);
|
|
}
|