import { EuiLink } from '@elastic/eui' ; import React from 'react' ; import { ALERT_DURATION , ALERT_SEVERITY , ALERT_STATUS , ALERT_STATUS_ACTIVE , ALERT_STATUS_RECOVERED , ALERT_REASON , TIMESTAMP , } from '@kbn/rule-data-utils' ; import type { CellValueElementProps , TimelineNonEcsData } from '../../../../../../timelines/common' ; import { AlertStatusIndicator } from '../../../../components/shared/alert_status_indicator' ; import { TimestampTooltip } from '../../../../components/shared/timestamp_tooltip' ; import { asDuration } from '../../../../../common/utils/formatters' ; import { SeverityBadge } from '../severity_badge' ; import { TopAlert } from '../../' ; import { parseAlert } from '../parse_alert' ; import { usePluginContext } from '../../../../hooks/use_plugin_context' ; export const getMappedNonEcsValue = ( { data , fieldName , } ) : string [ ] | undefined => { const item = data . find ( ( d ) => d . field === fieldName ) ; if ( item != null && item . value != null ) { return item . value ; } return undefined ; } ; /**
 * This implementation of `EuiDataGrid`'s `renderCellValue`
 * accepts `EuiDataGridCellValueElementProps`, plus `data`
 * from the TGrid
 */ export const getRenderCellValue = ( { setFlyoutAlert , } ) => { return ( { columnId , data } ) => { const { observabilityRuleTypeRegistry } = usePluginContext ( ) ; const value = getMappedNonEcsValue ( { data , fieldName : columnId , } ) ?. reduce ( ( x ) => x [ 0 ] ) ; switch ( columnId ) { case ALERT_STATUS : if ( value !== ALERT_STATUS_ACTIVE && value !== ALERT_STATUS_RECOVERED ) { return null ; } return < AlertStatusIndicator alertStatus = { value } />; case TIMESTAMP : return < TimestampTooltip time = { new Date ( value ?? '' ) . getTime ( ) } timeUnit = "milliseconds" / > ; case ALERT_DURATION : return asDuration ( Number ( value ) ) ; case ALERT_SEVERITY : return < SeverityBadge severityLevel = { value ?? undefined } />; case ALERT_REASON : const dataFieldEs = data . reduce ( ( acc , d ) => ( { ... acc , [ d . field ] : d . value } ) , { } ) ; const alert = parseAlert ( observabilityRuleTypeRegistry ) ( dataFieldEs ) ; return ( < EuiLink href = "" onClick = { ( ) => setFlyoutAlert && setFlyoutAlert ( alert ) } > { alert . reason } < /EuiLink>); default : return < > { value } < />; } } ; } ;