Skip to content

Commit 4d473a1

Browse files
ref(crons): Invalidate monitor query after environment muting (#103692)
Part of [NEW-564: There needs to be some way to mute the entire cron detector](https://linear.app/getsentry/issue/NEW-564/there-needs-to-be-some-way-to-mute-the-entire-cron-detector) Changes the monitor update flow to invalidate the query cache instead of manually updating cached data when muting/unmuting monitor environments. This ensures the UI always reflects the latest environment data from the server, including mute status and other environment properties.
1 parent 4ac6ca7 commit 4d473a1

File tree

3 files changed

+14
-35
lines changed

3 files changed

+14
-35
lines changed

static/app/views/alerts/rules/crons/details.tsx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {TimezoneProvider, useTimezone} from 'sentry/components/timezoneProvider'
1515
import {t} from 'sentry/locale';
1616
import {space} from 'sentry/styles/space';
1717
import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
18-
import {setApiQueryData, useApiQuery, useQueryClient} from 'sentry/utils/queryClient';
18+
import {useApiQuery, useQueryClient} from 'sentry/utils/queryClient';
1919
import useApi from 'sentry/utils/useApi';
2020
import useOrganization from 'sentry/utils/useOrganization';
2121
import {DetailsSidebar} from 'sentry/views/insights/crons/components/detailsSidebar';
@@ -73,26 +73,19 @@ function MonitorDetails({params, location}: Props) {
7373
monitorSlug: params.monitorSlug,
7474
});
7575

76-
function onUpdate(data: Monitor) {
77-
const updatedMonitor = {
78-
...data,
79-
// TODO(davidenwang): This is a bit of a hack, due to the PUT request
80-
// which pauses/unpauses a monitor not returning monitor environments
81-
// we should reuse the environments retrieved from the initial request
82-
environments: monitor?.environments,
83-
};
84-
setApiQueryData(queryClient, queryKey, updatedMonitor);
76+
function onUpdate() {
77+
// Invalidate the query to refetch the monitor with updated environment data.
78+
// The PUT request doesn't return environments, so we need to refetch to get
79+
// the latest environment muting status and other environment data.
80+
queryClient.invalidateQueries({queryKey});
8581
}
8682

8783
const handleUpdate = async (data: Partial<Monitor>) => {
8884
if (monitor === undefined) {
8985
return;
9086
}
91-
const resp = await updateMonitor(api, organization.slug, monitor, data);
92-
93-
if (resp !== null) {
94-
onUpdate(resp);
95-
}
87+
await updateMonitor(api, organization.slug, monitor, data);
88+
onUpdate();
9689
};
9790

9891
const userTimezone = useTimezone();

static/app/views/insights/crons/components/detailsTimeline.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export function DetailsTimeline({monitor, onStatsLoaded, onEnvironmentUpdated}:
6666
organization,
6767
monitor.project.slug,
6868
monitor.slug,
69-
{...location.query}
69+
{
70+
environment: location.query.environment,
71+
}
7072
);
7173

7274
const {data: monitorStats} = useMonitorStats({
@@ -110,21 +112,8 @@ export function DetailsTimeline({monitor, onStatsLoaded, onEnvironmentUpdated}:
110112
return;
111113
}
112114

113-
setApiQueryData<Monitor>(queryClient, monitorDetailsQueryKey, oldMonitorDetails => {
114-
return oldMonitorDetails
115-
? {
116-
...oldMonitorDetails,
117-
environments: oldMonitorDetails.environments.map(monitorEnv =>
118-
monitorEnv.name === env
119-
? {
120-
...monitorEnv,
121-
isMuted,
122-
}
123-
: monitorEnv
124-
),
125-
}
126-
: undefined;
127-
});
115+
// Invalidate the query to refetch the monitor with updated environment data
116+
queryClient.invalidateQueries({queryKey: monitorDetailsQueryKey});
128117

129118
onEnvironmentUpdated?.();
130119
};

static/app/views/insights/crons/components/overviewTimeline/overviewRow.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ export function OverviewRow({
147147
...(onToggleMuteEnvironment
148148
? [
149149
(env: string, isMuted: boolean) => ({
150-
label:
151-
isMuted && !monitor.isMuted
152-
? t('Unmute Environment')
153-
: t('Mute Environment'),
150+
label: isMuted ? t('Unmute Environment') : t('Mute Environment'),
154151
key: 'mute',
155152
onAction: () => onToggleMuteEnvironment(env, !isMuted),
156153
}),

0 commit comments

Comments
 (0)