import React from 'react';
import { c } from 'ttag';
import PropTypes from 'prop-types';
import {
    Alert,
    DropdownActions,
    ConfirmModal,
    useModals,
    useApiWithoutResult,
    useNotifications,
    useEventManager
} from 'react-components';
import { deleteDomain } from 'proton-shared/lib/api/domains';

import DomainModal from './DomainModal';
import CatchAllModal from './CatchAllModal';

const DomainActions = ({ domain, domainAddresses }) => {
    const { request } = useApiWithoutResult(deleteDomain);
    const { createNotification } = useNotifications();
    const { createModal } = useModals();
    const { call } = useEventManager();

    const handleConfirmDelete = async () => {
        await request(domain.ID);
        await call();
        createNotification({ text: c('Success message').t`Domain deleted` });
    };

    const list = [
        {
            text: c('Action').t`Edit`,
            onClick: () => createModal(<DomainModal domain={domain} domainAddresses={domainAddresses} />)
        },
        {
            text: c('Action').t`Set catch-all`,
            onClick: () => createModal(<CatchAllModal domain={domain} domainAddresses={domainAddresses} />)
        },
        {
            text: c('Action').t`Delete`,
            onClick: () => {
                createModal(
                    <ConfirmModal onConfirm={handleConfirmDelete} title={c('Title').t`Delete domain`}>
                        <Alert>{c('Info').t`Are you sure you want to delete this domain?`}</Alert>
                    </ConfirmModal>
                );
            }
        }
    ];

    return <DropdownActions className="pm-button--small" list={list} />;
};

DomainActions.propTypes = {
    domain: PropTypes.object.isRequired,
    domainAddresses: PropTypes.array
};

export default DomainActions;

@@ -1026,34 +1026,110 @@
      %7D,%0A        
-%7B%0A
+Array.isArray(domainAddresses) &&%0A            domainAddresses.length && %7B%0A    
             text
@@ -1160,16 +1160,20 @@
 h-all%60,%0A
+    
         
@@ -1264,32 +1264,36 @@
 nAddresses%7D /%3E)%0A
+    
         %7D,%0A     
@@ -1682,16 +1682,32 @@
  %7D%0A    %5D
+.filter(Boolean)
 ;%0A%0A    r

Fix MAILWEB-400 - Display "Set catch-all" when we have addresses