From e51fb87cc23b694e5fc5da1e45245edf818e799a Mon Sep 17 00:00:00 2001 From: tonypowa Date: Tue, 4 Nov 2025 10:22:09 +0100 Subject: [PATCH] Alerting: Refactor placeholder email detection into shared utility - Extract duplicate placeholder email detection logic into reusable functions - Add hasPlaceholderEmail() for single channel checks - Add receiverHasPlaceholderEmail() for receiver list checks - Centralize PLACEHOLDER_EMAILS constant in receiver-form.ts - Update ChannelSubForm, TestContactPointModal, and actions to use utilities - Reduces code duplication by 44 lines --- .../receivers/form/ChannelSubForm.tsx | 18 +++------ .../receivers/form/TestContactPointModal.tsx | 22 +++-------- .../alerting/unified/state/actions.ts | 20 +--------- .../alerting/unified/utils/receiver-form.ts | 39 +++++++++++++++++++ 4 files changed, 53 insertions(+), 46 deletions(-) diff --git a/public/app/features/alerting/unified/components/receivers/form/ChannelSubForm.tsx b/public/app/features/alerting/unified/components/receivers/form/ChannelSubForm.tsx index 0c440a09253..781ac80c14f 100644 --- a/public/app/features/alerting/unified/components/receivers/form/ChannelSubForm.tsx +++ b/public/app/features/alerting/unified/components/receivers/form/ChannelSubForm.tsx @@ -16,6 +16,7 @@ import { GrafanaChannelValues, ReceiverFormValues, } from '../../../types/receiver-form'; +import { hasPlaceholderEmail } from '../../../utils/receiver-form'; import { OnCallIntegrationType } from '../grafanaAppReceivers/onCall/useOnCallIntegration'; import { ChannelOptions } from './ChannelOptions'; @@ -71,15 +72,8 @@ export function ChannelSubForm({ const isTestAvailable = onCallIntegrationType !== OnCallIntegrationType.NewIntegration; // Check if email integration has placeholder addresses - const emailAddresses = watch(`${settingsFieldPath}.addresses`); - const hasPlaceholderEmail = useMemo(() => { - if (selectedType !== 'email' || !emailAddresses) { - return false; - } - const placeholders = ['', 'example@email.com']; - const addresses = typeof emailAddresses === 'string' ? [emailAddresses] : emailAddresses; - return addresses.some((addr: string) => placeholders.includes(addr?.trim())); - }, [selectedType, emailAddresses]); + const channelValues = getValues(channelFieldPath) as GrafanaChannelValues | undefined; + const isPlaceholderEmail = hasPlaceholderEmail(channelValues); useEffect(() => { register(`${channelFieldPath}.__id`); @@ -242,14 +236,14 @@ export function ChannelSubForm({
{isTestable && onTest && isTestAvailable && (
{notifier && (
- {hasPlaceholderEmail && ( + {isPlaceholderEmail && ( { - if (!channelValues || channelValues.type !== 'email') { - return false; - } - const addresses = channelValues.settings?.addresses; - if (!addresses) { - return false; - } - const placeholders = ['', 'example@email.com']; - const addressList = typeof addresses === 'string' ? [addresses] : addresses; - return addressList.some((addr: string) => placeholders.includes(addr?.trim())); - }, [channelValues]); + const isPlaceholderEmail = hasPlaceholderEmail(channelValues); const onSubmit = async (data: FormFields) => { let alert: TestReceiversAlert | undefined; @@ -114,7 +104,7 @@ export const TestContactPointModal = ({ /> )} - {hasPlaceholderEmail && ( + {isPlaceholderEmail && (