/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import { EuiFieldNumber, EuiFieldText, EuiFormRow, EuiRadioGroup, EuiRadioGroupOption, EuiSpacer, EuiSuperSelect, } from '@elastic/eui'; import React, { useContext } from 'react'; import { CUSTOM_WEBHOOK_ENDPOINT_TYPE } from '../../../utils/constants'; import { HeaderItemType, WebhookHttpType, WebhookMethodType, } from '../../Channels/types'; import { CreateChannelContext } from '../CreateChannel'; import { validateCustomURLHost, validateCustomURLPort, validateWebhookURL, } from '../utils/validationHelper'; import { WebhookHeaders } from './WebhookHeaders'; interface CustomWebhookSettingsProps { webhookTypeIdSelected: keyof typeof CUSTOM_WEBHOOK_ENDPOINT_TYPE; setWebhookTypeIdSelected: ( id: keyof typeof CUSTOM_WEBHOOK_ENDPOINT_TYPE ) => void; webhookURL: string; setWebhookURL: (webhookURL: string) => void; customURLType: WebhookHttpType; setCustomURLType: (customURLType: WebhookHttpType) => void; customURLHost: string; setCustomURLHost: (customURLHost: string) => void; customURLPort: string; setCustomURLPort: (customURLPort: string) => void; customURLPath: string; setCustomURLPath: (customURLPath: string) => void; webhookMethod: WebhookMethodType; setWebhookMethod: (webhookMethod: WebhookMethodType) => void; webhookParams: HeaderItemType[]; setWebhookParams: (webhookParams: HeaderItemType[]) => void; webhookHeaders: HeaderItemType[]; setWebhookHeaders: (webhookHeaders: HeaderItemType[]) => void; } export function CustomWebhookSettings(props: CustomWebhookSettingsProps) { const context = useContext(CreateChannelContext)!; const webhookTypeOptions: EuiRadioGroupOption[] = Object.entries( CUSTOM_WEBHOOK_ENDPOINT_TYPE ).map(([key, value]) => ({ id: key, label: value, })); const renderWebhook = () => { return ( 0} > props.setWebhookURL(e.target.value)} isInvalid={context.inputErrors.webhookURL.length > 0} onBlur={() => { context.setInputErrors({ ...context.inputErrors, webhookURL: validateWebhookURL(props.webhookURL), }); }} /> ); }; const renderCustomURL = () => { return ( <> 0} > props.setCustomURLHost(e.target.value)} isInvalid={context.inputErrors.customURLHost.length > 0} onBlur={() => { context.setInputErrors({ ...context.inputErrors, customURLHost: validateCustomURLHost(props.customURLHost), }); }} /> Port - optional } error={context.inputErrors.customURLPort.join(' ')} isInvalid={context.inputErrors.customURLPort.length > 0} > props.setCustomURLPort(e.target.value)} isInvalid={context.inputErrors.customURLPort.length > 0} onBlur={() => { context.setInputErrors({ ...context.inputErrors, customURLPort: validateCustomURLPort(props.customURLPort), }); }} /> Path - optional } > props.setCustomURLPath(e.target.value)} /> ); }; return ( <> props.setWebhookTypeIdSelected( id as keyof typeof CUSTOM_WEBHOOK_ENDPOINT_TYPE ) } /> {props.webhookTypeIdSelected === 'WEBHOOK_URL' ? renderWebhook() : renderCustomURL()} ); }