Repository: getsentry/sentry
Pull request: Fix(Redirection on deleting monitor): fixed redirection on deleting monitor

Fixes: #59723 

When the filter is applied on crons page (monitor listing page) - then prevUrl gets added to URL. which helps in redirection to the correct URL when the monitor is deleted by user.
<!-- Describe your PR here. -->



<!--

  Sentry employees and contractors can delete or ignore the following.

-->

### Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.


Problem statement:
Crons: Deleting a monitor removes the project selection
If you have a project selected, and go to your monitor details page and delete a monitor, the page is redirected back to the monitors list however the project selection is cleared.

Expected behavior: the project selection should not be cleared.

Hints from issue discussion:
Routing to @getsentry/product-owners-crons for [triage](https://develop.sentry.dev/processing-tickets/#3-triage) ⏲️
Hi,
Is it open?
Can i try resolving this?


Hey @Shubhdeep12, thanks for the interest! You are more than welcome to try and resolve it. Please make sure to read our [CONTRIBUTING.md](https://github.com/getsentry/sentry/blob/03d1c7f5e455a768b5047ce85613dbd45f4bdd2f/.github/CONTRIBUTING.md) before submitting your PR. Thank you!

Commit message: fixed redirection on deleting monitor

Patch to review:
diff --git a/static/app/views/monitors/components/monitorHeaderActions.tsx b/static/app/views/monitors/components/monitorHeaderActions.tsx
index 43f3ee029924d4..9a9cbbe3bf8087 100644
--- a/static/app/views/monitors/components/monitorHeaderActions.tsx
+++ b/static/app/views/monitors/components/monitorHeaderActions.tsx
@@ -8,6 +8,7 @@ import {IconDelete, IconEdit, IconPause, IconPlay} from 'sentry/icons';
 import {t} from 'sentry/locale';
 import useApi from 'sentry/utils/useApi';
 import usePageFilters from 'sentry/utils/usePageFilters';
+import useRouter from 'sentry/utils/useRouter';
 import {normalizeUrl} from 'sentry/utils/withDomainRequired';
 
 import {Monitor, MonitorStatus} from '../types';
@@ -22,11 +23,19 @@ type Props = {
 
 function MonitorHeaderActions({monitor, orgId, onUpdate}: Props) {
   const api = useApi();
+  const router = useRouter();
+
   const {selection} = usePageFilters();
 
   const handleDelete = async () => {
     await deleteMonitor(api, orgId, monitor.slug);
-    browserHistory.push(normalizeUrl(`/organizations/${orgId}/crons/`));
+    browserHistory.push(
+      normalizeUrl(
+        `/organizations/${orgId}/crons/${
+          router.location.query.prevUrl ? `?project=${router.location.query.prevUrl}` : ''
+        }`
+      )
+    );
   };
 
   const handleUpdate = async (data: Partial<Monitor>) => {
diff --git a/static/app/views/monitors/components/overviewTimeline/timelineTableRow.tsx b/static/app/views/monitors/components/overviewTimeline/timelineTableRow.tsx
index afd474b5e08c3e..bec5c1cd31dd74 100644
--- a/static/app/views/monitors/components/overviewTimeline/timelineTableRow.tsx
+++ b/static/app/views/monitors/components/overviewTimeline/timelineTableRow.tsx
@@ -12,6 +12,7 @@ import {t, tct} from 'sentry/locale';
 import {fadeIn} from 'sentry/styles/animations';
 import {space} from 'sentry/styles/space';
 import useOrganization from 'sentry/utils/useOrganization';
+import useRouter from 'sentry/utils/useRouter';
 import {Monitor, MonitorStatus} from 'sentry/views/monitors/types';
 import {scheduleAsText} from 'sentry/views/monitors/utils';
 import {statusIconColorMap} from 'sentry/views/monitors/utils/constants';
@@ -131,8 +132,11 @@ export function TimelineTableRow({
 function MonitorDetails({monitor}: {monitor: Monitor}) {
   const organization = useOrganization();
   const schedule = scheduleAsText(monitor.config);
+  const router = useRouter();
 
-  const monitorDetailUrl = `/organizations/${organization.slug}/crons/${monitor.slug}/`;
+  const monitorDetailUrl = `/organizations/${organization.slug}/crons/${monitor.slug}/${
+    router.location.query.project ? `?prevUrl=${router.location.query.project}` : ''
+  }`;
 
   return (
     <DetailsContainer to={monitorDetailUrl}>


Code review comments:
diff_hunk: @@ -22,11 +23,19 @@ type Props = {
 
 function MonitorHeaderActions({monitor, orgId, onUpdate}: Props) {
   const api = useApi();
+  const router = useRouter();
+
   const {selection} = usePageFilters();
 
   const handleDelete = async () => {
     await deleteMonitor(api, orgId, monitor.slug);
-    browserHistory.push(normalizeUrl(`/organizations/${orgId}/crons/`));
+    browserHistory.push(
+      normalizeUrl(
+        `/organizations/${orgId}/crons/${
+          router.location.query.prevUrl ? `?project=${router.location.query.prevUrl}` : ''
path: static/app/views/monitors/components/monitorHeaderActions.tsx
text: @user1:
This method works, but adds unnecessary query parameters to the URL

It also only functions when coming from the monitor overview page, if you were to add a monitor and then delete it immediately you would be redirected back to a page without a project selection

What I think we could do here is leverage the `selection` from `usePageFilters` which holds global state around which project(s) were selected and feed those into the query params. Let me know if that makes sense and if it works out, I haven't tried it.

@author:
Yes @user1 correct. Using prevUrl is not needed as we need selected filters - projects and environments which will be present in usePageFilters.

And, yes i only covered just one flow but the creation, edition redirection should also be handled and also the breadcrumbs.

I am pushing a commit in sometime for this.

Thanks