import { Control, Controller, UseFormSetValue, useWatch, } from 'react-hook-form'; import { AlertThresholdType, isRangeThresholdType, } from '@hyperdx/common-utils/dist/types'; import { ActionIcon, Alert, Badge, Box, Collapse, Group, NativeSelect, NumberInput, Paper, Text, Tooltip, UnstyledButton, } from '@mantine/core'; import { useDisclosure } from '@mantine/hooks'; import { IconChevronDown, IconHelpCircle, IconInfoCircleFilled, IconTrash, } from '@tabler/icons-react'; import api from '@/api'; import { AlertChannelForm } from '@/components/Alerts'; import { AckAlert } from '@/components/alerts/AckAlert'; import { AlertHistoryCardList } from '@/components/alerts/AlertHistoryCards'; import { AlertScheduleFields } from '@/components/AlertScheduleFields'; import { ChartEditorFormState } from '@/components/ChartEditor/types'; import { optionsToSelectData } from '@/utils'; import { ALERT_CHANNEL_OPTIONS, intervalToMinutes, TILE_ALERT_INTERVAL_OPTIONS, TILE_ALERT_THRESHOLD_TYPE_OPTIONS, } from '@/utils/alerts'; export function TileAlertEditor({ control, setValue, alert, onRemove, error, warning, tooltip, }: { control: Control; setValue: UseFormSetValue; alert: NonNullable; onRemove: () => void; error?: string; warning?: string; tooltip?: string; }) { const [opened, { toggle }] = useDisclosure(true); const alertChannelType = useWatch({ control, name: 'alert.channel.type' }); const alertThresholdType = useWatch({ control, name: 'alert.thresholdType' }); const alertThreshold = useWatch({ control, name: 'alert.threshold' }); const alertThresholdMax = useWatch({ control, name: 'alert.thresholdMax' }); const alertScheduleOffsetMinutes = useWatch({ control, name: 'alert.scheduleOffsetMinutes', }); const maxAlertScheduleOffsetMinutes = alert?.interval ? Math.max(intervalToMinutes(alert.interval) - 1, 0) : 0; const alertIntervalLabel = alert?.interval ? TILE_ALERT_INTERVAL_OPTIONS[alert.interval] : undefined; const { data: alertData } = api.useAlert(alert.id); const alertItem = alertData?.data; return ( Alert {tooltip && ( )} {error && ( Invalid Query )} {warning && ( Warning )} {alertItem && alertItem.history.length > 0 && ( )} {alertItem && } Trigger when the value ( { field.onChange(e); if ( isRangeThresholdType(e.currentTarget.value) && alertThresholdMax == null ) { setValue('alert.thresholdMax', (alertThreshold ?? 0) + 1); } }} /> )} /> ( )} /> {isRangeThresholdType(alertThresholdType as AlertThresholdType) && ( <> and ( )} /> )} over ( )} /> window via ( )} /> {alert?.createdBy && ( Created by {alert.createdBy.name || alert.createdBy.email} )} Send to {(alertThresholdType === AlertThresholdType.EQUAL || alertThresholdType === AlertThresholdType.NOT_EQUAL) && ( } color="gray" py="xs" mt="md" > Note: Floating-point query results are not rounded during equality comparison. )} ); }