/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { ChangeEvent, Component, useContext } from "react"; import { EuiLink, EuiIcon, EuiFlexGroup, EuiFlexItem, EuiText } from "@elastic/eui"; import { ContentPanel } from "../../components/ContentPanel"; import "brace/theme/github"; import "brace/mode/json"; import { FeatureChannelList } from "../../../server/models/interfaces"; import { BrowserServices } from "../../models/interfaces"; import { ErrorNotification as IErrorNotification } from "../../../models/interfaces"; import { ServicesContext } from "../../services"; import { getErrorMessage } from "../../utils/helpers"; import { CoreServicesContext } from "../../components/core_services"; import ChannelNotification from "../../components/ChannelNotification"; import LegacyNotification from "../../components/LegacyNotification"; import { ERROR_NOTIFICATION_DOCUMENTATION_URL } from "../../utils/constants"; export interface ErrorNotificationProps { value?: IErrorNotification; onChange: (val: Required["value"]) => void; onChangeChannelId?: (value: string) => void; onChangeMessage?: (value: string) => void; browserServices: BrowserServices; } interface ErrorNotificationState { channels: FeatureChannelList[]; loadingChannels: boolean; } class ErrorNotification extends Component { static contextType = CoreServicesContext; constructor(props: ErrorNotificationProps) { super(props); this.state = { channels: [], loadingChannels: true, }; } componentDidMount = async (): Promise => { await this.getChannels(); }; getChannels = async (): Promise => { this.setState({ loadingChannels: true }); try { const { notificationService } = this.props.browserServices; const response = await notificationService.getChannels(); if (response.ok) { this.setState({ channels: response.response.channel_list }); } else { this.context.notifications.toasts.addDanger(`Could not load notification channels: ${response.error}`); } } catch (err) { this.context.notifications.toasts.addDanger(getErrorMessage(err, "Could not load the notification channels")); } this.setState({ loadingChannels: false }); }; onChangeChannelId = (e: ChangeEvent) => { const { onChange, value, onChangeChannelId } = this.props; const id = e.target.value; onChangeChannelId && onChangeChannelId(id); onChange({ ...value, channel: { id, }, }); }; onChangeMessage = (e: ChangeEvent) => { const { onChange, value, onChangeMessage } = this.props; const message = e.target.value; onChangeMessage && onChangeMessage(message); onChange({ ...value, message_template: { source: message, }, }); }; onSwitchToChannels = () => { const { onChange } = this.props; onChange({ channel: { id: "", }, message_template: { source: "", }, }); }; render() { const { value: errorNotification, onChange } = this.props; const { channels, loadingChannels } = this.state; const hasDestination = !!errorNotification?.destination; let content = ( ); // If we have a destination in the error notification then it's either an older policy or they created through the API if (hasDestination) { content = ; } return (

Error notification

– optional } titleSize="s" subTitleText={

You can set up an error notification for when a policy execution fails.{" "} Learn more

} >
{content}
); } } export default function ErrorNotificationContainer(props: Omit) { const browserServices = useContext(ServicesContext) as BrowserServices; return ; }