Skip to content

Commit 95cf1ba

Browse files
committed
Merge branch 'synthetics/edit-default-rules-broken' into 241396/create-default-rules-without-connectors-and-do-not-create-default-rules-during-navigation
2 parents 085ce8d + d05a0d9 commit 95cf1ba

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/alerts/hooks/use_synthetics_rules.tsx

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@
66
*/
77

88
import { useDispatch, useSelector } from 'react-redux';
9-
import React, { useMemo } from 'react';
9+
import React, { useCallback, useEffect, useMemo } from 'react';
1010
import type { CoreStart } from '@kbn/core/public';
1111
import { RuleFormFlyout } from '@kbn/response-ops-rule-form/flyout';
1212
import { useKibana } from '@kbn/kibana-react-plugin/public';
1313
import { i18n } from '@kbn/i18n';
14+
import { selectDynamicSettings } from '../../../state/settings';
15+
import { useSyntheticsSettingsContext } from '../../../contexts';
1416
import {
1517
selectSyntheticsAlerts,
1618
selectSyntheticsAlertsLoading,
19+
selectSyntheticsAlertsLoaded,
1720
} from '../../../state/alert_rules/selectors';
21+
import {
22+
enableDefaultAlertingSilentlyAction,
23+
getDefaultAlertingAction,
24+
} from '../../../state/alert_rules';
1825
import { SYNTHETICS_TLS_RULE } from '../../../../../../common/constants/synthetics_alerts';
1926
import {
2027
selectAlertFlyoutVisibility,
2128
selectIsNewRule,
29+
selectMonitorListState,
2230
setAlertFlyoutVisible,
2331
} from '../../../state';
2432
import 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
};

x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/alerts/toggle_alert_flyout_button.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ export const ToggleAlertFlyoutButton = () => {
3333
const { application } = useKibana<ClientPluginsStart>().services;
3434
const hasUptimeWrite = application?.capabilities.uptime?.save ?? false;
3535

36-
const { EditAlertFlyout, loading, NewRuleFlyout } = useSyntheticsRules(isOpen);
36+
const { EditAlertFlyout, loading, NewRuleFlyout, defaultRules } = useSyntheticsRules(isOpen);
3737
const { loaded, data: monitors } = useSelector(selectMonitorListState);
3838

3939
const hasMonitors = loaded && monitors.absoluteTotal && monitors.absoluteTotal > 0;
40+
const statusRuleExists = Boolean(defaultRules?.statusRule);
41+
const tlsRuleExists = Boolean(defaultRules?.tlsRule);
4042

4143
const panels: EuiContextMenuPanelDescriptor[] = [
4244
{
@@ -81,7 +83,7 @@ export const ToggleAlertFlyoutButton = () => {
8183
setIsOpen(false);
8284
},
8385
toolTipContent: !hasUptimeWrite ? noWritePermissionsTooltipContent : null,
84-
disabled: !hasUptimeWrite || loading,
86+
disabled: !hasUptimeWrite || loading || !statusRuleExists,
8587
icon: 'bell',
8688
},
8789
],
@@ -107,7 +109,7 @@ export const ToggleAlertFlyoutButton = () => {
107109
setIsOpen(false);
108110
},
109111
toolTipContent: !hasUptimeWrite ? noWritePermissionsTooltipContent : null,
110-
disabled: !hasUptimeWrite || loading,
112+
disabled: !hasUptimeWrite || loading || !tlsRuleExists,
111113
icon: 'bell',
112114
},
113115
],

0 commit comments

Comments
 (0)