diff --git a/src/plugins/data_explorer/public/index.ts b/src/plugins/data_explorer/public/index.ts index cb33d2b7d90c..635a0ec285db 100644 --- a/src/plugins/data_explorer/public/index.ts +++ b/src/plugins/data_explorer/public/index.ts @@ -14,10 +14,4 @@ export function plugin() { } export { DataExplorerPluginSetup, DataExplorerPluginStart, DataExplorerServices } from './types'; export { ViewProps, ViewDefinition, DefaultViewState } from './services/view_service'; -export { - RootState, - Store, - useTypedSelector, - useTypedDispatch, - setIndexPattern, -} from './utils/state_management'; +export { RootState, useTypedSelector, useTypedDispatch } from './utils/state_management'; diff --git a/src/plugins/discover/public/application/utils/state_management/index.ts b/src/plugins/discover/public/application/utils/state_management/index.ts index 9e0d5bc64ffd..d72cc772e6c4 100644 --- a/src/plugins/discover/public/application/utils/state_management/index.ts +++ b/src/plugins/discover/public/application/utils/state_management/index.ts @@ -4,13 +4,7 @@ */ import { TypedUseSelectorHook } from 'react-redux'; -import { - RootState, - Store as StoreType, - setIndexPattern as updateIndexPattern, - useTypedDispatch, - useTypedSelector, -} from '../../../../../data_explorer/public'; +import { RootState, useTypedDispatch, useTypedSelector } from '../../../../../data_explorer/public'; import { DiscoverState } from './discover_slice'; export * from './discover_slice'; @@ -21,4 +15,3 @@ export interface DiscoverRootState extends RootState { export const useSelector: TypedUseSelectorHook = useTypedSelector; export const useDispatch = useTypedDispatch; -export { StoreType, updateIndexPattern }; diff --git a/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx b/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx index 8d9966af0ec0..e292303d9f8d 100644 --- a/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx @@ -35,7 +35,7 @@ export const DiscoverTable = ({ history }: Props) => { query: { filterManager }, }, } = services; - const { data$, refetch$, indexPattern, savedSearch } = useDiscoverContext(); + const { data$, refetch$, indexPattern } = useDiscoverContext(); const [fetchState, setFetchState] = useState({ status: data$.getValue().status, rows: [], diff --git a/src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts b/src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts index 10a795abac37..872639107987 100644 --- a/src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts +++ b/src/plugins/discover/public/application/view_components/utils/use_index_pattern.ts @@ -6,72 +6,46 @@ import { useEffect, useState } from 'react'; import { i18n } from '@osd/i18n'; import { IndexPattern } from '../../../../../data/public'; -import { useSelector, updateIndexPattern, StoreType } from '../../utils/state_management'; +import { useSelector } from '../../utils/state_management'; import { DiscoverServices } from '../../../build_services'; -import { getIndexPatternId } from '../../helpers/get_index_pattern_id'; -/** - * Custom hook to fetch and manage the index pattern based on the provided services. - * - * This hook does the following: - * 1. Check if there's an `indexPatternId` from the state. - * 2. If not, fetch a list of index patterns, determine the default, and update the store with it. - * 3. Once an `indexPatternId` is determined (either from the state or by fetching the default), - * it fetches the details of the index pattern. - * 4. If there's any error fetching the index pattern details, a warning notification is shown. - * - * @param services - The services needed to fetch the index patterns and show notifications. - * @param store - The redux store in data_explorer to dispatch actions. - * @returns - The fetched index pattern. - */ -export const useIndexPattern = (services: DiscoverServices, store: StoreType) => { - const indexPatternIdFromState = useSelector((state) => state.metadata.indexPattern); +export const useIndexPattern = (services: DiscoverServices) => { + const indexPatternId = useSelector((state) => state.metadata.indexPattern); const [indexPattern, setIndexPattern] = useState(undefined); - const { data, toastNotifications, uiSettings: config } = services; + const { data, toastNotifications } = services; useEffect(() => { let isMounted = true; + if (!indexPatternId) return; + const indexPatternMissingWarning = i18n.translate( + 'discover.valueIsNotConfiguredIndexPatternIDWarningTitle', + { + defaultMessage: '{id} is not a configured index pattern ID', + values: { + id: `"${indexPatternId}"`, + }, + } + ); - const fetchIndexPatternDetails = (id: string) => { - data.indexPatterns - .get(id) - .then((result) => { - if (isMounted) { - setIndexPattern(result); - } - }) - .catch(() => { - if (isMounted) { - const indexPatternMissingWarning = i18n.translate( - 'discover.valueIsNotConfiguredIndexPatternIDWarningTitle', - { - defaultMessage: '{id} is not a configured index pattern ID', - values: { - id: `"${id}"`, - }, - } - ); - toastNotifications.addDanger({ - title: indexPatternMissingWarning, - }); - } - }); - }; - - if (!indexPatternIdFromState) { - data.indexPatterns.getCache().then((indexPatternList) => { - const newId = getIndexPatternId('', indexPatternList, config.get('defaultIndex')); - store!.dispatch(updateIndexPattern(newId)); - fetchIndexPatternDetails(newId); + data.indexPatterns + .get(indexPatternId) + .then((result) => { + if (isMounted) { + setIndexPattern(result); + } + }) + .catch(() => { + if (isMounted) { + toastNotifications.addDanger({ + title: indexPatternMissingWarning, + }); + } }); - } else { - fetchIndexPatternDetails(indexPatternIdFromState); - } return () => { isMounted = false; }; - }, [indexPatternIdFromState, data.indexPatterns, toastNotifications, config, store]); + }, [indexPatternId, data.indexPatterns, toastNotifications]); return indexPattern; }; diff --git a/src/plugins/discover/public/application/view_components/utils/use_search.ts b/src/plugins/discover/public/application/view_components/utils/use_search.ts index b3fbefcdfad1..2560fc6d3567 100644 --- a/src/plugins/discover/public/application/view_components/utils/use_search.ts +++ b/src/plugins/discover/public/application/view_components/utils/use_search.ts @@ -69,8 +69,8 @@ export type RefetchSubject = Subject; export const useSearch = (services: DiscoverServices) => { const [savedSearch, setSavedSearch] = useState(undefined); const { savedSearch: savedSearchId, sort, interval } = useSelector((state) => state.discover); - const { data, filterManager, getSavedSearchById, core, toastNotifications, store } = services; - const indexPattern = useIndexPattern(services, store); + const indexPattern = useIndexPattern(services); + const { data, filterManager, getSavedSearchById, core, toastNotifications } = services; const timefilter = data.query.timefilter.timefilter; const fetchStateRef = useRef<{ abortController: AbortController | undefined;