From a5fcf4d0ccd738a40746d099bd295484781825f5 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Thu, 11 Jul 2024 14:57:33 -0500 Subject: [PATCH] Refactor nested ternary logic into useMemo fns It would be painful to try to write a pure function that captures this same logic (see the deps lists), but as a compromise we can at least encapsulate these in functions and use more expressive implementation. --- .../components/step_define_rule/index.tsx | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/step_define_rule/index.tsx b/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/step_define_rule/index.tsx index 5f9e877e1c2dc..221f0cc7ee60c 100644 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/step_define_rule/index.tsx +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/step_define_rule/index.tsx @@ -483,17 +483,30 @@ const StepDefineRuleComponent: FC = ({ isEsqlSuppressionLoading || (isMlRule(ruleType) && (noMlJobsStarted || mlFieldsLoading || !mlSuppressionFields.length)); - const suppressionGroupByDisabledText = areSuppressionFieldsDisabledBySequence - ? i18n.EQL_SEQUENCE_SUPPRESSION_DISABLE_TOOLTIP - : isMlRule(ruleType) && noMlJobsStarted - ? i18n.MACHINE_LEARNING_SUPPRESSION_DISABLED_LABEL - : alertSuppressionUpsellingMessage; - - const suppressionGroupByFields = isEsqlRule(ruleType) - ? esqlSuppressionFields - : isMlRule(ruleType) - ? mlSuppressionFields - : termsAggregationFields; + const suppressionGroupByDisabledText = useMemo(() => { + if (areSuppressionFieldsDisabledBySequence) { + return i18n.EQL_SEQUENCE_SUPPRESSION_DISABLE_TOOLTIP; + } else if (isMlRule(ruleType) && noMlJobsStarted) { + return i18n.MACHINE_LEARNING_SUPPRESSION_DISABLED_LABEL; + } else { + return alertSuppressionUpsellingMessage; + } + }, [ + alertSuppressionUpsellingMessage, + areSuppressionFieldsDisabledBySequence, + noMlJobsStarted, + ruleType, + ]); + + const suppressionGroupByFields = useMemo(() => { + if (isEsqlRule(ruleType)) { + return esqlSuppressionFields; + } else if (isMlRule(ruleType)) { + return mlSuppressionFields; + } else { + return termsAggregationFields; + } + }, [esqlSuppressionFields, mlSuppressionFields, ruleType, termsAggregationFields]); /** * Component that allows selection of suppression intervals disabled: