// Copyright 2019 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import type { MouseEvent } from 'react'; import React, { useCallback } from 'react'; import { animated } from '@react-spring/web'; import { Button, ButtonVariant } from './Button'; import type { LocalizerType } from '../types/Util'; import { ModalHost } from './ModalHost'; import { ModalPage } from './Modal'; import type { Theme } from '../util/theme'; import { useAnimated } from '../hooks/useAnimated'; import { Spinner } from './Spinner'; export type ActionSpec = { text: string; action: () => unknown; style?: 'affirmative' | 'negative'; autoClose?: boolean; }; export type OwnProps = Readonly<{ actions?: Array; dialogName: string; cancelButtonVariant?: ButtonVariant; cancelText?: string; isSpinning?: boolean; children?: React.ReactNode; hasXButton?: boolean; i18n: LocalizerType; moduleClassName?: string; noMouseClose?: boolean; noDefaultCancelButton?: boolean; onCancel?: () => unknown; onClose: () => unknown; onTopOfEverything?: boolean; theme?: Theme; title?: React.ReactNode; }>; export type Props = OwnProps; function focusRef(el: HTMLElement | null) { if (el) { el.focus(); } } function getButtonVariant( buttonStyle?: 'affirmative' | 'negative' ): ButtonVariant { if (buttonStyle === 'affirmative') { return ButtonVariant.Primary; } if (buttonStyle === 'negative') { return ButtonVariant.Destructive; } return ButtonVariant.Secondary; } export const ConfirmationDialog = React.memo(function ConfirmationDialogInner({ actions = [], dialogName, cancelButtonVariant, cancelText, children, hasXButton, i18n, isSpinning, moduleClassName, noMouseClose, noDefaultCancelButton, onCancel, onClose, onTopOfEverything, theme, title, }: Props) { const { close, overlayStyles, modalStyles } = useAnimated(onClose, { getFrom: () => ({ opacity: 0, transform: 'scale(0.25)' }), getTo: isOpen => ({ opacity: isOpen ? 1 : 0, transform: 'scale(1)' }), }); const cancelAndClose = useCallback(() => { if (onCancel) { onCancel(); } close(); }, [close, onCancel]); const handleCancel = useCallback( (e: MouseEvent) => { if (e.target === e.currentTarget) { cancelAndClose(); } }, [cancelAndClose] ); const hasActions = Boolean(actions.length); const footer = ( <> {!isSpinning && !noDefaultCancelButton ? ( ) : null} {actions.map((action, i) => ( ))} ); const modalName = `ConfirmationDialog.${dialogName}`; return ( {children} ); });