diff --git a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_exploration/components/exploration_query_bar/exploration_query_bar.tsx b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_exploration/components/exploration_query_bar/exploration_query_bar.tsx index b079fc154713e..dd451958e2646 100644 --- a/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_exploration/components/exploration_query_bar/exploration_query_bar.tsx +++ b/x-pack/plugins/ml/public/application/data_frame_analytics/pages/analytics_exploration/components/exploration_query_bar/exploration_query_bar.tsx @@ -5,10 +5,11 @@ * 2.0. */ -import React, { FC, useEffect, useState } from 'react'; +import React, { FC, useEffect, useMemo, useState } from 'react'; import { EuiButtonGroup, EuiCode, EuiFlexGroup, EuiFlexItem, EuiInputPopover } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; +import { debounce } from 'lodash'; import { Dictionary } from '../../../../../../../common/types/common'; import { IIndexPattern } from '../../../../../../../../../../src/plugins/data/common/index_patterns'; import { @@ -59,6 +60,33 @@ export const ExplorationQueryBar: FC = ({ const searchChangeHandler = (q: Query) => setSearchInput(q); + const regex = useMemo(() => new RegExp(`${filters?.columnId}\s?:\s?(true|false)`, 'g'), [ + filters?.columnId, + ]); + + /** + * Restoring state from the URL once on load. If a filter option is active + * in the url set the corresponding options button to selected mode. + */ + useEffect(function updateIdToSelectedMap() { + if (filters !== undefined) { + const match: string[] | null = query.query.match(regex); + let filterKeyInEffect: string | undefined; + + if (match !== null && match[0].includes('true')) { + // set { training: true } + filterKeyInEffect = Object.keys(filters.key).find((i) => filters.key[i] === true); + } else if (match !== null && match[0].includes('false')) { + // set { testing: true } + filterKeyInEffect = Object.keys(filters.key).find((i) => filters.key[i] === false); + } + + if (filterKeyInEffect) { + setIdToSelectedMap({ [filterKeyInEffect]: true }); + } + } + }, []); + /** * Component is responsible for parsing the query string, * hence it should sync submitted query string. @@ -107,11 +135,10 @@ export const ExplorationQueryBar: FC = ({ }); }; - const handleFilterUpdate = (optionId: string, currentIdToSelectedMap: any) => { + const debouncedHandleFilterUpdate = debounce((optionId: string, currentIdToSelectedMap: any) => { let newQuery = ''; const filterValue = filters?.key[optionId]; const filterQueryString = `${filters?.columnId}:${filterValue}`; - const regex = new RegExp(`${filters?.columnId}\s?:\s?(true|false)`, 'g'); // Toggling selected optionId to 'off' - remove column id query from filter if (currentIdToSelectedMap[optionId] === false) { @@ -140,7 +167,7 @@ export const ExplorationQueryBar: FC = ({ setSearchInput(newSearchInput); searchSubmitHandler(newSearchInput, true); - }; + }, 200); return ( = ({ onChange={(optionId: string) => { const newIdToSelectedMap = { [optionId]: !idToSelectedMap[optionId] }; setIdToSelectedMap(newIdToSelectedMap); - handleFilterUpdate(optionId, newIdToSelectedMap); + debouncedHandleFilterUpdate(optionId, newIdToSelectedMap); }} />