Просмотр исходного кода

feat(page-filters): Add function to revert to pinned filters (#32140)

Used for when this revert button is clicked: #32141
David Wang 3 лет назад
Родитель
Сommit
bfdb4dad04

+ 27 - 0
static/app/actionCreators/pageFilters.tsx

@@ -22,6 +22,7 @@ import {
 } from 'sentry/components/organizations/pageFilters/utils';
 import {DATE_TIME_KEYS, URL_PARAM} from 'sentry/constants/pageFilters';
 import OrganizationStore from 'sentry/stores/organizationStore';
+import PageFiltersStore from 'sentry/stores/pageFiltersStore';
 import {
   DateString,
   Environment,
@@ -534,3 +535,29 @@ function getNewQueryParams(
 
   return Object.fromEntries(paramEntries) as PageFilterQuery;
 }
+
+export function revertToPinnedFilters(orgSlug: string, router: InjectedRouter) {
+  const {selection, desyncedFilters} = PageFiltersStore.getState();
+  const storedFilterState = getPageFilterStorage(orgSlug)?.state;
+
+  if (!storedFilterState) {
+    return;
+  }
+
+  const newParams = {
+    project: desyncedFilters.has('projects')
+      ? storedFilterState.project
+      : selection.projects,
+    environment: desyncedFilters.has('environments')
+      ? storedFilterState.environment
+      : selection.environments,
+    ...(desyncedFilters.has('datetime')
+      ? pick(storedFilterState, DATE_TIME_KEYS)
+      : selection.datetime),
+  };
+
+  updateParams(newParams, router, {
+    keepCursor: true,
+  });
+  updateDesyncedUrlState(router);
+}

+ 55 - 0
tests/js/spec/actionCreators/pageFilters.spec.jsx

@@ -1,5 +1,6 @@
 import {
   initializeUrlState,
+  revertToPinnedFilters,
   updateDateTime,
   updateEnvironments,
   updateProjects,
@@ -483,4 +484,58 @@ describe('PageFilters ActionCreators', function () {
       });
     });
   });
+
+  describe('revertToPinnedFilters()', function () {
+    it('reverts all filters that are desynced from localStorage', async function () {
+      const router = TestStubs.router({
+        location: {
+          pathname: '/test/',
+          query: {},
+        },
+      });
+      // Mock storage to have a saved value
+      const pageFilterStorageMock = jest
+        .spyOn(PageFilterPersistence, 'getPageFilterStorage')
+        .mockReturnValueOnce({
+          state: {
+            project: ['1'],
+            environment: [],
+            start: null,
+            end: null,
+            period: '14d',
+            utc: null,
+          },
+          pinnedFilters: new Set(['projects', 'environments', 'datetime']),
+        });
+
+      PageFiltersActions.initializeUrlState({
+        projects: ['2'],
+        environments: ['prod'],
+        datetime: {
+          start: null,
+          end: null,
+          period: '1d',
+          utc: null,
+        },
+      });
+      PageFiltersActions.updateDesyncedFilters(
+        new Set(['projects', 'environments', 'datetime'])
+      );
+      // Tick for PageFiltersActions
+      await tick();
+
+      revertToPinnedFilters('org-slug', router);
+
+      expect(router.push).toHaveBeenCalledWith({
+        pathname: '/test/',
+        query: {
+          environment: [],
+          project: ['1'],
+          statsPeriod: '14d',
+        },
+      });
+
+      pageFilterStorageMock.mockRestore();
+    });
+  });
 });