Skip to content

Commit

Permalink
Rename AppState / DataStateContainer
Browse files Browse the repository at this point in the history
To DiscoverAppState / DiscoverDataStateContainer
  • Loading branch information
kertal committed Mar 21, 2023
1 parent 7ac10aa commit 07bb3c3
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, { useState } from 'react';
import { storiesOf } from '@storybook/react';
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react';
import { DiscoverMainProvider } from '../../../services/discover_state_provider';
import { AppState } from '../../../services/discover_app_state_container';
import { DiscoverAppState } from '../../../services/discover_app_state_container';
import { getDataViewMock } from '../../../../../__mocks__/__storybook_mocks__/get_data_view_mock';
import { withDiscoverServices } from '../../../../../__mocks__/__storybook_mocks__/with_discover_services';
import { getDocumentsLayoutProps, getPlainRecordLayoutProps } from './get_layout_props';
Expand All @@ -23,7 +23,7 @@ setHeaderActionMenuMounter(() => void 0);
const DiscoverLayoutStory = (layoutProps: DiscoverLayoutProps) => {
const [state, setState] = useState({});

const update = (newState: Partial<AppState>) => {
const update = (newState: Partial<DiscoverAppState>) => {
setState((prevState) => ({ ...prevState, ...newState }));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { buildDataTableRecord } from '../../../../utils/build_data_record';
import { EsHitRecord } from '../../../../types';
import { DiscoverMainProvider } from '../../services/discover_state_provider';
import { getDiscoverStateMock } from '../../../../__mocks__/discover_state.mock';
import { AppState } from '../../services/discover_app_state_container';
import { DiscoverAppState } from '../../services/discover_app_state_container';

setHeaderActionMenuMounter(jest.fn());

Expand Down Expand Up @@ -84,7 +84,7 @@ describe('Discover documents layout', () => {
test('should set rounded width to state on resize column', () => {
const state = {
grid: { columns: { timestamp: { width: 173 }, someField: { width: 197 } } },
} as AppState;
} as DiscoverAppState;
const container = getDiscoverStateMock({});
container.appState.update(state);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { isEqual } from 'lodash';
import { DiscoverStateContainer } from '../../services/discover_state';
import { AppState, isEqualState } from '../../services/discover_app_state_container';
import { DiscoverAppState, isEqualState } from '../../services/discover_app_state_container';
import { addLog } from '../../../../utils/add_log';
import { FetchStatus } from '../../../types';

Expand All @@ -27,9 +27,9 @@ export const buildStateSubscribe =
}: {
stateContainer: DiscoverStateContainer;
savedSearch: SavedSearch;
setState: (state: AppState) => void;
setState: (state: DiscoverAppState) => void;
}) =>
async (nextState: AppState) => {
async (nextState: DiscoverAppState) => {
const prevState = stateContainer.appState.getPrevious();
if (isEqualState(prevState, nextState)) {
addLog('[appstate] subscribe update ignored due to no changes');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ import { handleSourceColumnState } from '../../../utils/state_helpers';
import { DiscoverGridSettings } from '../../../components/discover_grid/types';

export const APP_STATE_URL_KEY = '_a';
export interface DiscoverAppStateContainer extends ReduxLikeStateContainer<AppState> {
export interface DiscoverAppStateContainer extends ReduxLikeStateContainer<DiscoverAppState> {
/**
* Returns the previous state, used for diffing e.g. if fetching new data is necessary
*/
getPrevious: () => AppState;
getPrevious: () => DiscoverAppState;
/**
* Determines if the current state is different from the initial state
*/
Expand All @@ -51,7 +51,7 @@ export interface DiscoverAppStateContainer extends ReduxLikeStateContainer<AppSt
* @param newState
* @param merge if true, the given state is merged with the current state
*/
replaceUrlState: (newPartial: AppState, merge?: boolean) => void;
replaceUrlState: (newPartial: DiscoverAppState, merge?: boolean) => void;
/**
* Resets the state by the given saved search
* @param savedSearch
Expand All @@ -70,10 +70,10 @@ export interface DiscoverAppStateContainer extends ReduxLikeStateContainer<AppSt
* @param newPartial
* @param replace
*/
update: (newPartial: AppState, replace?: boolean) => void;
update: (newPartial: DiscoverAppState, replace?: boolean) => void;
}

export interface AppState {
export interface DiscoverAppState {
/**
* Columns displayed in the table
*/
Expand Down Expand Up @@ -133,7 +133,7 @@ export interface AppState {
}

export const { Provider: DiscoverAppStateProvider, useSelector: useAppStateSelector } =
createStateContainerReactHelpers<ReduxLikeStateContainer<AppState>>();
createStateContainerReactHelpers<ReduxLikeStateContainer<DiscoverAppState>>();

/**
* This is the app state container for Discover main, it's responsible for syncing state with the URL
Expand All @@ -150,13 +150,13 @@ export const getDiscoverAppStateContainer = ({
savedSearch: SavedSearch;
services: DiscoverServices;
}): DiscoverAppStateContainer => {
let previousState: AppState = {};
let previousState: DiscoverAppState = {};
let initialState = getInitialState(stateStorage, savedSearch, services);
const appStateContainer = createStateContainer<AppState>(initialState);
const appStateContainer = createStateContainer<DiscoverAppState>(initialState);

const enhancedAppContainer = {
...appStateContainer,
set: (value: AppState | null) => {
set: (value: DiscoverAppState | null) => {
if (value) {
previousState = appStateContainer.getState();
appStateContainer.set(value);
Expand All @@ -173,7 +173,7 @@ export const getDiscoverAppStateContainer = ({
initialState = appStateContainer.getState();
};

const replaceUrlState = async (newPartial: AppState = {}, merge = true) => {
const replaceUrlState = async (newPartial: DiscoverAppState = {}, merge = true) => {
addLog('[appState] replaceUrlState', { newPartial, merge });
const state = merge ? { ...appStateContainer.getState(), ...newPartial } : newPartial;
await stateStorage.set(APP_STATE_URL_KEY, state, { replace: true });
Expand Down Expand Up @@ -247,7 +247,7 @@ export const getDiscoverAppStateContainer = ({
appStateContainer.set(nextAppState);
};

const update = (newPartial: AppState, replace = false) => {
const update = (newPartial: DiscoverAppState, replace = false) => {
addLog('[appState] update', { newPartial, replace });
if (replace) {
return replaceUrlState(newPartial);
Expand All @@ -272,7 +272,7 @@ export const getDiscoverAppStateContainer = ({
};
};

export interface AppStateUrl extends Omit<AppState, 'sort'> {
export interface AppStateUrl extends Omit<DiscoverAppState, 'sort'> {
/**
* Necessary to take care of legacy links [fieldName,direction]
*/
Expand Down Expand Up @@ -304,7 +304,10 @@ function getInitialState(
* Helper function to merge a given new state with the existing state and to set the given state
* container
*/
export function setState(stateContainer: ReduxLikeStateContainer<AppState>, newState: AppState) {
export function setState(
stateContainer: ReduxLikeStateContainer<DiscoverAppState>,
newState: DiscoverAppState
) {
addLog('[appstate] setState', { newState });
const oldState = stateContainer.getState();
const mergedState = { ...oldState, ...newState };
Expand All @@ -329,7 +332,7 @@ export function isEqualFilters(filtersA?: Filter[] | Filter, filtersB?: Filter[]
* Helper function to compare 2 different state, is needed since comparing filters
* works differently
*/
export function isEqualState(stateA: AppState, stateB: AppState) {
export function isEqualState(stateA: DiscoverAppState, stateB: DiscoverAppState) {
if (!stateA && !stateB) {
return true;
} else if (!stateA || !stateB) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { AggregateQuery, Query } from '@kbn/es-query';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { ReduxLikeStateContainer } from '@kbn/kibana-utils-plugin/common';
import { getRawRecordType } from '../utils/get_raw_record_type';
import { AppState } from './discover_app_state_container';
import { DiscoverAppState } from './discover_app_state_container';
import { DiscoverServices } from '../../../build_services';
import { DiscoverSearchSessionManager } from './discover_search_session';
import { SEARCH_FIELDS_FROM_SOURCE, SEARCH_ON_PAGE_LOAD_SETTING } from '../../../../common';
Expand Down Expand Up @@ -84,7 +84,7 @@ export interface DataAvailableFieldsMsg extends DataMsg {
fields?: string[];
}

export interface DataStateContainer {
export interface DiscoverDataStateContainer {
/**
* Implicitly starting fetching data from ES
*/
Expand Down Expand Up @@ -134,10 +134,10 @@ export function getDataStateContainer({
}: {
services: DiscoverServices;
searchSessionManager: DiscoverSearchSessionManager;
getAppState: () => AppState;
getAppState: () => DiscoverAppState;
getSavedSearch: () => SavedSearch;
appStateContainer: ReduxLikeStateContainer<AppState>;
}): DataStateContainer {
appStateContainer: ReduxLikeStateContainer<DiscoverAppState>;
}): DiscoverDataStateContainer {
const { data, uiSettings, toastNotifications } = services;
const { timefilter } = data.query.timefilter;
const inspectorAdapters = { requests: new RequestAdapter() };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
import { DataView } from '@kbn/data-views-plugin/public';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { loadDataView, resolveDataView } from '../utils/resolve_data_view';
import { DataStateContainer, getDataStateContainer } from './discover_data_state_container';
import { DiscoverDataStateContainer, getDataStateContainer } from './discover_data_state_container';
import { DiscoverSearchSessionManager } from './discover_search_session';
import { DISCOVER_APP_LOCATOR, DiscoverAppLocatorParams } from '../../../../common';
import {
AppState,
DiscoverAppState,
DiscoverAppStateContainer,
getDiscoverAppStateContainer,
GLOBAL_STATE_URL_KEY,
Expand Down Expand Up @@ -72,7 +72,7 @@ export interface DiscoverStateContainer {
/**
* Data fetching related state
**/
dataState: DataStateContainer;
dataState: DiscoverDataStateContainer;
/**
* functions executed by UI
*/
Expand Down Expand Up @@ -233,7 +233,7 @@ export function getDiscoverStateContainer({
}

export function createSearchSessionRestorationDataProvider(deps: {
appStateContainer: StateContainer<AppState>;
appStateContainer: StateContainer<DiscoverAppState>;
data: DataPublicPluginStart;
getSavedSearch: () => SavedSearch;
}): SearchSessionInfoProvider {
Expand Down Expand Up @@ -272,7 +272,7 @@ function createUrlGeneratorState({
getSavedSearchId,
shouldRestoreSearchSession,
}: {
appStateContainer: StateContainer<AppState>;
appStateContainer: StateContainer<DiscoverAppState>;
data: DataPublicPluginStart;
getSavedSearchId: () => string | undefined;
shouldRestoreSearchSession: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Side Public License, v 1.
*/
import { isOfAggregateQueryType } from '@kbn/es-query';
import { AppState, AppStateUrl } from '../services/discover_app_state_container';
import { DiscoverAppState, AppStateUrl } from '../services/discover_app_state_container';
import { migrateLegacyQuery } from '../../../utils/migrate_legacy_query';

/**
* Takes care of the given url state, migrates legacy props and cleans up empty props
* @param appStateFromUrl
*/
export function cleanupUrlState(appStateFromUrl: AppStateUrl): AppState {
export function cleanupUrlState(appStateFromUrl: AppStateUrl): DiscoverAppState {
if (
appStateFromUrl &&
appStateFromUrl.query &&
Expand Down Expand Up @@ -46,5 +46,5 @@ export function cleanupUrlState(appStateFromUrl: AppStateUrl): AppState {
delete appStateFromUrl.rowsPerPage;
}

return appStateFromUrl as AppState;
return appStateFromUrl as DiscoverAppState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { fetchDocuments } from './fetch_documents';
import { fetchSql } from './fetch_sql';
import { buildDataTableRecord } from '../../../utils/build_data_record';
import { dataViewMock } from '../../../__mocks__/data_view';
import { AppState } from '../services/discover_app_state_container';
import { DiscoverAppState } from '../services/discover_app_state_container';

jest.mock('./fetch_documents', () => ({
fetchDocuments: jest.fn().mockResolvedValue([]),
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('test fetchAll', () => {
getState: () => {
return { interval: 'auto' };
},
} as ReduxLikeStateContainer<AppState>,
} as ReduxLikeStateContainer<DiscoverAppState>,
abortController: new AbortController(),
data: discoverServiceMock.data,
inspectorAdapters: { requests: new RequestAdapter() },
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('test fetchAll', () => {
getState: () => {
return { interval: 'auto', query };
},
} as unknown as ReduxLikeStateContainer<AppState>,
} as unknown as ReduxLikeStateContainer<DiscoverAppState>,
abortController: new AbortController(),
data: discoverServiceMock.data,
inspectorAdapters: { requests: new RequestAdapter() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Adapters } from '@kbn/inspector-plugin/common';
import { ReduxLikeStateContainer } from '@kbn/kibana-utils-plugin/common';
import type { SavedSearch, SortOrder } from '@kbn/saved-search-plugin/public';
import { BehaviorSubject, filter, firstValueFrom, map, merge, scan } from 'rxjs';
import { AppState } from '../services/discover_app_state_container';
import { DiscoverAppState } from '../services/discover_app_state_container';
import { getRawRecordType } from './get_raw_record_type';
import {
checkHitCount,
Expand All @@ -29,7 +29,7 @@ import { fetchSql } from './fetch_sql';

export interface FetchDeps {
abortController: AbortController;
appStateContainer: ReduxLikeStateContainer<AppState>;
appStateContainer: ReduxLikeStateContainer<DiscoverAppState>;
data: DataPublicPluginStart;
initialFetchStatus: FetchStatus;
inspectorAdapters: Adapters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { cloneDeep, isEqual } from 'lodash';
import { IUiSettingsClient } from '@kbn/core/public';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { getChartHidden } from '@kbn/unified-histogram-plugin/public';
import { AppState } from '../services/discover_app_state_container';
import { DiscoverAppState } from '../services/discover_app_state_container';
import { DiscoverServices } from '../../../build_services';
import { getDefaultSort, getSortArray } from '../../../utils/sorting';
import {
Expand Down Expand Up @@ -49,7 +49,7 @@ export function getStateDefaults({
const columns = getDefaultColumns(savedSearch, uiSettings);
const chartHidden = getChartHidden(storage, 'discover');

const defaultState: AppState = {
const defaultState: DiscoverAppState = {
query,
sort: !sort.length
? getDefaultSort(
Expand All @@ -61,7 +61,7 @@ export function getStateDefaults({
columns,
index: dataView?.id,
interval: 'auto',
filters: cloneDeep(searchSource.getOwnField('filter')) as AppState['filters'],
filters: cloneDeep(searchSource.getOwnField('filter')) as DiscoverAppState['filters'],
hideChart: typeof chartHidden === 'boolean' ? chartHidden : undefined,
viewMode: undefined,
hideAggregatedPreview: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isOfAggregateQueryType } from '@kbn/es-query';
import { DataView } from '@kbn/data-views-plugin/public';
import { SavedObjectSaveOpts } from '@kbn/saved-objects-plugin/public';
import { SavedSearch, SortOrder, saveSavedSearch } from '@kbn/saved-search-plugin/public';
import { AppState } from '../services/discover_app_state_container';
import { DiscoverAppState } from '../services/discover_app_state_container';
import { updateSearchSource } from './update_search_source';
import { DiscoverServices } from '../../../build_services';
/**
Expand All @@ -30,7 +30,7 @@ export async function persistSavedSearch(
onSuccess: (id: string) => void;
saveOptions: SavedObjectSaveOpts;
services: DiscoverServices;
state: AppState;
state: DiscoverAppState;
}
) {
updateSearchSource(savedSearch.searchSource, true, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import { configMock } from '../../../__mocks__/config';
import { dataViewMock } from '../../../__mocks__/data_view';
import { dataViewsMock } from '../../../__mocks__/data_views';
import { Capabilities } from '@kbn/core/types';
import { AppState } from '../../../application/main/services/discover_app_state_container';
import { DiscoverAppState } from '../../../application/main/services/discover_app_state_container';

function getStateColumnAction(state: AppState, setAppState: (state: Partial<AppState>) => void) {
function getStateColumnAction(
state: DiscoverAppState,
setAppState: (state: Partial<DiscoverAppState>) => void
) {
return getStateColumnActions({
capabilities: {
discover: {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/discover/public/utils/get_sharing_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
import type { Filter } from '@kbn/es-query';
import type { SavedSearch, SortOrder } from '@kbn/saved-search-plugin/public';
import {
AppState,
DiscoverAppState,
isEqualFilters,
} from '../application/main/services/discover_app_state_container';
import { getSortForSearchSource } from './sorting';
Expand All @@ -31,7 +31,7 @@ import {
*/
export async function getSharingData(
currentSearchSource: ISearchSource,
state: AppState | SavedSearch,
state: DiscoverAppState | SavedSearch,
services: { uiSettings: IUiSettingsClient; data: DataPublicPluginStart }
) {
const { uiSettings: config, data } = services;
Expand Down

0 comments on commit 07bb3c3

Please sign in to comment.