Skip to content

Commit

Permalink
Refactor nested ternary logic into useMemo fns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rylnd committed Jul 12, 2024
1 parent 3df635e commit a5fcf4d
Showing 1 changed file with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,30 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
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:
Expand Down

0 comments on commit a5fcf4d

Please sign in to comment.