Skip to content

Commit d05a0d9

Browse files
committed
Disable edit default rule(s) buttons if default rules are not defined.
1 parent 8c975fc commit d05a0d9

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

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)