From 22c6527c49fbcfb1e9a809b78075056e6a9726bb Mon Sep 17 00:00:00 2001 From: Sag Date: Wed, 29 Nov 2023 15:35:33 -0300 Subject: [PATCH] Moved helpers for newsletter emails into a separate util (#19185) no issue --- .../components/settings/email/Newsletters.tsx | 2 +- .../newsletters/NewsletterDetailModal.tsx | 42 +------------------ .../email/newsletters/NewsletterPreview.tsx | 2 +- .../src/utils/newsletterEmails.ts | 41 ++++++++++++++++++ 4 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 apps/admin-x-settings/src/utils/newsletterEmails.ts diff --git a/apps/admin-x-settings/src/components/settings/email/Newsletters.tsx b/apps/admin-x-settings/src/components/settings/email/Newsletters.tsx index 702b0f14df..60f641af94 100644 --- a/apps/admin-x-settings/src/components/settings/email/Newsletters.tsx +++ b/apps/admin-x-settings/src/components/settings/email/Newsletters.tsx @@ -48,7 +48,7 @@ const Newsletters: React.FC<{ keywords: string[] }> = ({keywords}) => { const verify = async () => { try { - const {newsletters: [updatedNewsletter], meta: {email_verified: emailVerified = []} = {}} = await verifyEmail({token: verifyEmailToken}); + const {newsletters: [updatedNewsletter], meta: {email_verified: emailVerified} = {}} = await verifyEmail({token: verifyEmailToken}); let title; let prompt; diff --git a/apps/admin-x-settings/src/components/settings/email/newsletters/NewsletterDetailModal.tsx b/apps/admin-x-settings/src/components/settings/email/newsletters/NewsletterDetailModal.tsx index bf12abe607..33bac53303 100644 --- a/apps/admin-x-settings/src/components/settings/email/newsletters/NewsletterDetailModal.tsx +++ b/apps/admin-x-settings/src/components/settings/email/newsletters/NewsletterDetailModal.tsx @@ -5,55 +5,17 @@ import useFeatureFlag from '../../../../hooks/useFeatureFlag'; import useSettingGroup from '../../../../hooks/useSettingGroup'; import validator from 'validator'; import {Button, ButtonGroup, ColorPickerField, ConfirmationModal, Form, Heading, Hint, HtmlField, Icon, ImageUpload, LimitModal, Link, PreviewModalContent, Select, SelectOption, Separator, Tab, TabView, TextArea, TextField, Toggle, ToggleGroup, showToast} from '@tryghost/admin-x-design-system'; -import {Config, hasSendingDomain, isManagedEmail, sendingDomain} from '@tryghost/admin-x-framework/api/config'; import {ErrorMessages, useForm, useHandleError} from '@tryghost/admin-x-framework/hooks'; import {HostLimitError, useLimiter} from '../../../../hooks/useLimiter'; import {Newsletter, useBrowseNewsletters, useEditNewsletter} from '@tryghost/admin-x-framework/api/newsletters'; import {RoutingModalProps, useRouting} from '@tryghost/admin-x-framework/routing'; import {getImageUrl, useUploadImage} from '@tryghost/admin-x-framework/api/images'; import {getSettingValues} from '@tryghost/admin-x-framework/api/settings'; +import {hasSendingDomain, isManagedEmail, sendingDomain} from '@tryghost/admin-x-framework/api/config'; +import {renderReplyToEmail, renderSenderEmail} from '../../../../utils/newsletterEmails'; import {textColorForBackgroundColor} from '@tryghost/color-utils'; import {useGlobalData} from '../../../providers/GlobalDataProvider'; -export const renderSenderEmail = (newsletter: Newsletter, config: Config, defaultEmailAddress: string|undefined) => { - if (isManagedEmail(config) && !hasSendingDomain(config) && defaultEmailAddress) { - // Not changeable: sender_email is ignored - return defaultEmailAddress; - } - - if (isManagedEmail(config) && hasSendingDomain(config)) { - // Only return sender_email if the domain names match - if (newsletter.sender_email?.split('@')[1] === sendingDomain(config)) { - return newsletter.sender_email; - } else { - return defaultEmailAddress || ''; - } - } - - return newsletter.sender_email || defaultEmailAddress || ''; -}; - -export const renderReplyToEmail = (newsletter: Newsletter, config: Config, supportEmailAddress: string|undefined, defaultEmailAddress: string|undefined) => { - if (newsletter.sender_reply_to === 'newsletter') { - return renderSenderEmail(newsletter, config, defaultEmailAddress); - } - - if (newsletter.sender_reply_to === 'support') { - return supportEmailAddress || defaultEmailAddress || ''; - } - - if (isManagedEmail(config) && hasSendingDomain(config)) { - // Only return sender_reply_to if the domain names match - if (newsletter.sender_reply_to.split('@')[1] === sendingDomain(config)) { - return newsletter.sender_reply_to; - } else { - return ''; - } - } - - return newsletter.sender_reply_to; -}; - const Sidebar: React.FC<{ newsletter: Newsletter; onlyOne: boolean; diff --git a/apps/admin-x-settings/src/components/settings/email/newsletters/NewsletterPreview.tsx b/apps/admin-x-settings/src/components/settings/email/newsletters/NewsletterPreview.tsx index f75d3c0e5e..a62a2dccf3 100644 --- a/apps/admin-x-settings/src/components/settings/email/newsletters/NewsletterPreview.tsx +++ b/apps/admin-x-settings/src/components/settings/email/newsletters/NewsletterPreview.tsx @@ -3,7 +3,7 @@ import React from 'react'; import useFeatureFlag from '../../../../hooks/useFeatureFlag'; import {Newsletter} from '@tryghost/admin-x-framework/api/newsletters'; import {getSettingValues} from '@tryghost/admin-x-framework/api/settings'; -import {renderReplyToEmail, renderSenderEmail} from './NewsletterDetailModal'; +import {renderReplyToEmail, renderSenderEmail} from '../../../../utils/newsletterEmails'; import {textColorForBackgroundColor} from '@tryghost/color-utils'; import {useGlobalData} from '../../../providers/GlobalDataProvider'; diff --git a/apps/admin-x-settings/src/utils/newsletterEmails.ts b/apps/admin-x-settings/src/utils/newsletterEmails.ts new file mode 100644 index 0000000000..f4752da46b --- /dev/null +++ b/apps/admin-x-settings/src/utils/newsletterEmails.ts @@ -0,0 +1,41 @@ +import {Config, hasSendingDomain, isManagedEmail, sendingDomain} from '@tryghost/admin-x-framework/api/config'; +import {Newsletter} from '@tryghost/admin-x-framework/api/newsletters'; + +export const renderSenderEmail = (newsletter: Newsletter, config: Config, defaultEmailAddress: string|undefined) => { + if (isManagedEmail(config) && !hasSendingDomain(config) && defaultEmailAddress) { + // Not changeable: sender_email is ignored + return defaultEmailAddress; + } + + if (isManagedEmail(config) && hasSendingDomain(config)) { + // Only return sender_email if the domain names match + if (newsletter.sender_email?.split('@')[1] === sendingDomain(config)) { + return newsletter.sender_email; + } else { + return defaultEmailAddress || ''; + } + } + + return newsletter.sender_email || defaultEmailAddress || ''; +}; + +export const renderReplyToEmail = (newsletter: Newsletter, config: Config, supportEmailAddress: string|undefined, defaultEmailAddress: string|undefined) => { + if (newsletter.sender_reply_to === 'newsletter') { + return renderSenderEmail(newsletter, config, defaultEmailAddress); + } + + if (newsletter.sender_reply_to === 'support') { + return supportEmailAddress || defaultEmailAddress || ''; + } + + if (isManagedEmail(config) && hasSendingDomain(config)) { + // Only return sender_reply_to if the domain names match + if (newsletter.sender_reply_to.split('@')[1] === sendingDomain(config)) { + return newsletter.sender_reply_to; + } else { + return ''; + } + } + + return newsletter.sender_reply_to; +};