diff --git a/public/app/features/alerting/unified/components/rule-editor/RuleEditorSection.tsx b/public/app/features/alerting/unified/components/rule-editor/RuleEditorSection.tsx index a020e50542f..fe92306e403 100644 --- a/public/app/features/alerting/unified/components/rule-editor/RuleEditorSection.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/RuleEditorSection.tsx @@ -37,6 +37,7 @@ export const RuleEditorSection = ({ {switchMode && ( { @@ -151,6 +152,15 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P return queries.filter((query) => isExpressionQueryInAlert(query)); }, [queries]); + useEffect(() => { + // we only remove or add the reducer(optimize reducer) expression when creating a new alert. + // When editing an alert, we assume the user wants to manually adjust expressions and queries for more control and customization. + + if (!editingExistingRule && isOptimizeReducerEnabled) { + dispatch(optimizeReduceExpression({ updatedQueries: dataQueries, expressionQueries })); + } + }, [dataQueries, expressionQueries, editingExistingRule, isOptimizeReducerEnabled]); + const [type, condition, dataSourceName, editorSettings] = watch([ 'type', 'condition', @@ -265,7 +275,6 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P ); const updateExpressionAndDatasource = useSetExpressionAndDataSource(); - const isOptimizeReducerEnabled = config.featureToggles.alertingUIOptimizeReducer ?? false; const onChangeQueries = useCallback( (updatedQueries: AlertQuery[]) => { @@ -279,12 +288,6 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P setValue('queries', [...updatedQueries, ...expressionQueries], { shouldValidate: false }); updateExpressionAndDatasource(updatedQueries); - // we only remove or add the reducer(optimize reducer) expression when creating a new alert. - // When editing an alert, we assume the user wants to manually adjust expressions and queries for more control and customization. - if (!editingExistingRule && isOptimizeReducerEnabled) { - dispatch(optimizeReduceExpression({ updatedQueries, expressionQueries })); - } - dispatch(setDataQueries(updatedQueries)); dispatch(updateExpressionTimeRange()); @@ -294,7 +297,7 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange }: P dispatch(rewireExpressions({ oldRefId, newRefId })); } }, - [queries, updateExpressionAndDatasource, getValues, setValue, editingExistingRule, isOptimizeReducerEnabled] + [queries, updateExpressionAndDatasource, getValues, setValue] ); const onChangeRecordingRulesQueries = useCallback(