From 5721019573c5578ee9fc7ebf99e42ca583fef2f4 Mon Sep 17 00:00:00 2001 From: Domas Date: Tue, 18 May 2021 19:14:57 +0300 Subject: [PATCH] Alerting: various rule form fixes (#34272) --- .../grafana-ui/src/components/Forms/Field.tsx | 24 ++++- .../alerting/unified/PanelAlertTabContent.tsx | 2 +- .../features/alerting/unified/RuleList.tsx | 4 +- .../components/rule-editor/AlertRuleForm.tsx | 6 +- .../components/rule-editor/AlertTypeStep.tsx | 30 +++---- .../rule-editor/AnnotationsField.tsx | 90 ++++++++++--------- .../components/rule-editor/ConditionsStep.tsx | 39 ++++++-- .../rule-editor/GrafanaAlertStatePicker.tsx | 17 +++- .../rule-editor/GroupAndNamespaceFields.tsx | 31 +++++-- .../components/rule-editor/QueryStep.tsx | 4 +- .../rule-editor/RuleEditorSection.tsx | 37 +++++--- ...mOrApplicationRules.tsx => CloudRules.tsx} | 4 +- .../{ThresholdRules.tsx => GrafanaRules.tsx} | 4 +- .../components/rules/RuleListGroupView.tsx | 10 +-- .../alerting/unified/state/actions.ts | 6 +- .../alerting/unified/types/rule-form.ts | 8 +- .../alerting/unified/utils/rule-form.ts | 18 ++-- 17 files changed, 211 insertions(+), 123 deletions(-) rename public/app/features/alerting/unified/components/rules/{SystemOrApplicationRules.tsx => CloudRules.tsx} (94%) rename public/app/features/alerting/unified/components/rules/{ThresholdRules.tsx => GrafanaRules.tsx} (94%) diff --git a/packages/grafana-ui/src/components/Forms/Field.tsx b/packages/grafana-ui/src/components/Forms/Field.tsx index 92b983f19d0..b9ae14679c7 100644 --- a/packages/grafana-ui/src/components/Forms/Field.tsx +++ b/packages/grafana-ui/src/components/Forms/Field.tsx @@ -25,6 +25,9 @@ export interface FieldProps extends HTMLAttributes { error?: string | null; /** Indicates horizontal layout of the field */ horizontal?: boolean; + /** make validation message overflow horizontally. Prevents pushing out adjacent inline components */ + validationMessageHorizontalOverflow?: boolean; + className?: string; } @@ -46,6 +49,14 @@ export const getFieldStyles = stylesFactory((theme: GrafanaTheme2) => { fieldValidationWrapperHorizontal: css` flex: 1 1 100%; `, + validationMessageHorizontalOverflow: css` + width: 0; + overflow-x: visible; + + & > * { + white-space: nowrap; + } + `, }; }); @@ -60,6 +71,7 @@ export const Field: React.FC = ({ error, children, className, + validationMessageHorizontalOverflow, ...otherProps }) => { const theme = useTheme2(); @@ -81,14 +93,22 @@ export const Field: React.FC = ({
{React.cloneElement(children, { invalid, disabled, loading })} {invalid && error && !horizontal && ( -
+
{error}
)}
{invalid && error && horizontal && ( -
+
{error}
)} diff --git a/public/app/features/alerting/unified/PanelAlertTabContent.tsx b/public/app/features/alerting/unified/PanelAlertTabContent.tsx index 8cef6ef04d5..6c81cc9a9ce 100644 --- a/public/app/features/alerting/unified/PanelAlertTabContent.tsx +++ b/public/app/features/alerting/unified/PanelAlertTabContent.tsx @@ -23,7 +23,7 @@ export const PanelAlertTabContent: FC = ({ dashboard, panel }) => { const alert = errors.length ? ( {errors.map((error, index) => ( -
Failed to load Grafana threshold rules state: {error.message || 'Unknown error.'}
+
Failed to load Grafana rules state: {error.message || 'Unknown error.'}
))}
) : null; diff --git a/public/app/features/alerting/unified/RuleList.tsx b/public/app/features/alerting/unified/RuleList.tsx index 14e6f25076d..01afe507a1e 100644 --- a/public/app/features/alerting/unified/RuleList.tsx +++ b/public/app/features/alerting/unified/RuleList.tsx @@ -93,10 +93,10 @@ export const RuleList = withErrorBoundary( {(promReqeustErrors.length || rulerRequestErrors.length || grafanaPromError) && ( {grafanaPromError && ( -
Failed to load Grafana threshold rules state: {grafanaPromError.message || 'Unknown error.'}
+
Failed to load Grafana rules state: {grafanaPromError.message || 'Unknown error.'}
)} {grafanaRulerError && ( -
Failed to load Grafana threshold rules config: {grafanaRulerError.message || 'Unknown error.'}
+
Failed to load Grafana rules config: {grafanaRulerError.message || 'Unknown error.'}
)} {promReqeustErrors.map(({ dataSource, error }) => (
diff --git a/public/app/features/alerting/unified/components/rule-editor/AlertRuleForm.tsx b/public/app/features/alerting/unified/components/rule-editor/AlertRuleForm.tsx index 735102cd65a..fd636ce013c 100644 --- a/public/app/features/alerting/unified/components/rule-editor/AlertRuleForm.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/AlertRuleForm.tsx @@ -58,7 +58,7 @@ export const AlertRuleForm: FC = ({ existing }) => { const type = watch('type'); const dataSourceName = watch('dataSourceName'); - const showStep2 = Boolean(type && (type === RuleFormType.threshold || !!dataSourceName)); + const showStep2 = Boolean(type && (type === RuleFormType.grafana || !!dataSourceName)); const submitState = useUnifiedAlertingSelector((state) => state.ruleForm.saveRule) || initialAsyncRequestState; useCleanup((state) => state.unifiedAlerting.ruleForm.saveRule); @@ -69,7 +69,7 @@ export const AlertRuleForm: FC = ({ existing }) => { values: { ...defaultValues, ...values, - annotations: values.annotations?.filter(({ key }) => !!key) ?? [], + annotations: values.annotations?.filter(({ key, value }) => !!key && !!value) ?? [], labels: values.labels?.filter(({ key }) => !!key) ?? [], }, existing, @@ -80,7 +80,7 @@ export const AlertRuleForm: FC = ({ existing }) => { return ( -
submit(values, false))} className={styles.form}> + e.preventDefault()} className={styles.form}>
- {ruleFormType === RuleFormType.system && dataSourceName && ( + {ruleFormType === RuleFormType.cloud && dataSourceName && ( )} - {ruleFormType === RuleFormType.threshold && ( + {ruleFormType === RuleFormType.grafana && ( = ({ editingExistingRule }) => { ); }; -const getStyles = (theme: GrafanaTheme) => ({ +const getStyles = (theme: GrafanaTheme2) => ({ formInput: css` width: 330px; & + & { - margin-left: ${theme.spacing.sm}; + margin-left: ${theme.spacing(3)}; } `, flexRow: css` diff --git a/public/app/features/alerting/unified/components/rule-editor/AnnotationsField.tsx b/public/app/features/alerting/unified/components/rule-editor/AnnotationsField.tsx index 58fbbc51a24..4841452dccf 100644 --- a/public/app/features/alerting/unified/components/rule-editor/AnnotationsField.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/AnnotationsField.tsx @@ -1,5 +1,5 @@ import React, { FC, useCallback } from 'react'; -import { Button, Field, FieldArray, InputControl, Label, TextArea, useStyles } from '@grafana/ui'; +import { Button, Field, FieldArray, Input, InputControl, Label, TextArea, useStyles } from '@grafana/ui'; import { GrafanaTheme } from '@grafana/data'; import { css, cx } from '@emotion/css'; import { useFormContext } from 'react-hook-form'; @@ -28,53 +28,55 @@ const AnnotationsField: FC = () => { {({ fields, append, remove }) => { return (
- {fields.map((field, index) => ( -
- - ( - - )} - control={control} - rules={{ required: { value: !!annotations[index]?.value, message: 'Required.' } }} + {fields.map((field, index) => { + const isUrl = annotations[index]?.key?.toLocaleLowerCase().endsWith('url'); + const ValueInputComponent = isUrl ? Input : TextArea; + return ( +
+ + ( + + )} + control={control} + rules={{ required: { value: !!annotations[index]?.value, message: 'Required.' } }} + /> + + + + +