diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/mocks.ts b/x-pack/plugins/endpoint/public/applications/endpoint/mocks.ts index 86431139a0389..e1a90b4a416dc 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/mocks.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/mocks.ts @@ -27,10 +27,16 @@ type DataMock = Omit & { }; }; +/** + * Type for our app's depsStart (plugin start dependencies) + */ export interface DepsStartMock { data: DataMock; } +/** + * Returns a mock of our app's depsStart (plugin start dependencies) + */ export const depsStartMock: () => DepsStartMock = () => { const dataMock: DataMock = (dataPluginMock.createStartContract() as unknown) as DataMock; dataMock.indexPatterns.getFieldsForWildcard = jest.fn(); diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/middleware.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/middleware.ts index 67de586d13f06..b37ba0c0983d3 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/middleware.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/middleware.ts @@ -15,8 +15,6 @@ import { EndpointAppConstants } from '../../../../../common/types'; export const alertMiddlewareFactory: MiddlewareFactory = (coreStart, depsStart) => { async function fetchIndexPatterns(): Promise { const { indexPatterns } = depsStart.data; - // TODO: what's the best way to get the index pattern? - // const pattern = await indexPatterns.get('287e7b60-4394-11ea-aaac-c76376668d76'); const indexName = EndpointAppConstants.ALERT_INDEX_NAME; const fields = await indexPatterns.getFieldsForWildcard({ pattern: indexName }); const indexPattern: IIndexPattern = { @@ -31,7 +29,6 @@ export const alertMiddlewareFactory: MiddlewareFactory = (coreSt next(action); const state = api.getState(); if (action.type === 'userChangedUrl' && isOnAlertPage(state)) { - // TODO: only fetch index pattern once when the page loads const patterns = await fetchIndexPatterns(); api.dispatch({ type: 'serverReturnedSearchBarIndexPatterns', payload: patterns }); diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts index b562bfc67e81c..68731bb3f307f 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts @@ -78,6 +78,10 @@ export const uiQueryParams: ( } ); +/** + * Parses the ui query params and returns a object that represents the query used by the SearchBar component. + * If the query url param is undefined, a default is returned. + */ export const searchBarQuery: (state: AlertListState) => Query = createSelector( uiQueryParams, ({ query }) => { @@ -89,17 +93,24 @@ export const searchBarQuery: (state: AlertListState) => Query = createSelector( } ); +/** + * Parses the ui query params and returns a rison encoded string that represents the search bar's date range. + * A default is provided if 'date_range' is not present in the url params. + */ export const encodedSearchBarDateRange: (state: AlertListState) => string = createSelector( uiQueryParams, ({ date_range: dateRange }) => { if (dateRange === undefined) { - return encode({ from: 'now-15m', to: 'now' }); + return encode({ from: 'now-24h', to: 'now' }); } else { return dateRange; } } ); +/** + * Parses the ui query params and returns a object that represents the dateRange used by the SearchBar component. + */ export const searchBarDateRange: (state: AlertListState) => TimeRange = createSelector( encodedSearchBarDateRange, encodedDateRange => { @@ -107,6 +118,10 @@ export const searchBarDateRange: (state: AlertListState) => TimeRange = createSe } ); +/** + * Parses the ui query params and returns an array of filters used by the SearchBar component. + * If the 'filters' param is not present, a default is returned. + */ export const searchBarFilters: (state: AlertListState) => Filter[] = createSelector( uiQueryParams, ({ filters }) => { @@ -118,6 +133,11 @@ export const searchBarFilters: (state: AlertListState) => Filter[] = createSelec } ); +/** + * Returns the indexPatterns used by the SearchBar component + */ +export const searchBarIndexPatterns = (state: AlertListState) => state.searchBar.patterns; + /** * query params to use when requesting alert data. */ @@ -157,5 +177,3 @@ export const selectedAlertIsLegacyEndpointEvent: ( } return 'endgame' in event; }); - -export const searchBarIndexPatterns = (state: AlertListState) => state.searchBar.patterns; diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/index.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/index.ts index 08e8625d2c08b..f275fbcbbb5e6 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/index.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/index.ts @@ -49,11 +49,17 @@ export const substateMiddlewareFactory = ( }; }; +/** + * @param middlewareDeps Optionally create the store without any middleware. This is useful for testing the store w/o side effects. + */ export const appStoreFactory: (middlewareDeps?: { /** * Allow middleware to communicate with Kibana core. */ coreStart: CoreStart; + /** + * Give middleware access to plugin start dependencies. + */ depsStart: EndpointPluginStartDependencies; }) => Store = middlewareDeps => { let middleware; diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/types.ts b/x-pack/plugins/endpoint/public/applications/endpoint/types.ts index 8d52012accffe..3b70a580436fe 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/types.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/types.ts @@ -129,7 +129,7 @@ export interface AlertListState { /** Specific Alert data to be shown in the details view */ readonly alertDetails?: Immutable; - /** Search bar filters, query, dateRange, and index */ + /** Search bar state including indexPatterns */ readonly searchBar: AlertsSearchBarState; }