/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { useCallback, useState } from 'react'; import { EuiButton, EuiEmptyPrompt, EuiFlexGroup, EuiFlexItem, EuiFlyoutBody, EuiFlyoutHeader, EuiInMemoryTable, EuiLoadingSpinner, EuiSpacer, EuiText, EuiTitle, } from '@elastic/eui'; import './styles.scss'; import { useColumns } from './helpers'; import { ConfirmUnlinkDetectorModal } from './ConfirmUnlinkModal'; import { deleteAlertingAugmentVisSavedObj } from '../../../utils/savedObjectHelper'; import { getNotifications } from '../../../services'; const AssociatedMonitors = ({ embeddable, closeFlyout, setFlyoutMode, monitors, isAssociateAllowed, limitReachedCallout, setAssociatedMonitors }) => { const title = embeddable.vis.title; const [modalState, setModalState] = useState(undefined); const notifications = getNotifications(); const onUnlink = useCallback((item) => { setModalState({ monitor: { name: item.name, id: item.id, }, }); }, []); const onEdit = useCallback( (item) => { window.open(`alerting#/monitors/${item.id}?action=update-monitor`, '_blank'); }, [] ); const onUnlinkMonitor = useCallback(async () => { try { await deleteAlertingAugmentVisSavedObj(embeddable.vis.id, modalState.monitor.id); notifications.toasts.addSuccess({ title: `Association removed between the ${modalState.monitor.name} monitor and the ${title} visualization`, text: "The monitor's alerts do not automatically appear on the visualization. Refresh your dashboard to update the visualization.", }); const newMonitors = monitors.filter((monitor) => monitor.id !== modalState.monitor.id); setAssociatedMonitors(newMonitors); } catch (e) { notifications.toasts.addDanger( `Failed to remove the association between the "${modalState.monitor.name}" monitor with the ${title} visualization. Failed due to ${e.message}.` ); } finally { handleHideModal(); } }, [closeFlyout, modalState]); const columns = useColumns({ onUnlink, onEdit }); const emptyMsg = ( No monitors to display} titleSize="s" body={`There are no alerting monitors associated with ${title} visualization.`} /> ); const noResultsMsg = ( No monitors to display} titleSize="s" body="There are no alerting monitors that match the search result." /> ); const loadingMsg = } />; const tableProps = { items: monitors || [], columns, search: { box: { disabled: !monitors || monitors.length === 0, incremental: true, schema: true, }, }, hasActions: true, pagination: true, sorting: true, message: monitors ? (monitors.length ? noResultsMsg : emptyMsg) : loadingMsg, loading: !monitors, }; const handleHideModal = useCallback(() => { setModalState(undefined); }, []); return (
{modalState ? ( ) : null}

Associated monitors

{!isAssociateAllowed && ( {limitReachedCallout} )}

Visualization: {title}

setFlyoutMode('existing')}> Associate a monitor
); }; export default AssociatedMonitors;