From 26146f3ed2e77dd6440c213cc3867c446fd1e08e Mon Sep 17 00:00:00 2001 From: Anton Dosov Date: Tue, 3 Nov 2020 13:20:34 +0100 Subject: [PATCH] remove query param from the URL only on subsequant state changes --- .../public/application/angular/discover.js | 40 ++++++++---------- .../public/history/get_query_params.test.ts | 41 +++++++++++++++++++ .../public/history/get_query_params.ts | 27 ++++++++++++ .../kibana_utils/public/history/index.ts | 1 + src/plugins/kibana_utils/public/index.ts | 2 +- 5 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 src/plugins/kibana_utils/public/history/get_query_params.test.ts create mode 100644 src/plugins/kibana_utils/public/history/get_query_params.ts diff --git a/src/plugins/discover/public/application/angular/discover.js b/src/plugins/discover/public/application/angular/discover.js index b2fd947750e1e..2de89ac36dc05 100644 --- a/src/plugins/discover/public/application/angular/discover.js +++ b/src/plugins/discover/public/application/angular/discover.js @@ -83,7 +83,7 @@ import { MODIFY_COLUMNS_ON_SWITCH, } from '../../../common'; import { SEARCH_SESSION_ID_QUERY_PARAM } from '../../url_generator'; -import { removeQueryParam } from '../../../../kibana_utils/public'; +import { removeQueryParam, getQueryParams } from '../../../../kibana_utils/public'; const fetchStatuses = { UNINITIALIZED: 'uninitialized', @@ -92,6 +92,9 @@ const fetchStatuses = { ERROR: 'error', }; +const getSearchSessionIdFromURL = (history) => + getQueryParams(history.location)[SEARCH_SESSION_ID_QUERY_PARAM]; + const app = getAngularModule(); app.config(($routeProvider) => { @@ -193,16 +196,7 @@ app.directive('discoverApp', function () { }; }); -function discoverController( - $element, - $route, - $scope, - $timeout, - $window, - Promise, - uiCapabilities, - $routeParams -) { +function discoverController($element, $route, $scope, $timeout, $window, Promise, uiCapabilities) { const { isDefault: isDefaultType } = indexPatternsUtils; const subscriptions = new Subscription(); const refetch$ = new Subject(); @@ -218,11 +212,7 @@ function discoverController( }; const history = getHistory(); - - let initialSearchSessionId = $routeParams[SEARCH_SESSION_ID_QUERY_PARAM]; - if (initialSearchSessionId) { - removeQueryParam(history, SEARCH_SESSION_ID_QUERY_PARAM); - } + let isInitialSearch = true; const { appStateContainer, @@ -814,14 +804,18 @@ function discoverController( abortController = new AbortController(); const searchSessionId = (() => { - if (initialSearchSessionId) { - const searchSessionId = initialSearchSessionId; - data.search.session.restore(searchSessionId); - initialSearchSessionId = null; // reset to make further searches create new session - return searchSessionId; - } else { - return data.search.session.start(); + const searchSessionIdFromURL = getSearchSessionIdFromURL(history); + if (searchSessionIdFromURL) { + if (isInitialSearch) { + data.search.session.restore(searchSessionIdFromURL); + isInitialSearch = false; + return searchSessionIdFromURL; + } else { + // navigating away from background search + removeQueryParam(history, SEARCH_SESSION_ID_QUERY_PARAM); + } } + return data.search.session.start(); })(); $scope diff --git a/src/plugins/kibana_utils/public/history/get_query_params.test.ts b/src/plugins/kibana_utils/public/history/get_query_params.test.ts new file mode 100644 index 0000000000000..d2fecb18d439a --- /dev/null +++ b/src/plugins/kibana_utils/public/history/get_query_params.test.ts @@ -0,0 +1,41 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { getQueryParams } from './get_query_params'; +import { Location } from 'history'; + +describe('getQueryParams', () => { + it('should getQueryParams', () => { + const location: Location = { + pathname: '/dashboard/c3a76790-3134-11ea-b024-83a7b4783735', + search: "?_a=(description:'')&_b=3", + state: null, + hash: '', + }; + + const query = getQueryParams(location); + + expect(query).toMatchInlineSnapshot(` + Object { + "_a": "(description:'')", + "_b": "3", + } + `); + }); +}); diff --git a/src/plugins/kibana_utils/public/history/get_query_params.ts b/src/plugins/kibana_utils/public/history/get_query_params.ts new file mode 100644 index 0000000000000..e28aafd2d3be8 --- /dev/null +++ b/src/plugins/kibana_utils/public/history/get_query_params.ts @@ -0,0 +1,27 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { parse, ParsedQuery } from 'query-string'; +import { Location } from 'history'; + +export function getQueryParams(location: Location): ParsedQuery { + const search = (location.search || '').replace(/^\?/, ''); + const query = parse(search, { sort: false }); + return query; +} diff --git a/src/plugins/kibana_utils/public/history/index.ts b/src/plugins/kibana_utils/public/history/index.ts index bb13ea09f928a..88693a971250b 100644 --- a/src/plugins/kibana_utils/public/history/index.ts +++ b/src/plugins/kibana_utils/public/history/index.ts @@ -19,3 +19,4 @@ export { removeQueryParam } from './remove_query_param'; export { redirectWhenMissing } from './redirect_when_missing'; +export { getQueryParams } from './get_query_params'; diff --git a/src/plugins/kibana_utils/public/index.ts b/src/plugins/kibana_utils/public/index.ts index 7edf62ce04e81..9ba42d39139da 100644 --- a/src/plugins/kibana_utils/public/index.ts +++ b/src/plugins/kibana_utils/public/index.ts @@ -74,7 +74,7 @@ export { StopSyncStateFnType, } from './state_sync'; export { Configurable, CollectConfigProps } from './ui'; -export { removeQueryParam, redirectWhenMissing } from './history'; +export { removeQueryParam, redirectWhenMissing, getQueryParams } from './history'; export { applyDiff } from './state_management/utils/diff_object'; export { createStartServicesGetter, StartServicesGetter } from './core/create_start_service_getter';