[Discover] Migrate session session initialization to state container (#156957)

## Summary

Migrate `useSearchSession` to state container's `initializeAndSync` function, to remove another `hook`.
This commit is contained in:
Matthias Wilhelm 2023-05-22 18:33:42 +02:00 committed by GitHub
parent 8a9fb98c75
commit ef678f3d92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 82 deletions

View file

@ -9,7 +9,6 @@ import React, { useCallback, useEffect } from 'react';
import { RootDragDropProvider } from '@kbn/dom-drag-drop';
import { useHistory } from 'react-router-dom';
import { useUrlTracking } from './hooks/use_url_tracking';
import { useSearchSession } from './hooks/use_search_session';
import { DiscoverStateContainer } from './services/discover_state';
import { DiscoverLayout } from './components/layout';
import { setBreadcrumbsTitle } from '../../utils/breadcrumbs';
@ -44,11 +43,6 @@ export function DiscoverMainApp(props: DiscoverMainProps) {
useUrlTracking(stateContainer.savedSearchState);
/**
* Search session logic
*/
useSearchSession({ services, stateContainer });
/**
* Adhoc data views functionality
*/

View file

@ -1,32 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { useSearchSession } from './use_search_session';
import { renderHook } from '@testing-library/react-hooks';
import { discoverServiceMock } from '../../../__mocks__/services';
import { savedSearchMock } from '../../../__mocks__/saved_search';
import { getDiscoverStateMock } from '../../../__mocks__/discover_state.mock';
describe('test useSearchSession', () => {
test('getting the next session id', async () => {
const stateContainer = getDiscoverStateMock({
savedSearch: savedSearchMock,
});
const nextId = 'id';
discoverServiceMock.data.search.session.start = jest.fn(() => nextId);
renderHook(() => {
return useSearchSession({
services: discoverServiceMock,
stateContainer,
});
});
expect(stateContainer.searchSessionManager.getNextSearchSessionId()).toBe('id');
});
});

View file

@ -1,43 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { useEffect } from 'react';
import { noSearchSessionStorageCapabilityMessage } from '@kbn/data-plugin/public';
import {
createSearchSessionRestorationDataProvider,
DiscoverStateContainer,
} from '../services/discover_state';
import { DiscoverServices } from '../../../build_services';
export function useSearchSession({
services,
stateContainer,
}: {
services: DiscoverServices;
stateContainer: DiscoverStateContainer;
}) {
const { data, capabilities } = services;
useEffect(() => {
data.search.session.enableStorage(
createSearchSessionRestorationDataProvider({
appStateContainer: stateContainer.appState,
data,
getSavedSearch: () => stateContainer.savedSearchState.getState(),
}),
{
isDisabled: () =>
capabilities.discover.storeSearchSession
? { disabled: false }
: {
disabled: true,
reasonText: noSearchSessionStorageCapabilityMessage,
},
}
);
}, [capabilities.discover.storeSearchSession, data, stateContainer]);
}

View file

@ -34,7 +34,7 @@ const startSync = (appState: DiscoverAppStateContainer) => {
return stop;
};
async function getState(url: string, savedSearch?: SavedSearch) {
async function getState(url: string = '/', savedSearch?: SavedSearch) {
const nextHistory = createBrowserHistory();
nextHistory.push(url);
const nextState = getDiscoverStateContainer({
@ -237,6 +237,16 @@ describe('createSearchSessionRestorationDataProvider', () => {
});
});
describe('searchSessionManager', () => {
test('getting the next session id', async () => {
const { state } = await getState();
const nextId = 'id';
discoverServiceMock.data.search.session.start = jest.fn(() => nextId);
state.actions.initializeAndSync();
expect(state.searchSessionManager.getNextSearchSessionId()).toBe(nextId);
});
});
describe('actions', () => {
beforeEach(async () => {
discoverServiceMock.data.query.timefilter.timefilter.getTime = jest.fn(() => {

View file

@ -16,6 +16,7 @@ import {
} from '@kbn/kibana-utils-plugin/public';
import {
DataPublicPluginStart,
noSearchSessionStorageCapabilityMessage,
QueryState,
SearchSessionInfoProvider,
} from '@kbn/data-plugin/public';
@ -382,6 +383,23 @@ export function getDiscoverStateContainer({
fetchData();
});
services.data.search.session.enableStorage(
createSearchSessionRestorationDataProvider({
appStateContainer,
data: services.data,
getSavedSearch: () => savedSearchContainer.getState(),
}),
{
isDisabled: () =>
services.capabilities.discover.storeSearchSession
? { disabled: false }
: {
disabled: true,
reasonText: noSearchSessionStorageCapabilityMessage,
},
}
);
return () => {
unsubscribeData();
appStateUnsubscribe();