Skip to content

Commit f6d564a

Browse files
committed
fix(dashboards): Apply filters when toggling to/from board without filters
Repro steps: - Star a dashboard that has no saved filters, and a dashboard that does have saved filters (e.g. a single project) - Navigate to the dashboard with filters -> correctly loads with filters - Using the starred dashboards sidebar, navigate to the dashboard without filters -> correctly loads without filters - Using the starrred dashboards sidebar, navigate back to the dashboard with filters -> **bug**: does not apply saved filters Dashboards without filters failed to update `dashboardRedirectRef`, causing navigation to short-circuit before applying filters when navigating back to the previous dashboard. Update the ref whenever a filter application decision is made so that navigating between dashboards always triggers a new filter application check.
1 parent a8c4530 commit f6d564a

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

static/app/views/dashboards/orgDashboards.spec.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,58 @@ describe('OrgDashboards', () => {
227227

228228
expect(router.location.query).toEqual({});
229229
});
230+
231+
it('redirects after changing to a dashboard without filters', async () => {
232+
const mockDashboardWithFilters = {
233+
dateCreated: '2021-08-10T21:20:46.798237Z',
234+
id: '1',
235+
title: 'Test Dashboard',
236+
widgets: [],
237+
projects: [1],
238+
filters: {},
239+
};
240+
const mockDashboardWithoutFilters = {
241+
dateCreated: '2021-08-10T21:20:46.798237Z',
242+
id: '2',
243+
title: 'Test Dashboard',
244+
widgets: [],
245+
projects: [],
246+
filters: {},
247+
};
248+
const dashboardWithoutFiltersPath = `/organizations/${organization.slug}/dashboard/2/`;
249+
MockApiClient.addMockResponse({
250+
url: '/organizations/org-slug/dashboards/1/',
251+
method: 'GET',
252+
body: mockDashboardWithFilters,
253+
});
254+
MockApiClient.addMockResponse({
255+
url: '/organizations/org-slug/dashboards/2/',
256+
method: 'GET',
257+
body: mockDashboardWithoutFilters,
258+
});
259+
MockApiClient.addMockResponse({
260+
url: '/organizations/org-slug/dashboards/',
261+
body: [mockDashboardWithFilters, mockDashboardWithoutFilters],
262+
});
263+
264+
const {router} = render(<OrgDashboards>{renderChildFn}</OrgDashboards>, {
265+
initialRouterConfig,
266+
organization,
267+
});
268+
269+
await waitFor(() => expect(router.location.query.project).toBe('1'));
270+
271+
router.navigate(dashboardWithoutFiltersPath);
272+
273+
// Since we navigate to a URL without parameters, the empty query assertion
274+
// trivially succeeds - wait on loading to be complete so that we know the
275+
// dashboard filter hook has run, and _then_ make sure the query is (still)
276+
// empty.
277+
await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
278+
await waitFor(() => expect(router.location.query).toEqual({}));
279+
280+
router.navigate(dashboardPath);
281+
282+
await waitFor(() => expect(router.location.query.project).toBe('1'));
283+
});
230284
});

static/app/views/dashboards/orgDashboards.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,21 @@ export function OrgDashboards({children, initialDashboard}: OrgDashboardsProps)
133133

134134
// current filters based on location
135135
const locationFilters = getCurrentPageFilters(location);
136+
137+
if (!selectedDashboard) {
138+
// Still loading.
139+
return;
140+
}
141+
136142
if (
137-
!selectedDashboard ||
138143
!hasSavedPageFilters(selectedDashboard) ||
139144
// Apply redirect once for each dashboard id
140145
dashboardRedirectRef.current === selectedDashboard.id ||
141146
hasSavedPageFilters(locationFilters)
142147
) {
148+
// Mark the redirect check complete even though we didn't redirect (so
149+
// that switching to another dashboard reruns the check.)
150+
dashboardRedirectRef.current = selectedDashboard.id;
143151
return;
144152
}
145153

0 commit comments

Comments
 (0)