import { useState } from 'react'; import { __, _x } from '@wordpress/i18n'; import { extractEmailDomain } from 'common/functions'; import { MailPoet } from 'mailpoet'; import ReactStringReplace from 'react-string-replace'; import { AuthorizeSenderEmailAndDomainModal } from 'common/authorize-sender-email-and-domain-modal'; const userHostDomain = window.location.hostname.replace('www.', ''); const suggestedEmailAddress = `contact@${userHostDomain}`; type Props = { emailAddress: string; mssActive: boolean; isEmailAuthorized?: boolean; showSenderDomainWarning?: boolean; onSuccessfulEmailOrDomainAuthorization?: (data) => void; }; function SenderEmailAddressWarning({ emailAddress, mssActive, isEmailAuthorized = true, showSenderDomainWarning = false, onSuccessfulEmailOrDomainAuthorization = () => {}, }: Props) { const [showAuthorizedEmailModal, setShowAuthorizedEmailModal] = useState(null); const loadModal = (event, tab) => { event.preventDefault(); setShowAuthorizedEmailModal(tab); }; const switchToNewTab = (newTab) => { setShowAuthorizedEmailModal(newTab); }; const emailAddressDomain = extractEmailDomain(emailAddress); const displayElements = []; const isFreeDomain = MailPoet.freeMailDomains.indexOf(emailAddressDomain) > -1; if (mssActive) { if (!isEmailAuthorized) { displayElements.push(

{ReactStringReplace( __( 'Not an authorized sender email address. [link]Authorize it now.[/link]', 'mailpoet', ), /\[link\](.*?)\[\/link\]/g, (match) => ( loadModal(e, 'sender_email')} key={emailAddress} > {match} ), )}

, ); } if (showSenderDomainWarning && isEmailAuthorized) { displayElements.push(

{ReactStringReplace( __( 'Email violates Sender Domain’s DMARC policy. Please set up [link]sender authentication[/link].', 'mailpoet', ), /\[link](.*?)\[\/link]/g, (match) => ( loadModal(e, 'sender_domain')} > {match} ), )}

, ); } displayElements.push(
{showAuthorizedEmailModal && ( { setShowAuthorizedEmailModal(null); }} showSenderEmailTab={!isEmailAuthorized} showSenderDomainTab={showSenderDomainWarning && isEmailAuthorized} initialTab={showAuthorizedEmailModal} onSuccessAction={onSuccessfulEmailOrDomainAuthorization} autoSwitchTab={switchToNewTab} /> )}
, ); if (displayElements.length) return ( <> {' '} {displayElements.map((child) => (
{child}
))} ); return null; } if (isFreeDomain) { return ( <>

{_x( 'You might not reach the inbox of your subscribers if you use this email address.', 'In the last step, before sending a newsletter. URL: ?page=mailpoet-newsletters#/send/2', 'mailpoet', )}

{ReactStringReplace( _x( 'Use an address like %1$s for the Sender and put %2$s in the Reply-to field below.', 'In the last step, before sending a newsletter. URL: ?page=mailpoet-newsletters#/send/2', 'mailpoet', ), /(%1\$s|%2\$s|.*<\/em>)/, (match) => { if (match === '%1$s') return suggestedEmailAddress; if (match === '%2$s') return {emailAddress}; return {match.replace(/<\/?em>/g, '')}; }, )}

{__('Read more', 'mailpoet')}

); } return null; } SenderEmailAddressWarning.displayName = 'SenderEmailAddressWarning'; export { SenderEmailAddressWarning };