diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule.ts index f16858edf0da0..0579dc6fc93cf 100644 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule.ts +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule.ts @@ -4,8 +4,9 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { UseQueryOptions } from '@tanstack/react-query'; import { useAppToasts } from '../../../common/hooks/use_app_toasts'; +import type { RuleResponse } from '../../../../common/api/detection_engine'; import { useFetchRuleByIdQuery } from '../api/hooks/use_fetch_rule_by_id_query'; import * as i18n from './translations'; @@ -13,19 +14,27 @@ import * as i18n from './translations'; * Hook for using to get a Rule from the Detection Engine API * * @param id desired Rule ID's (not rule_id) - * + * @param showToast whether to show toasts on error + * @param options options for the useQuery */ -export const useRule = (id: string, showToast = true) => { +export const useRule = ( + id: string, + showToast = true, + options: UseQueryOptions = {} +) => { const { addError } = useAppToasts(); + let fetchRuleOptions = { + ...options, + }; + + if (showToast) { + fetchRuleOptions = { + ...fetchRuleOptions, + onError: (error) => { + addError(error, { title: i18n.RULE_AND_TIMELINE_FETCH_FAILURE }); + }, + }; + } - return useFetchRuleByIdQuery( - id, - showToast - ? { - onError: (error) => { - addError(error, { title: i18n.RULE_AND_TIMELINE_FETCH_FAILURE }); - }, - } - : undefined - ); + return useFetchRuleByIdQuery(id, fetchRuleOptions); }; diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.ts index 4df58eb3f70f4..aa423f859f754 100644 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.ts +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_management/logic/use_rule_with_fallback.ts @@ -78,7 +78,14 @@ const buildLastAlertQuery = (ruleId: string) => ({ * In that case, try to fetch the latest alert generated by the rule and retrieve the rule data from the alert (fallback). */ export const useRuleWithFallback = (ruleId: string): UseRuleWithFallback => { - const { isFetching: ruleLoading, data: ruleData, error, refetch } = useRule(ruleId, false); + const { + isFetching: ruleLoading, + data: ruleData, + error, + refetch, + } = useRule(ruleId, false, { + refetchOnWindowFocus: false, + }); const { addError } = useAppToasts(); const isExistingRule = !isNotFoundError(error); diff --git a/x-pack/plugins/security_solution/public/detections/components/alerts_table/timeline_actions/alert_context_menu.tsx b/x-pack/plugins/security_solution/public/detections/components/alerts_table/timeline_actions/alert_context_menu.tsx index 2aa984314bd78..33ef9ad005881 100644 --- a/x-pack/plugins/security_solution/public/detections/components/alerts_table/timeline_actions/alert_context_menu.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/alerts_table/timeline_actions/alert_context_menu.tsx @@ -431,6 +431,8 @@ export const AddExceptionFlyoutWrapper: React.FC enrichedAlert == null || (memoRuleIndices == null && memoDataViewId == null); + if (isLoading || isRuleLoading) return null; + return (