From 85ce9d22bc018792939ac879b7807dc28dcc04af Mon Sep 17 00:00:00 2001 From: "Michael S. Molina" Date: Fri, 18 Aug 2023 11:39:29 -0300 Subject: [PATCH] fix: Dashboard fullscreen is removing custom params --- .../dashboard/util/getDashboardUrl.test.js | 30 +++++++++++++++++++ .../src/dashboard/util/getDashboardUrl.ts | 20 ++++++++----- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/superset-frontend/src/dashboard/util/getDashboardUrl.test.js b/superset-frontend/src/dashboard/util/getDashboardUrl.test.js index 653c17de89a87..c75f2c98b0689 100644 --- a/superset-frontend/src/dashboard/util/getDashboardUrl.test.js +++ b/superset-frontend/src/dashboard/util/getDashboardUrl.test.js @@ -73,6 +73,35 @@ describe('getChartIdsFromLayout', () => { ); }); + it('should encode filters with missing filters', () => { + const urlWithStandalone = getDashboardUrl({ + pathname: 'path', + filters: undefined, + standalone: DashboardStandaloneMode.HIDE_NAV, + }); + expect(urlWithStandalone).toBe( + `path?standalone=${DashboardStandaloneMode.HIDE_NAV}`, + ); + }); + + it('should preserve unknown filters', () => { + const windowSpy = jest.spyOn(window, 'window', 'get'); + windowSpy.mockImplementation(() => ({ + location: { + origin: 'https://localhost', + search: '?unkown_param=value', + }, + })); + const urlWithStandalone = getDashboardUrl({ + pathname: 'path', + standalone: DashboardStandaloneMode.HIDE_NAV, + }); + expect(urlWithStandalone).toBe( + `path?unkown_param=value&standalone=${DashboardStandaloneMode.HIDE_NAV}`, + ); + windowSpy.mockRestore(); + }); + it('should process native filters key', () => { const windowSpy = jest.spyOn(window, 'window', 'get'); windowSpy.mockImplementation(() => ({ @@ -89,5 +118,6 @@ describe('getChartIdsFromLayout', () => { expect(urlWithNativeFilters).toBe( 'path?preselect_filters=%7B%7D&native_filters_key=024380498jdkjf-2094838', ); + windowSpy.mockRestore(); }); }); diff --git a/superset-frontend/src/dashboard/util/getDashboardUrl.ts b/superset-frontend/src/dashboard/util/getDashboardUrl.ts index 9c261e721db80..bade710936f46 100644 --- a/superset-frontend/src/dashboard/util/getDashboardUrl.ts +++ b/superset-frontend/src/dashboard/util/getDashboardUrl.ts @@ -17,6 +17,7 @@ * under the License. */ import { JsonObject } from '@superset-ui/core'; +import { isEmpty } from 'lodash'; import { URL_PARAMS } from 'src/constants'; import { getUrlParam } from 'src/utils/urlUtils'; import serializeActiveFilterValues from './serializeActiveFilterValues'; @@ -32,18 +33,23 @@ export default function getDashboardUrl({ hash: string; standalone?: number | null; }) { - const newSearchParams = new URLSearchParams(); + const newSearchParams = new URLSearchParams(window.location.search); - // convert flattened { [id_column]: values } object - // to nested filter object - newSearchParams.set( - URL_PARAMS.preselectFilters.name, - JSON.stringify(serializeActiveFilterValues(filters)), - ); + if (!isEmpty(filters)) { + // convert flattened { [id_column]: values } object + // to nested filter object + newSearchParams.set( + URL_PARAMS.preselectFilters.name, + JSON.stringify(serializeActiveFilterValues(filters)), + ); + } if (standalone) { newSearchParams.set(URL_PARAMS.standalone.name, standalone.toString()); + } else { + newSearchParams.delete(URL_PARAMS.standalone.name); } + const dataMaskKey = getUrlParam(URL_PARAMS.nativeFiltersKey); if (dataMaskKey) { newSearchParams.set(