2025-09-09 14:56:29 +07:00
|
|
|
import './styles.scss';
|
|
|
|
|
|
2025-09-15 22:52:07 +07:00
|
|
|
import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';
|
|
|
|
|
import { useCallback, useMemo } from 'react';
|
2025-09-09 14:56:29 +07:00
|
|
|
import { Labels } from 'types/api/alerts/def';
|
|
|
|
|
|
|
|
|
|
import { useCreateAlertState } from '../context';
|
|
|
|
|
import LabelsInput from './LabelsInput';
|
|
|
|
|
|
|
|
|
|
function CreateAlertHeader(): JSX.Element {
|
|
|
|
|
const { alertState, setAlertState } = useCreateAlertState();
|
|
|
|
|
|
2025-09-15 22:52:07 +07:00
|
|
|
const { currentQuery } = useQueryBuilder();
|
|
|
|
|
|
|
|
|
|
const groupByLabels = useMemo(() => {
|
|
|
|
|
const labels = new Array<string>();
|
|
|
|
|
currentQuery.builder.queryData.forEach((query) => {
|
|
|
|
|
query.groupBy.forEach((groupBy) => {
|
|
|
|
|
labels.push(groupBy.key);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return labels;
|
|
|
|
|
}, [currentQuery]);
|
|
|
|
|
|
|
|
|
|
// If the label key is a group by label, then it is not allowed to be used as a label key
|
|
|
|
|
const validateLabelsKey = useCallback(
|
|
|
|
|
(key: string): string | null => {
|
|
|
|
|
if (groupByLabels.includes(key)) {
|
|
|
|
|
return `Cannot use ${key} as a key`;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
[groupByLabels],
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-09 14:56:29 +07:00
|
|
|
return (
|
|
|
|
|
<div className="alert-header">
|
|
|
|
|
<div className="alert-header__tab-bar">
|
|
|
|
|
<div className="alert-header__tab">New Alert Rule</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="alert-header__content">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={alertState.name}
|
|
|
|
|
onChange={(e): void =>
|
|
|
|
|
setAlertState({ type: 'SET_ALERT_NAME', payload: e.target.value })
|
|
|
|
|
}
|
|
|
|
|
className="alert-header__input title"
|
|
|
|
|
placeholder="Enter alert rule name"
|
|
|
|
|
/>
|
|
|
|
|
<LabelsInput
|
|
|
|
|
labels={alertState.labels}
|
|
|
|
|
onLabelsChange={(labels: Labels): void =>
|
|
|
|
|
setAlertState({ type: 'SET_ALERT_LABELS', payload: labels })
|
|
|
|
|
}
|
2025-09-15 22:52:07 +07:00
|
|
|
validateLabelsKey={validateLabelsKey}
|
2025-09-09 14:56:29 +07:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default CreateAlertHeader;
|