Skip to content

Commit bfdb4da

Browse files
author
David Wang
authored
feat(page-filters): Add function to revert to pinned filters (#32140)
Used for when this revert button is clicked: #32141
1 parent 36f2ac5 commit bfdb4da

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

static/app/actionCreators/pageFilters.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from 'sentry/components/organizations/pageFilters/utils';
2323
import {DATE_TIME_KEYS, URL_PARAM} from 'sentry/constants/pageFilters';
2424
import OrganizationStore from 'sentry/stores/organizationStore';
25+
import PageFiltersStore from 'sentry/stores/pageFiltersStore';
2526
import {
2627
DateString,
2728
Environment,
@@ -534,3 +535,29 @@ function getNewQueryParams(
534535

535536
return Object.fromEntries(paramEntries) as PageFilterQuery;
536537
}
538+
539+
export function revertToPinnedFilters(orgSlug: string, router: InjectedRouter) {
540+
const {selection, desyncedFilters} = PageFiltersStore.getState();
541+
const storedFilterState = getPageFilterStorage(orgSlug)?.state;
542+
543+
if (!storedFilterState) {
544+
return;
545+
}
546+
547+
const newParams = {
548+
project: desyncedFilters.has('projects')
549+
? storedFilterState.project
550+
: selection.projects,
551+
environment: desyncedFilters.has('environments')
552+
? storedFilterState.environment
553+
: selection.environments,
554+
...(desyncedFilters.has('datetime')
555+
? pick(storedFilterState, DATE_TIME_KEYS)
556+
: selection.datetime),
557+
};
558+
559+
updateParams(newParams, router, {
560+
keepCursor: true,
561+
});
562+
updateDesyncedUrlState(router);
563+
}

tests/js/spec/actionCreators/pageFilters.spec.jsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
initializeUrlState,
3+
revertToPinnedFilters,
34
updateDateTime,
45
updateEnvironments,
56
updateProjects,
@@ -483,4 +484,58 @@ describe('PageFilters ActionCreators', function () {
483484
});
484485
});
485486
});
487+
488+
describe('revertToPinnedFilters()', function () {
489+
it('reverts all filters that are desynced from localStorage', async function () {
490+
const router = TestStubs.router({
491+
location: {
492+
pathname: '/test/',
493+
query: {},
494+
},
495+
});
496+
// Mock storage to have a saved value
497+
const pageFilterStorageMock = jest
498+
.spyOn(PageFilterPersistence, 'getPageFilterStorage')
499+
.mockReturnValueOnce({
500+
state: {
501+
project: ['1'],
502+
environment: [],
503+
start: null,
504+
end: null,
505+
period: '14d',
506+
utc: null,
507+
},
508+
pinnedFilters: new Set(['projects', 'environments', 'datetime']),
509+
});
510+
511+
PageFiltersActions.initializeUrlState({
512+
projects: ['2'],
513+
environments: ['prod'],
514+
datetime: {
515+
start: null,
516+
end: null,
517+
period: '1d',
518+
utc: null,
519+
},
520+
});
521+
PageFiltersActions.updateDesyncedFilters(
522+
new Set(['projects', 'environments', 'datetime'])
523+
);
524+
// Tick for PageFiltersActions
525+
await tick();
526+
527+
revertToPinnedFilters('org-slug', router);
528+
529+
expect(router.push).toHaveBeenCalledWith({
530+
pathname: '/test/',
531+
query: {
532+
environment: [],
533+
project: ['1'],
534+
statsPeriod: '14d',
535+
},
536+
});
537+
538+
pageFilterStorageMock.mockRestore();
539+
});
540+
});
486541
});

0 commit comments

Comments
 (0)