@@ -40,19 +40,12 @@ export const useSyntheticsRules = (isOpen: boolean) => {
4040 const alertFlyoutVisible = useSelector ( selectAlertFlyoutVisibility ) ;
4141 const isNewRule = useSelector ( selectIsNewRule ) ;
4242 const { settings } = useSelector ( selectDynamicSettings ) ;
43- const defaultRulesEnabled =
44- settings && ( settings ?. defaultStatusRuleEnabled || settings ?. defaultTLSRuleEnabled ) ;
45- // Fetch default rules when popover opens if they haven't been loaded yet
46- useEffect ( ( ) => {
47- if ( isOpen && ! loading && rulesLoaded === null && defaultRulesEnabled ) {
48- dispatch ( getDefaultAlertingAction . get ( ) ) ;
49- }
50- } , [ isOpen , loading , rulesLoaded , dispatch , defaultRulesEnabled ] ) ;
5143 const { canSave } = useSyntheticsSettingsContext ( ) ;
52-
5344 const { loaded, data : monitors } = useSelector ( selectMonitorListState ) ;
5445
5546 const hasMonitors = loaded && monitors . absoluteTotal && monitors . absoluteTotal > 0 ;
47+ const defaultRulesEnabled =
48+ settings && ( settings ?. defaultStatusRuleEnabled || settings ?. defaultTLSRuleEnabled ) ;
5649
5750 const getOrCreateAlerts = useCallback ( ( ) => {
5851 if ( canSave ) {
@@ -62,20 +55,23 @@ export const useSyntheticsRules = (isOpen: boolean) => {
6255 }
6356 } , [ canSave , dispatch ] ) ;
6457
58+ // Fetch or create default rules when popover opens
6559 useEffect ( ( ) => {
66- if ( hasMonitors && defaultRulesEnabled ) {
67- if ( ! defaultRules ) {
68- // on initial load we prioritize loading the app
69- setTimeout ( ( ) => {
70- getOrCreateAlerts ( ) ;
71- } , 1000 ) ;
72- } else {
60+ if ( isOpen && hasMonitors && defaultRulesEnabled && ! loading ) {
61+ // If rules haven't been loaded yet, fetch/create them
62+ if ( rulesLoaded === null || ! defaultRules ) {
7363 getOrCreateAlerts ( ) ;
7464 }
7565 }
76- // we don't want to run this on defaultRules change
77- // eslint-disable-next-line react-hooks/exhaustive-deps
78- } , [ dispatch , isOpen , hasMonitors , defaultRulesEnabled ] ) ;
66+ } , [
67+ isOpen ,
68+ hasMonitors ,
69+ defaultRulesEnabled ,
70+ loading ,
71+ rulesLoaded ,
72+ defaultRules ,
73+ getOrCreateAlerts ,
74+ ] ) ;
7975
8076 const {
8177 triggersActionsUi : { ruleTypeRegistry, actionTypeRegistry } ,
@@ -85,11 +81,19 @@ export const useSyntheticsRules = (isOpen: boolean) => {
8581 const onClose = useMemo ( ( ) => ( ) => dispatch ( setAlertFlyoutVisible ( null ) ) , [ dispatch ] ) ;
8682
8783 const EditAlertFlyout = useMemo ( ( ) => {
84+ // Don't render if this is a new rule flyout
85+ if ( isNewRule || ! alertFlyoutVisible ) {
86+ return null ;
87+ }
88+
8889 const initialRule =
8990 alertFlyoutVisible === SYNTHETICS_TLS_RULE ? defaultRules ?. tlsRule : defaultRules ?. statusRule ;
90- if ( ! initialRule || isNewRule ) {
91+
92+ // If the rule doesn't exist yet, return null (the useEffect will try to fetch/create it)
93+ if ( ! initialRule ) {
9194 return null ;
9295 }
96+
9397 return (
9498 < RuleFormFlyout
9599 plugins = { { ...plugins , ruleTypeRegistry, actionTypeRegistry } }
@@ -134,7 +138,7 @@ export const useSyntheticsRules = (isOpen: boolean) => {
134138 } , [ isNewRule , alertFlyoutVisible , plugins , ruleTypeRegistry , actionTypeRegistry , onClose ] ) ;
135139
136140 return useMemo (
137- ( ) => ( { loading, EditAlertFlyout, NewRuleFlyout } ) ,
138- [ EditAlertFlyout , loading , NewRuleFlyout ]
141+ ( ) => ( { loading, EditAlertFlyout, NewRuleFlyout, defaultRules } ) ,
142+ [ EditAlertFlyout , loading , NewRuleFlyout , defaultRules ]
139143 ) ;
140144} ;
0 commit comments