import { useEffect, useRef, useState } from 'react'; import { Dialog, Flex, Typography } from '@strapi/design-system'; import { ExclamationTriangleIcon } from '@heroicons/react/24/outline'; import { DangerButton, GradientButton, TertiaryButton } from '../StyledButtons'; import { useMagicMailIntl } from '../../hooks/useMagicMailIntl'; import { useReturnFocus } from '../../hooks/useReturnFocus'; import { accent } from '../../utils/theme'; import styled from 'styled-components'; const WarningIcon = styled(ExclamationTriangleIcon)` width: 24px; height: 24px; color: ${(p) => (p.$danger ? accent.danger(p) : accent.warning(p))}; `; /** * Accessible confirmation dialog (DS 2 alert dialog) in the MagicMail look. * Replaces window.confirm(): it follows dark mode, is translated, traps focus * and announces its title and description to screen readers. * * { await removeAccount(id); setPendingDelete(null); }} * onClose={() => setPendingDelete(null)} * /> * * While the promise returned by onConfirm is pending, both buttons are * disabled, the confirm button shows a spinner and Esc/overlay do not close. * * @param {object} props * @param {boolean} props.isOpen * @param {() => void} props.onClose - called on Cancel, Esc and overlay click * @param {() => (void|Promise)} props.onConfirm * @param {React.ReactNode} props.title * @param {React.ReactNode} [props.description] - main sentence (read as the dialog description) * @param {React.ReactNode} [props.children] - extra body content below the description * @param {string} [props.confirmLabel] * @param {string} [props.cancelLabel] * @param {'danger'|'default'} [props.variant='danger'] * @param {boolean} [props.isLoading] - external loading flag (optional) * @param {React.ReactNode} [props.confirmIcon] * @param {() => (HTMLElement|null|undefined)} [props.getReturnFocusFallback] - focus target on * close when the element that opened the dialog is gone (for example a deleted row) */ export const ConfirmDialog = ({ isOpen, onClose, onConfirm, title, description, children, confirmLabel, cancelLabel, variant = 'danger', isLoading = false, confirmIcon, getReturnFocusFallback, }) => { const { t } = useMagicMailIntl(); const [isPending, setIsPending] = useState(false); const mounted = useRef(true); const returnFocus = useReturnFocus(Boolean(isOpen), getReturnFocusFallback); useEffect(() => { mounted.current = true; return () => { mounted.current = false; }; }, []); const busy = isLoading || isPending; const danger = variant === 'danger'; const ConfirmButton = danger ? DangerButton : GradientButton; const handleConfirm = async () => { if (busy) return; const result = onConfirm?.(); if (result && typeof result.then === 'function') { setIsPending(true); try { await result; } catch { // The caller reports errors (notification); keep the dialog usable. } finally { if (mounted.current) setIsPending(false); } } }; const handleOpenChange = (open) => { if (!open && !busy) onClose?.(); }; return ( {/* The DS header centres a one-line title with flexbox; textAlign keeps a wrapped title (narrow screens, long translations) centred too. */} {title} {/* Dialog.Cancel is the element the alert dialog focuses on open; it closes through onOpenChange, which ignores clicks while busy. */} {cancelLabel ?? t('common.cancel', 'Cancel')} {confirmLabel ?? (danger ? t('common.delete', 'Delete') : t('common.confirm', 'Confirm'))} ); }; export default ConfirmDialog;