From 28ca6600fd69812309c96ba784d25cb906461fd6 Mon Sep 17 00:00:00 2001 From: Edward Gou Date: Wed, 15 Apr 2026 13:02:04 -0400 Subject: [PATCH] fix(dashboards): Preserve page filters when navigating from prebuilt dashboard link The link from a prebuilt dashboard to the saved dashboard was dropping project, environment, and date selections. Pass the current selection parameters through via extractSelectionParameters. --- .../prebuiltDashboardRenderer.spec.tsx | 55 +++++++++++++++++++ .../dashboards/prebuiltDashboardRenderer.tsx | 8 ++- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 static/app/views/dashboards/prebuiltDashboardRenderer.spec.tsx diff --git a/static/app/views/dashboards/prebuiltDashboardRenderer.spec.tsx b/static/app/views/dashboards/prebuiltDashboardRenderer.spec.tsx new file mode 100644 index 00000000000000..8e442f3f071fe7 --- /dev/null +++ b/static/app/views/dashboards/prebuiltDashboardRenderer.spec.tsx @@ -0,0 +1,55 @@ +import {OrganizationFixture} from 'sentry-fixture/organization'; + +import {render, screen} from 'sentry-test/reactTestingLibrary'; + +import {PrebuiltDashboardRenderer} from 'sentry/views/dashboards/prebuiltDashboardRenderer'; +import {PrebuiltDashboardId} from 'sentry/views/dashboards/utils/prebuiltConfigs'; + +jest.mock('sentry/views/dashboards/detail', () => ({ + DashboardDetailWithInjectedProps: () =>
, +})); + +jest.mock('sentry/views/dashboards/utils/usePopulateLinkedDashboards', () => ({ + useGetPrebuiltDashboard: () => ({ + dashboard: {id: '42', widgets: []}, + isLoading: false, + }), +})); + +describe('PrebuiltDashboardRenderer', () => { + const organization = OrganizationFixture(); + + it('renders dashboard link with page filter query params from the URL', async () => { + render( + , + { + organization, + initialRouterConfig: { + location: { + pathname: '/insights/backend/', + query: { + project: '1', + environment: 'production', + statsPeriod: '7d', + }, + }, + }, + } + ); + + const link = await screen.findByRole('link', { + name: 'View this page on Dashboards', + }); + + expect(link).toHaveAttribute( + 'href', + expect.stringContaining(`/organizations/${organization.slug}/dashboard/42/`) + ); + expect(link).toHaveAttribute('href', expect.stringContaining('project=1')); + expect(link).toHaveAttribute( + 'href', + expect.stringContaining('environment=production') + ); + expect(link).toHaveAttribute('href', expect.stringContaining('statsPeriod=7d')); + }); +}); diff --git a/static/app/views/dashboards/prebuiltDashboardRenderer.tsx b/static/app/views/dashboards/prebuiltDashboardRenderer.tsx index cff03c05743839..6947b2e6e9a6eb 100644 --- a/static/app/views/dashboards/prebuiltDashboardRenderer.tsx +++ b/static/app/views/dashboards/prebuiltDashboardRenderer.tsx @@ -5,10 +5,12 @@ import {Link} from '@sentry/scraps/link'; import {useDismissable} from 'sentry/components/banner'; import {LoadingContainer} from 'sentry/components/loading/loadingContainer'; +import {extractSelectionParameters} from 'sentry/components/pageFilters/parse'; import {usePageFilters} from 'sentry/components/pageFilters/usePageFilters'; import {IconClose} from 'sentry/icons'; import {tct} from 'sentry/locale'; import {useIsSentryEmployee} from 'sentry/utils/useIsSentryEmployee'; +import {useLocation} from 'sentry/utils/useLocation'; import {useOrganization} from 'sentry/utils/useOrganization'; import {DashboardDetailWithInjectedProps as DashboardDetail} from 'sentry/views/dashboards/detail'; import { @@ -33,6 +35,7 @@ export function PrebuiltDashboardRenderer({ storageNamespace, }: PrebuiltDashboardRendererProps) { const organization = useOrganization(); + const location = useLocation(); const prebuiltDashboard = PREBUILT_DASHBOARDS[prebuiltId]; const {dashboard: populatedPrebuiltDashboard, isLoading} = useGetPrebuiltDashboard(prebuiltId); @@ -100,7 +103,10 @@ export function PrebuiltDashboardRenderer({ { link: ( ), }