66 */
77
88import { useDispatch , useSelector } from 'react-redux' ;
9- import React , { useMemo } from 'react' ;
9+ import React , { useCallback , useEffect , useMemo } from 'react' ;
1010import type { CoreStart } from '@kbn/core/public' ;
1111import { RuleFormFlyout } from '@kbn/response-ops-rule-form/flyout' ;
1212import { useKibana } from '@kbn/kibana-react-plugin/public' ;
1313import { i18n } from '@kbn/i18n' ;
14+ import { selectDynamicSettings } from '../../../state/settings' ;
15+ import { useSyntheticsSettingsContext } from '../../../contexts' ;
1416import {
1517 selectSyntheticsAlerts ,
1618 selectSyntheticsAlertsLoading ,
19+ selectSyntheticsAlertsLoaded ,
1720} from '../../../state/alert_rules/selectors' ;
21+ import {
22+ enableDefaultAlertingSilentlyAction ,
23+ getDefaultAlertingAction ,
24+ } from '../../../state/alert_rules' ;
1825import { SYNTHETICS_TLS_RULE } from '../../../../../../common/constants/synthetics_alerts' ;
1926import {
2027 selectAlertFlyoutVisibility ,
2128 selectIsNewRule ,
29+ selectMonitorListState ,
2230 setAlertFlyoutVisible ,
2331} from '../../../state' ;
2432import type { ClientPluginsStart } from '../../../../../plugin' ;
@@ -28,8 +36,42 @@ export const useSyntheticsRules = (isOpen: boolean) => {
2836
2937 const defaultRules = useSelector ( selectSyntheticsAlerts ) ;
3038 const loading = useSelector ( selectSyntheticsAlertsLoading ) ;
39+ const rulesLoaded = useSelector ( selectSyntheticsAlertsLoaded ) ;
3140 const alertFlyoutVisible = useSelector ( selectAlertFlyoutVisibility ) ;
3241 const isNewRule = useSelector ( selectIsNewRule ) ;
42+ const { settings } = useSelector ( selectDynamicSettings ) ;
43+ const { canSave } = useSyntheticsSettingsContext ( ) ;
44+ const { loaded, data : monitors } = useSelector ( selectMonitorListState ) ;
45+
46+ const hasMonitors = loaded && monitors . absoluteTotal && monitors . absoluteTotal > 0 ;
47+ const defaultRulesEnabled =
48+ settings && ( settings ?. defaultStatusRuleEnabled || settings ?. defaultTLSRuleEnabled ) ;
49+
50+ const getOrCreateAlerts = useCallback ( ( ) => {
51+ if ( canSave ) {
52+ dispatch ( enableDefaultAlertingSilentlyAction . get ( ) ) ;
53+ } else {
54+ dispatch ( getDefaultAlertingAction . get ( ) ) ;
55+ }
56+ } , [ canSave , dispatch ] ) ;
57+
58+ // // Fetch or create default rules when popover opens
59+ useEffect ( ( ) => {
60+ if ( isOpen && hasMonitors && defaultRulesEnabled && ! loading ) {
61+ // If rules haven't been loaded yet, fetch/create them
62+ if ( rulesLoaded === null || ! defaultRules ) {
63+ getOrCreateAlerts ( ) ;
64+ }
65+ }
66+ } , [
67+ isOpen ,
68+ hasMonitors ,
69+ defaultRulesEnabled ,
70+ loading ,
71+ rulesLoaded ,
72+ defaultRules ,
73+ getOrCreateAlerts ,
74+ ] ) ;
3375
3476 const {
3577 triggersActionsUi : { ruleTypeRegistry, actionTypeRegistry } ,
@@ -39,11 +81,19 @@ export const useSyntheticsRules = (isOpen: boolean) => {
3981 const onClose = useMemo ( ( ) => ( ) => dispatch ( setAlertFlyoutVisible ( null ) ) , [ dispatch ] ) ;
4082
4183 const EditAlertFlyout = useMemo ( ( ) => {
84+ // Don't render if this is a new rule flyout
85+ if ( isNewRule || ! alertFlyoutVisible ) {
86+ return null ;
87+ }
88+
4289 const initialRule =
4390 alertFlyoutVisible === SYNTHETICS_TLS_RULE ? defaultRules ?. tlsRule : defaultRules ?. statusRule ;
44- 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 ) {
4594 return null ;
4695 }
96+
4797 return (
4898 < RuleFormFlyout
4999 plugins = { { ...plugins , ruleTypeRegistry, actionTypeRegistry } }
@@ -88,7 +138,7 @@ export const useSyntheticsRules = (isOpen: boolean) => {
88138 } , [ isNewRule , alertFlyoutVisible , plugins , ruleTypeRegistry , actionTypeRegistry , onClose ] ) ;
89139
90140 return useMemo (
91- ( ) => ( { loading, EditAlertFlyout, NewRuleFlyout } ) ,
92- [ EditAlertFlyout , loading , NewRuleFlyout ]
141+ ( ) => ( { loading, EditAlertFlyout, NewRuleFlyout, defaultRules } ) ,
142+ [ EditAlertFlyout , loading , NewRuleFlyout , defaultRules ]
93143 ) ;
94144} ;
0 commit comments