Skip to content

Commit 1c78cf1

Browse files
committed
(csrf) replace auto muting useEffect in SnoozeAlert
1 parent 8d290fd commit 1c78cf1

2 files changed

Lines changed: 59 additions & 13 deletions

File tree

static/app/components/alerts/snoozeAlert.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import {useCallback, useEffect, useState} from 'react';
1+
import {useCallback, useEffect, useRef, useState} from 'react';
22
import styled from '@emotion/styled';
33

44
import {Button} from '@sentry/scraps/button';
55
import {Grid} from '@sentry/scraps/layout';
66

77
import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
8+
import {openConfirmModal} from 'sentry/components/confirm';
89
import {IconMute, IconSound} from 'sentry/icons';
910
import {t} from 'sentry/locale';
1011
import {useApi} from 'sentry/utils/useApi';
@@ -124,11 +125,40 @@ export function SnoozeAlert({
124125

125126
const primaryMuteAction = 'everyone';
126127

128+
const hasPrompted = useRef(false);
129+
127130
useEffect(() => {
128-
if (location.query.mute === '1' && !isSnoozed) {
129-
handleMute(primaryMuteAction, true);
131+
if (location.query.mute !== '1' || isSnoozed || hasPrompted.current) {
132+
return;
130133
}
131-
}, [location.query, isSnoozed, handleMute, primaryMuteAction]);
134+
hasPrompted.current = true;
135+
136+
const stripMuteParam = () => {
137+
navigate(
138+
{
139+
pathname: location.pathname,
140+
query: {...location.query, mute: undefined},
141+
},
142+
{replace: true}
143+
);
144+
};
145+
146+
openConfirmModal({
147+
header: t('Mute Alert'),
148+
message: t('Are you sure you want to mute this alert for everyone?'),
149+
confirmText: t('Mute'),
150+
onConfirm: () => handleMute(primaryMuteAction, true),
151+
onCancel: stripMuteParam,
152+
onClose: stripMuteParam,
153+
});
154+
}, [
155+
location.query,
156+
location.pathname,
157+
isSnoozed,
158+
navigate,
159+
handleMute,
160+
primaryMuteAction,
161+
]);
132162

133163
if (isSnoozed) {
134164
return (

static/app/views/alerts/rules/issue/details/ruleDetails.spec.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import {OrganizationFixture} from 'sentry-fixture/organization';
55
import {ProjectFixture} from 'sentry-fixture/project';
66
import {ProjectAlertRuleFixture} from 'sentry-fixture/projectAlertRule';
77

8-
import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
8+
import {
9+
render,
10+
renderGlobalModal,
11+
screen,
12+
userEvent,
13+
waitFor,
14+
} from 'sentry-test/reactTestingLibrary';
915

1016
import {ProjectsStore} from 'sentry/stores/projectsStore';
1117

@@ -311,21 +317,31 @@ describe('AlertRuleDetails', () => {
311317
expect(deleteRequest).toHaveBeenCalledTimes(1);
312318
});
313319

314-
it('mutes alert if query parameter is set', async () => {
320+
it('shows confirmation modal when mute query parameter is set', async () => {
315321
const request = MockApiClient.addMockResponse({
316322
url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/snooze/`,
317323
method: 'POST',
318324
});
319325

320326
createWrapper({query: {mute: '1'}});
327+
renderGlobalModal();
321328

322-
expect(await screen.findByText('Unmute')).toBeInTheDocument();
323-
expect(request).toHaveBeenCalledWith(
324-
expect.anything(),
325-
expect.objectContaining({
326-
data: {target: 'everyone'},
327-
})
328-
);
329+
expect(
330+
await screen.findByText('Are you sure you want to mute this alert for everyone?')
331+
).toBeInTheDocument();
332+
333+
expect(request).not.toHaveBeenCalled();
334+
335+
await userEvent.click(screen.getByRole('button', {name: 'Mute'}));
336+
337+
await waitFor(() => {
338+
expect(request).toHaveBeenCalledWith(
339+
expect.anything(),
340+
expect.objectContaining({
341+
data: {target: 'everyone'},
342+
})
343+
);
344+
});
329345
});
330346

331347
it('mute button is disabled if no alerts:write permission', async () => {

0 commit comments

Comments
 (0)