From d9e9d52028a745a96b17b90b42027f82950e5f82 Mon Sep 17 00:00:00 2001 From: laurenashleigh Date: Wed, 22 Oct 2025 15:21:46 +0100 Subject: [PATCH] refactoring and fixing --- .../components/AlertRuleDrawerForm.tsx | 81 ++++++++++++++++++- .../components/RuleDefinitionSection.tsx | 3 +- .../NewRuleFromPanelButton.tsx | 65 +++++++-------- .../rule-editor/labels/LabelsField.tsx | 5 +- .../rule-editor/labels/LabelsFieldInForm.tsx | 9 ++- .../PanelDataPane/NewAlertRuleButton.tsx | 7 +- 6 files changed, 126 insertions(+), 44 deletions(-) diff --git a/public/app/features/alerting/unified/components/AlertRuleDrawerForm.tsx b/public/app/features/alerting/unified/components/AlertRuleDrawerForm.tsx index c72b2a5feeb..440579a0468 100644 --- a/public/app/features/alerting/unified/components/AlertRuleDrawerForm.tsx +++ b/public/app/features/alerting/unified/components/AlertRuleDrawerForm.tsx @@ -1,13 +1,19 @@ import { css } from '@emotion/css'; +import { useEffect, useMemo } from 'react'; import { FormProvider, useForm } from 'react-hook-form'; import { GrafanaTheme2 } from '@grafana/data'; import { t } from '@grafana/i18n'; -import { Drawer, useStyles2 } from '@grafana/ui'; +import { Button, Drawer, Stack, useStyles2 } from '@grafana/ui'; +import { useAppNotification } from 'app/core/copy/appNotification'; import { RuleDefinitionSection } from 'app/features/alerting/unified/components/RuleDefinitionSection'; +import { isGrafanaGroupUpdatedResponse } from '../api/alertRuleModel'; +import { useAddRuleToRuleGroup } from '../hooks/ruleGroup/useUpsertRuleFromRuleGroup'; import { getDefaultFormValues } from '../rule-editor/formDefaults'; import { RuleFormType, RuleFormValues } from '../types/rule-form'; +import { formValuesToRulerGrafanaRuleDTO } from '../utils/rule-form'; +import { getRuleGroupLocationFromFormValues } from '../utils/rules'; import { RuleConditionSection } from './RuleConditionSection'; import { RuleNotificationSection } from './RuleNotificationSection'; @@ -16,16 +22,53 @@ export interface AlertRuleDrawerFormProps { isOpen: boolean; onClose: () => void; title?: string; + onContinueInAlerting?: () => void; + prefill?: Partial; } -export function AlertRuleDrawerForm({ isOpen, onClose, title }: AlertRuleDrawerFormProps) { - const methods = useForm({ defaultValues: getDefaultFormValues(RuleFormType.grafana) }); +export function AlertRuleDrawerForm({ isOpen, onClose, title, onContinueInAlerting, prefill }: AlertRuleDrawerFormProps) { + const baseDefaults = useMemo(() => getDefaultFormValues(RuleFormType.grafana), []); + const methods = useForm({ + defaultValues: prefill ? { ...baseDefaults, ...(prefill as Partial) } : baseDefaults, + }); const styles = useStyles2(getStyles); + const [addRuleToRuleGroup] = useAddRuleToRuleGroup(); + const notifyApp = useAppNotification(); + + // Keep form in sync if prefill changes between openings + useEffect(() => { + if (prefill) { + methods.reset({ ...baseDefaults, ...(prefill as Partial) }); + } + }, [prefill, methods]); if (!isOpen) { return null; } + const submit = async (values: RuleFormValues) => { + try { + const groupName = values.group && values.group.trim().length > 0 ? values.group : (values.name?.trim() || 'default'); + const effectiveValues: RuleFormValues = { ...values, group: groupName }; + + const dto = formValuesToRulerGrafanaRuleDTO(effectiveValues); + const groupIdentifier = getRuleGroupLocationFromFormValues(effectiveValues); + const result = await addRuleToRuleGroup.execute(groupIdentifier, dto, effectiveValues.evaluateEvery); + if (isGrafanaGroupUpdatedResponse(result)) { + onClose(); + return; + } + notifyApp.error('Failed to create rule', 'The rule was not created. Please review the form and try again.'); + } catch (err: any) { + const msg = err?.data?.message || err?.message || 'Unknown error while creating the rule.'; + notifyApp.error('Failed to create rule', msg); + } + }; + + const onInvalid = () => { + notifyApp.error('There are errors in the form. Please correct them and try again!'); + }; + return (