/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { useRef, forwardRef, useMemo, useImperativeHandle, useContext, useEffect, useState } from "react"; import { EuiBadge, EuiButton, EuiFlexGroup, EuiFlexItem, EuiSpacer, EuiTitle } from "@elastic/eui"; import ChannelSelect, { useChannels } from "../ChannelSelect"; import { AllBuiltInComponents } from "../../components/FormGenerator"; import { ActionType, FieldEnum, FieldMapLabel, OperationType, OperationTypeMapTitle, VALIDATE_ERROR_FOR_CHANNELS, } from "../../pages/Notifications/constant"; import { GetLronConfig, associateWithTask, checkPermissionForSubmitLRONConfig, ifSetDefaultNotification } from "./hooks"; import { ServicesContext } from "../../services"; import { BrowserServices } from "../../models/interfaces"; import { ILronConfig } from "../../pages/Notifications/interface"; import useField, { FieldInstance } from "../../lib/field"; import { FeatureChannelList } from "../../../server/models/interfaces"; import CustomFormRow from "../../components/CustomFormRow"; import { CoreServicesContext } from "../../components/core_services"; import { CoreStart } from "opensearch-dashboards/public"; import NotificationCallout from "./NotificationCallout"; import { ContentPanel, ContentPanelProps } from "../../components/ContentPanel"; export interface NotificationConfigProps { actionType: ActionType; operationType?: OperationType; withPanel?: boolean; panelProps?: Omit; } export interface NotificationConfigRef extends FieldInstance { associateWithTask: (props: { taskId: string }) => Promise; } const NotificationConfig = ( { actionType, operationType, withPanel, panelProps }: NotificationConfigProps, ref: React.Ref ) => { const { channels } = useChannels(); const field = useField< ILronConfig & { customize: boolean; } >({ onChange(name, value) { if ((name[1] === FieldEnum.success || name[1] === FieldEnum.failure) && !value) { field.validatePromise(); } }, values: { customize: false, lron_condition: { success: false, failure: false, }, channels: [], }, }); const [LronConfig, setLronConfig] = useState(); const [permissionForCreateLRON, setPermissionForCreateLRON] = useState(false); const [permissionForViewLRON, setPermissionForViewLRON] = useState(false); const context = useContext(ServicesContext) as BrowserServices; const coreServices = useContext(CoreServicesContext) as CoreStart; const stateRef = useRef<{ permissionForCreateLRON: boolean; }>({ permissionForCreateLRON, }); stateRef.current.permissionForCreateLRON = permissionForCreateLRON; useEffect(() => { GetLronConfig({ actionType, services: context, }).then((res) => { if (res && res.ok) { setPermissionForViewLRON(true); const lronConfig = res.response?.lron_configs?.[0]?.lron_config; setLronConfig(lronConfig); if (!ifSetDefaultNotification(lronConfig)) { field.setValue("customize", true); } } else { field.setValue("customize", true); } }); checkPermissionForSubmitLRONConfig({ services: context, }).then((result) => setPermissionForCreateLRON(result)); }, []); const selectedChannels: FeatureChannelList[] = useMemo(() => { return (LronConfig?.channels || []) .map((item) => channels.find((channel) => channel.config_id === item.id)) .filter((item) => item) as FeatureChannelList[]; }, [LronConfig, channels]); const values = field.getValues(); useImperativeHandle(ref, () => ({ ...field, associateWithTask: ({ taskId }) => { const { customize, ...others } = field.getValues(); if (!customize || !stateRef.current.permissionForCreateLRON) { return Promise.resolve(true); } return associateWithTask({ services: context, coreServices, taskId, lronConfig: others, }); }, })); const hasDefaultNotification = ifSetDefaultNotification(LronConfig); if (!hasDefaultNotification && !permissionForCreateLRON) { return null; } const content = (
Notifications
{hasDefaultNotification ? ( <>
    {LronConfig?.lron_condition.failure ?
  • Has failed
  • : null} {LronConfig?.lron_condition.success ?
  • Has completed
  • : null}
{selectedChannels?.map((item) => ( {item.name} ({item.config_type}) ))}
) : null} {hasDefaultNotification && permissionForCreateLRON ? ( <> ) : null} {values.customize && permissionForCreateLRON ? ( <> <> {values?.lron_condition?.[FieldEnum.failure] || values?.lron_condition?.[FieldEnum.success] ? ( <> window.open("/app/notifications-dashboards#/channels")} iconType="popout"> Manage channels ) : null} ) : null}
); return withPanel ? ( <> {content} ) : ( <> {content} ); }; export default forwardRef(NotificationConfig);