[Lens] Do not reset session on Lens load with filters (#133191) (#133316)

* improve initializing

* fix test

* resolve conflict

Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
(cherry picked from commit b429cab94a)

Co-authored-by: Joe Reuter <johannes.reuter@elastic.co>
This commit is contained in:
Kibana Machine 2022-06-01 11:01:46 -05:00 committed by GitHub
parent e868002f34
commit ecb04ae525
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 27 deletions

View file

@ -118,6 +118,7 @@ describe('Lens App', () => {
);
const frame = props.editorFrame as ReturnType<typeof createMockFrame>;
lensStore.dispatch(setState({ ...preloadedState }));
return { instance, frame, props, services, lensStore };
}
@ -997,7 +998,7 @@ describe('Lens App', () => {
min: moment('2021-01-09T04:00:00.000Z'),
max: moment('2021-01-09T08:00:00.000Z'),
});
act(() =>
await act(async () =>
instance.find(services.navigation.ui.TopNavMenu).prop('onQuerySubmit')!({
dateRange: { from: 'now-14d', to: 'now-7d' },
query: { query: 'new', language: 'lucene' },

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import React, { FC, useCallback, useEffect, useState } from 'react';
import React, { FC, useCallback, useEffect, useState, useMemo } from 'react';
import { PreloadedState } from '@reduxjs/toolkit';
import { AppMountParameters, CoreSetup, CoreStart } from '@kbn/core/public';
import { FormattedMessage, I18nProvider } from '@kbn/i18n-react';
@ -226,6 +226,20 @@ export async function mountApp(
},
[props.history]
);
const initialInput = useMemo(
() => getInitialInput(props.id, props.editByValue),
[props.editByValue, props.id]
);
const initCallback = useCallback(() => {
// Clear app-specific filters when navigating to Lens. Necessary because Lens
// can be loaded without a full page refresh. If the user navigates to Lens from Discover
// we keep the filters
if (!initialContext) {
data.query.filterManager.setAppFilters([]);
}
lensStore.dispatch(setState(getPreloadedState(storeDeps) as LensAppState));
lensStore.dispatch(loadInitial({ redirectCallback, initialInput, history: props.history }));
}, [initialInput, props.history, redirectCallback]);
useEffect(() => {
(async () => {
const hasUserDataView = await data.dataViews.hasData.hasUserDataView().catch(() => false);
@ -235,27 +249,10 @@ export async function mountApp(
return;
}
setEditorState('data');
initCallback();
})();
}, [props.history]);
}, [initCallback, initialInput, props.history, redirectCallback]);
trackUiEvent('loaded');
const initialInput = getInitialInput(props.id, props.editByValue);
// Clear app-specific filters when navigating to Lens. Necessary because Lens
// can be loaded without a full page refresh. If the user navigates to Lens from Discover
// we keep the filters
if (!initialContext) {
data.query.filterManager.setAppFilters([]);
}
useEffect(() => {
if (editorState === 'data') {
lensStore.dispatch(setState(getPreloadedState(storeDeps) as LensAppState));
lensStore.dispatch(
loadInitial({ redirectCallback, initialInput, history: props.history })
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.id, props.editByValue, editorState]);
if (editorState === 'loading') {
return <EuiLoadingSpinner />;
@ -272,6 +269,7 @@ export async function mountApp(
<AnalyticsNoDataPage
onDataViewCreated={() => {
setEditorState('data');
initCallback();
}}
/>
;

View file

@ -32,11 +32,7 @@ function isTimeBased(state: LensState, datasourceMap: DatasourceMap) {
}
export const contextMiddleware = (storeDeps: LensStoreDeps) => (store: MiddlewareAPI) => {
const unsubscribeFromExternalContext = subscribeToExternalContext(
storeDeps.lensServices.data,
store.getState,
store.dispatch
);
let unsubscribeFromExternalContext: (() => void) | undefined;
return (next: Dispatch) => (action: PayloadAction<unknown>) => {
if (
!(action.payload as Partial<LensAppState>)?.searchSessionId &&
@ -46,10 +42,18 @@ export const contextMiddleware = (storeDeps: LensStoreDeps) => (store: Middlewar
) {
updateTimeRange(storeDeps.lensServices.data, store.dispatch);
}
if (navigateAway.match(action)) {
if (navigateAway.match(action) && unsubscribeFromExternalContext) {
return unsubscribeFromExternalContext();
}
next(action);
// store stopped loading and external context is not subscribed to yet - do it now
if (!store.getState().lens.isLoading && !unsubscribeFromExternalContext) {
unsubscribeFromExternalContext = subscribeToExternalContext(
storeDeps.lensServices.data,
store.getState,
store.dispatch
);
}
};
};