import './styles.scss'; import { Button, Select, Typography } from 'antd'; import getAllChannels from 'api/channels/getAll'; import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder'; import { Plus } from 'lucide-react'; import { useQuery } from 'react-query'; import { SuccessResponseV2 } from 'types/api'; import { Channels } from 'types/api/channels/getAll'; import APIError from 'types/api/error'; import { useCreateAlertState } from '../context'; import { INITIAL_INFO_THRESHOLD, INITIAL_RANDOM_THRESHOLD, INITIAL_WARNING_THRESHOLD, THRESHOLD_MATCH_TYPE_OPTIONS, THRESHOLD_OPERATOR_OPTIONS, } from '../context/constants'; import ThresholdItem from './ThresholdItem'; import { UpdateThreshold } from './types'; import { getCategoryByOptionId, getCategorySelectOptionByName, getQueryNames, } from './utils'; function AlertThreshold(): JSX.Element { const { alertState, thresholdState, setThresholdState, } = useCreateAlertState(); const { data, isLoading: isLoadingChannels } = useQuery< SuccessResponseV2, APIError >(['getChannels'], { queryFn: () => getAllChannels(), }); const channels = data?.data || []; const { currentQuery } = useQueryBuilder(); const queryNames = getQueryNames(currentQuery); const selectedCategory = getCategoryByOptionId(alertState.yAxisUnit || ''); const categorySelectOptions = getCategorySelectOptionByName( selectedCategory || '', ); const addThreshold = (): void => { let newThreshold; if (thresholdState.thresholds.length === 1) { newThreshold = INITIAL_WARNING_THRESHOLD; } else if (thresholdState.thresholds.length === 2) { newThreshold = INITIAL_INFO_THRESHOLD; } else { newThreshold = INITIAL_RANDOM_THRESHOLD; } setThresholdState({ type: 'SET_THRESHOLDS', payload: [...thresholdState.thresholds, newThreshold], }); }; const removeThreshold = (id: string): void => { if (thresholdState.thresholds.length > 1) { setThresholdState({ type: 'SET_THRESHOLDS', payload: thresholdState.thresholds.filter((t) => t.id !== id), }); } }; const updateThreshold: UpdateThreshold = (id, field, value) => { setThresholdState({ type: 'SET_THRESHOLDS', payload: thresholdState.thresholds.map((t) => t.id === id ? { ...t, [field]: value } : t, ), }); }; return (
{/* Main condition sentence */}
Send a notification when { setThresholdState({ type: 'SET_OPERATOR', payload: value, }); }} style={{ width: 120 }} options={THRESHOLD_OPERATOR_OPTIONS} /> the threshold(s)