[8.12] [Custom threshold] Fix bug with deleting conditions (#172187) (#173036)

# Backport

This will backport the following commits from `main` to `8.12`:
- [[Custom threshold] Fix bug with deleting conditions
(#172187)](https://github.com/elastic/kibana/pull/172187)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Maryam
Saeidi","email":"maryam.saeidi@elastic.co"},"sourceCommit":{"committedDate":"2023-12-11T11:24:18Z","message":"[Custom
threshold] Fix bug with deleting conditions (#172187)\n\nFixes
#171623\r\n\r\n## Summary\r\n\r\nThis PR fixes custom threshold updates
by making sure that aggregation\r\nand equation will re-render if the
expression changes. The chart and\r\nthreshold updates were correct
during
deletion.","sha":"75b52e865441d7f2a733f0b9aa78792acc0016c6","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:prev-minor","Team:obs-ux-management","Feature:
Custom
threshold","v8.13.0"],"number":172187,"url":"https://github.com/elastic/kibana/pull/172187","mergeCommit":{"message":"[Custom
threshold] Fix bug with deleting conditions (#172187)\n\nFixes
#171623\r\n\r\n## Summary\r\n\r\nThis PR fixes custom threshold updates
by making sure that aggregation\r\nand equation will re-render if the
expression changes. The chart and\r\nthreshold updates were correct
during
deletion.","sha":"75b52e865441d7f2a733f0b9aa78792acc0016c6"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/172187","number":172187,"mergeCommit":{"message":"[Custom
threshold] Fix bug with deleting conditions (#172187)\n\nFixes
#171623\r\n\r\n## Summary\r\n\r\nThis PR fixes custom threshold updates
by making sure that aggregation\r\nand equation will re-render if the
expression changes. The chart and\r\nthreshold updates were correct
during deletion.","sha":"75b52e865441d7f2a733f0b9aa78792acc0016c6"}}]}]
BACKPORT-->

Co-authored-by: Maryam Saeidi <maryam.saeidi@elastic.co>
This commit is contained in:
Kibana Machine 2023-12-11 07:47:37 -05:00 committed by GitHub
parent 93cff0aacd
commit 16613281b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,7 +16,7 @@ import {
EuiExpression,
EuiPopover,
} from '@elastic/eui';
import React, { useState, useCallback, useMemo } from 'react';
import React, { useState, useCallback, useMemo, useEffect } from 'react';
import { range, first, xor, debounce } from 'lodash';
import { IErrorObject } from '@kbn/triggers-actions-ui-plugin/public';
import { FormattedMessage } from '@kbn/i18n-react';
@ -64,9 +64,14 @@ export function CustomEquationEditor({
expression?.metrics ?? [NEW_METRIC]
);
const [customEqPopoverOpen, setCustomEqPopoverOpen] = useState(false);
const [equation, setEquation] = useState<string | undefined>(expression?.equation || undefined);
const [equation, setEquation] = useState<string | undefined>(expression?.equation);
const debouncedOnChange = useMemo(() => debounce(onChange, 500), [onChange]);
useEffect(() => {
setCustomMetrics(expression?.metrics ?? [NEW_METRIC]);
setEquation(expression?.equation);
}, [expression?.metrics, expression?.equation]);
const handleAddNewRow = useCallback(() => {
setCustomMetrics((previous) => {
const currentVars = previous?.map((m) => m.name) ?? [];