From 9de1e9e205f68dcc9fbf21c9756002524ab1af3b Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Fri, 26 Apr 2024 18:06:13 +0200 Subject: [PATCH 1/6] fix data view checks --- x-pack/plugins/transform/common/constants.ts | 1 + .../transform/public/app/hooks/index.ts | 1 + .../hooks/use_get_data_view_ids_with_title.ts | 24 ++++++++++ .../app/hooks/use_search_items/common.ts | 15 +----- .../use_search_items/use_search_items.ts | 4 +- .../action_clone/use_clone_action.tsx | 31 ++++++++----- .../action_discover/use_action_discover.tsx | 46 ++++++++----------- 7 files changed, 67 insertions(+), 55 deletions(-) create mode 100644 x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts diff --git a/x-pack/plugins/transform/common/constants.ts b/x-pack/plugins/transform/common/constants.ts index 761d9e4c79c72..8b69c4447431f 100644 --- a/x-pack/plugins/transform/common/constants.ts +++ b/x-pack/plugins/transform/common/constants.ts @@ -37,6 +37,7 @@ export const addExternalBasePath = (uri: string): string => `${EXTERNAL_API_BASE export const TRANSFORM_REACT_QUERY_KEYS = { DATA_SEARCH: 'transform.data_search', DATA_VIEW_EXISTS: 'transform.data_view_exists', + GET_DATA_VIEW_IDS_WITH_TITLE: 'transform.get_data_view_ids_with_title', GET_DATA_VIEW_TITLES: 'transform.get_data_view_titles', GET_ES_INDICES: 'transform.get_es_indices', GET_ES_INGEST_PIPELINES: 'transform.get_es_ingest_pipelines', diff --git a/x-pack/plugins/transform/public/app/hooks/index.ts b/x-pack/plugins/transform/public/app/hooks/index.ts index 749706f97cd70..aa900cc961d1e 100644 --- a/x-pack/plugins/transform/public/app/hooks/index.ts +++ b/x-pack/plugins/transform/public/app/hooks/index.ts @@ -7,6 +7,7 @@ export { useCreateTransform } from './use_create_transform'; export { useDocumentationLinks } from './use_documentation_links'; +export { useGetDataViewIdsWithTitle } from './use_get_data_view_ids_with_title'; export { useGetDataViewTitles } from './use_get_data_view_titles'; export { useGetEsIndices } from './use_get_es_indices'; export { useGetEsIngestPipelines } from './use_get_es_ingest_pipelines'; diff --git a/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts b/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts new file mode 100644 index 0000000000000..0890e86af3f7f --- /dev/null +++ b/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts @@ -0,0 +1,24 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { useQuery } from '@tanstack/react-query'; + +import type { IHttpFetchError } from '@kbn/core-http-browser'; +import type { DataViewListItem } from '@kbn/data-views-plugin/public'; + +import { TRANSFORM_REACT_QUERY_KEYS } from '../../../common/constants'; + +import { useAppDependencies } from '../app_dependencies'; + +export const useGetDataViewIdsWithTitle = () => { + const { data } = useAppDependencies(); + + return useQuery( + [TRANSFORM_REACT_QUERY_KEYS.GET_DATA_VIEW_IDS_WITH_TITLE], + async () => await data.dataViews.getIdsWithTitle() + ); +}; diff --git a/x-pack/plugins/transform/public/app/hooks/use_search_items/common.ts b/x-pack/plugins/transform/public/app/hooks/use_search_items/common.ts index 0432dca913f8f..2277ca7c48e11 100644 --- a/x-pack/plugins/transform/public/app/hooks/use_search_items/common.ts +++ b/x-pack/plugins/transform/public/app/hooks/use_search_items/common.ts @@ -8,24 +8,11 @@ import { buildEsQuery } from '@kbn/es-query'; import type { IUiSettingsClient } from '@kbn/core/public'; import { getEsQueryConfig } from '@kbn/data-plugin/public'; -import type { DataView, DataViewsContract } from '@kbn/data-views-plugin/public'; +import type { DataView } from '@kbn/data-views-plugin/public'; import { matchAllQuery } from '@kbn/ml-query-utils'; import { isDataView } from '../../../../common/types/data_view'; -let dataViewCache: DataView[] = []; - -export let refreshDataViews: () => Promise; - -export async function loadDataViews(dataViewsContract: DataViewsContract) { - dataViewCache = await dataViewsContract.find('*', 10000); - return dataViewCache; -} - -export function getDataViewIdByTitle(dataViewTitle: string): string | undefined { - return dataViewCache.find(({ title }) => title === dataViewTitle)?.id; -} - type CombinedQuery = Record<'bool', any> | object; export interface SearchItems { diff --git a/x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts b/x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts index 502f167582a98..ed3b291a2d7f8 100644 --- a/x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts +++ b/x-pack/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts @@ -10,7 +10,7 @@ import { i18n } from '@kbn/i18n'; import { isDataView } from '../../../../common/types/data_view'; import { useAppDependencies } from '../../app_dependencies'; import type { SearchItems } from './common'; -import { createSearchItems, getDataViewIdByTitle, loadDataViews } from './common'; +import { createSearchItems } from './common'; export const useSearchItems = (defaultSavedObjectId: string | undefined) => { const [savedObjectId, setSavedObjectId] = useState(defaultSavedObjectId); @@ -73,8 +73,6 @@ export const useSearchItems = (defaultSavedObjectId: string | undefined) => { return { error, - getDataViewIdByTitle, - loadDataViews, searchItems, setSavedObjectId, }; diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx index a33afe14bf04f..7e02a14da33d8 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx @@ -11,32 +11,33 @@ import { i18n } from '@kbn/i18n'; import type { TransformListAction, TransformListRow } from '../../../../common'; import { SECTION_SLUG } from '../../../../common/constants'; -import { useTransformCapabilities, useSearchItems } from '../../../../hooks'; -import { useAppDependencies, useToastNotifications } from '../../../../app_dependencies'; +import { useGetDataViewIdsWithTitle, useTransformCapabilities } from '../../../../hooks'; +import { useToastNotifications } from '../../../../app_dependencies'; import { cloneActionNameText, CloneActionName } from './clone_action_name'; export type CloneAction = ReturnType; export const useCloneAction = (forceDisable: boolean, transformNodes: number) => { const history = useHistory(); - const appDeps = useAppDependencies(); - const dataViewsContract = appDeps.data.dataViews; const toastNotifications = useToastNotifications(); - const { getDataViewIdByTitle, loadDataViews } = useSearchItems(undefined); + const { data: dataViewListItems } = useGetDataViewIdsWithTitle(); const { canCreateTransform } = useTransformCapabilities(); const clickHandler = useCallback( async (item: TransformListRow) => { try { - await loadDataViews(dataViewsContract); + if (!dataViewListItems) { + return; + } + const dataViewTitle = Array.isArray(item.config.source.index) ? item.config.source.index.join(',') : item.config.source.index; - const dataViewId = getDataViewIdByTitle(dataViewTitle); + const dataViewListItem = dataViewListItems.find((d) => d.title === dataViewTitle); - if (dataViewId === undefined) { + if (dataViewListItem === undefined) { toastNotifications.addDanger( i18n.translate('xpack.transform.clone.noDataViewErrorPromptText', { defaultMessage: @@ -45,7 +46,9 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) => }) ); } else { - history.push(`/${SECTION_SLUG.CLONE_TRANSFORM}/${item.id}?dataViewId=${dataViewId}`); + history.push( + `/${SECTION_SLUG.CLONE_TRANSFORM}/${item.id}?dataViewId=${dataViewListItem.id}` + ); } } catch (e) { toastNotifications.addError(e, { @@ -55,20 +58,24 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) => }); } }, - [history, dataViewsContract, toastNotifications, loadDataViews, getDataViewIdByTitle] + [dataViewListItems, history, toastNotifications] ); const action: TransformListAction = useMemo( () => ({ name: (item: TransformListRow) => , - enabled: () => canCreateTransform && !forceDisable && transformNodes > 0, + enabled: () => + dataViewListItems !== undefined && + canCreateTransform && + !forceDisable && + transformNodes > 0, description: cloneActionNameText, icon: 'copy', type: 'icon', onClick: clickHandler, 'data-test-subj': 'transformActionClone', }), - [canCreateTransform, forceDisable, clickHandler, transformNodes] + [canCreateTransform, dataViewListItems, forceDisable, clickHandler, transformNodes] ); return { action }; diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_discover/use_action_discover.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_discover/use_action_discover.tsx index 2d4515b479f17..66394ba3412da 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_discover/use_action_discover.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_discover/use_action_discover.tsx @@ -5,13 +5,13 @@ * 2.0. */ -import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import React, { useCallback, useMemo } from 'react'; import { DISCOVER_APP_LOCATOR } from '@kbn/discover-plugin/common'; import type { TransformListAction, TransformListRow } from '../../../../common'; -import { useSearchItems } from '../../../../hooks/use_search_items'; import { useAppDependencies } from '../../../../app_dependencies'; +import { useGetDataViewIdsWithTitle } from '../../../../hooks'; import { isDiscoverActionDisabled, @@ -23,42 +23,36 @@ export type DiscoverAction = ReturnType; export const useDiscoverAction = (forceDisable: boolean) => { const { share, - data: { dataViews: dataViewsContract }, application: { capabilities }, } = useAppDependencies(); const isDiscoverAvailable = !!capabilities.discover?.show; - const { getDataViewIdByTitle, loadDataViews } = useSearchItems(undefined); - - const [dataViewsLoaded, setDataViewsLoaded] = useState(false); - - useEffect(() => { - async function checkDataViewAvailability() { - await loadDataViews(dataViewsContract); - setDataViewsLoaded(true); - } - - checkDataViewAvailability(); - }, [loadDataViews, dataViewsContract]); + const { data: dataViewListItems } = useGetDataViewIdsWithTitle(); const clickHandler = useCallback( (item: TransformListRow) => { const locator = share.url.locators.get(DISCOVER_APP_LOCATOR); - if (!locator) return; - const dataViewId = getDataViewIdByTitle(item.config.dest.index); - locator.navigateSync({ - indexPatternId: dataViewId, - }); + if (!locator || !dataViewListItems) return; + const dataView = dataViewListItems.find((d) => d.title === item.config.dest.index); + if (dataView) { + locator.navigateSync({ + indexPatternId: dataView.id, + }); + } }, - [getDataViewIdByTitle, share] + [dataViewListItems, share] ); const dataViewExists = useCallback( - (item: TransformListRow) => { - const dataViewId = getDataViewIdByTitle(item.config.dest.index); + (item: TransformListRow): boolean => { + if (!dataViewListItems) { + return false; + } + + const dataViewId = dataViewListItems.find((d) => d.title === item.config.dest.index); return dataViewId !== undefined; }, - [getDataViewIdByTitle] + [dataViewListItems] ); const action: TransformListAction = useMemo( @@ -68,14 +62,14 @@ export const useDiscoverAction = (forceDisable: boolean) => { }, available: () => isDiscoverAvailable, enabled: (item: TransformListRow) => - dataViewsLoaded && !isDiscoverActionDisabled([item], forceDisable, dataViewExists(item)), + !isDiscoverActionDisabled([item], forceDisable, dataViewExists(item)), description: discoverActionNameText, icon: 'visTable', type: 'icon', onClick: clickHandler, 'data-test-subj': 'transformActionDiscover', }), - [forceDisable, dataViewExists, dataViewsLoaded, isDiscoverAvailable, clickHandler] + [forceDisable, dataViewExists, isDiscoverAvailable, clickHandler] ); return { action }; From 2e6abe594e5106b77d5bb7fbb6956ad94d6d8ec0 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 May 2024 11:03:09 +0200 Subject: [PATCH 2/6] fix caching --- .../public/app/hooks/use_get_data_view_ids_with_title.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts b/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts index 0890e86af3f7f..7e0614e57714d 100644 --- a/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts +++ b/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts @@ -19,6 +19,11 @@ export const useGetDataViewIdsWithTitle = () => { return useQuery( [TRANSFORM_REACT_QUERY_KEYS.GET_DATA_VIEW_IDS_WITH_TITLE], - async () => await data.dataViews.getIdsWithTitle() + async () => { + // Since we let useQuery take care of caching, + // clear the cache to ensure we get the latest data view list. + await data.dataViews.clearCache(); + return await data.dataViews.getIdsWithTitle(); + } ); }; From ae18fdf82b27846326ecc21ef73eaa9da89da430 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 May 2024 13:25:46 +0200 Subject: [PATCH 3/6] fix edit button --- .../action_clone/use_clone_action.tsx | 1 - .../action_edit/use_edit_action.tsx | 23 ++++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx index 7e02a14da33d8..5e9c59ed18255 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx @@ -22,7 +22,6 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) => const toastNotifications = useToastNotifications(); const { data: dataViewListItems } = useGetDataViewIdsWithTitle(); - const { canCreateTransform } = useTransformCapabilities(); const clickHandler = useCallback( diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_edit/use_edit_action.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_edit/use_edit_action.tsx index d54abfa498a9a..ce9f7766616aa 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_edit/use_edit_action.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_edit/use_edit_action.tsx @@ -10,15 +10,15 @@ import React, { useCallback, useMemo, useState } from 'react'; import { i18n } from '@kbn/i18n'; import type { TransformListAction, TransformListRow } from '../../../../common'; -import { useTransformCapabilities } from '../../../../hooks'; +import { useGetDataViewIdsWithTitle, useTransformCapabilities } from '../../../../hooks'; import { editActionNameText, EditActionName } from './edit_action_name'; -import { useSearchItems } from '../../../../hooks/use_search_items'; -import { useAppDependencies, useToastNotifications } from '../../../../app_dependencies'; +import { useToastNotifications } from '../../../../app_dependencies'; import type { TransformConfigUnion } from '../../../../../../common/types/transform'; export type EditAction = ReturnType; export const useEditAction = (forceDisable: boolean, transformNodes: number) => { + const { data: dataViewListItems } = useGetDataViewIdsWithTitle(); const { canCreateTransform } = useTransformCapabilities(); const [config, setConfig] = useState(); @@ -27,20 +27,21 @@ export const useEditAction = (forceDisable: boolean, transformNodes: number) => const closeFlyout = () => setIsFlyoutVisible(false); - const { getDataViewIdByTitle } = useSearchItems(undefined); const toastNotifications = useToastNotifications(); - const appDeps = useAppDependencies(); - const dataViews = appDeps.data.dataViews; const clickHandler = useCallback( async (item: TransformListRow) => { try { + if (!dataViewListItems) { + return; + } + const dataViewTitle = Array.isArray(item.config.source.index) ? item.config.source.index.join(',') : item.config.source.index; - const currentDataViewId = getDataViewIdByTitle(dataViewTitle); + const dataViewListItem = dataViewListItems.find((d) => d.title === dataViewTitle); - if (currentDataViewId === undefined) { + if (dataViewListItem === undefined) { toastNotifications.addWarning( i18n.translate('xpack.transform.edit.noDataViewErrorPromptText', { defaultMessage: @@ -48,8 +49,9 @@ export const useEditAction = (forceDisable: boolean, transformNodes: number) => values: { dataViewTitle, transformId: item.id }, }) ); + } else { + setDataViewId(dataViewListItem.id); } - setDataViewId(currentDataViewId); setConfig(item.config); setIsFlyoutVisible(true); } catch (e) { @@ -60,8 +62,7 @@ export const useEditAction = (forceDisable: boolean, transformNodes: number) => }); } }, - // eslint-disable-next-line react-hooks/exhaustive-deps - [dataViews, toastNotifications, getDataViewIdByTitle] + [dataViewListItems, toastNotifications] ); const action: TransformListAction = useMemo( From 9f1b5bf4066af24462b8ef81bcab3a6adc98426a Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Thu, 2 May 2024 16:42:42 +0200 Subject: [PATCH 4/6] fix jest test --- .../components/transform_list/transform_list.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/transform_list.test.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/transform_list.test.tsx index e3f98beea9e7e..c47c0f0add99f 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/transform_list.test.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/transform_list/transform_list.test.tsx @@ -46,7 +46,7 @@ describe('Transform: Transform List ', () => { ); await waitFor(() => { - expect(useQueryMock).toHaveBeenCalledTimes(4); + expect(useQueryMock).toHaveBeenCalledTimes(5); expect(container.textContent).toContain('Create your first transform'); }); }); From 9f23cb895147f7cac6d1ab06df8f7a0e39cb1f78 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Fri, 3 May 2024 17:39:07 +0200 Subject: [PATCH 5/6] tweak hook --- .../transform/public/app/hooks/index.ts | 2 +- ....ts => use_get_data_views_title_id_map.ts} | 18 ++++++++++++----- .../action_clone/use_clone_action.tsx | 20 +++++++++---------- .../action_discover/use_action_discover.tsx | 20 +++++++++---------- .../action_edit/use_edit_action.tsx | 14 ++++++------- 5 files changed, 40 insertions(+), 34 deletions(-) rename x-pack/plugins/transform/public/app/hooks/{use_get_data_view_ids_with_title.ts => use_get_data_views_title_id_map.ts} (65%) diff --git a/x-pack/plugins/transform/public/app/hooks/index.ts b/x-pack/plugins/transform/public/app/hooks/index.ts index aa900cc961d1e..6af5f2201b027 100644 --- a/x-pack/plugins/transform/public/app/hooks/index.ts +++ b/x-pack/plugins/transform/public/app/hooks/index.ts @@ -7,7 +7,7 @@ export { useCreateTransform } from './use_create_transform'; export { useDocumentationLinks } from './use_documentation_links'; -export { useGetDataViewIdsWithTitle } from './use_get_data_view_ids_with_title'; +export { useGetDataViewsTitleIdMap } from './use_get_data_views_title_id_map'; export { useGetDataViewTitles } from './use_get_data_view_titles'; export { useGetEsIndices } from './use_get_es_indices'; export { useGetEsIngestPipelines } from './use_get_es_ingest_pipelines'; diff --git a/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts b/x-pack/plugins/transform/public/app/hooks/use_get_data_views_title_id_map.ts similarity index 65% rename from x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts rename to x-pack/plugins/transform/public/app/hooks/use_get_data_views_title_id_map.ts index 7e0614e57714d..5a19aaf13e35c 100644 --- a/x-pack/plugins/transform/public/app/hooks/use_get_data_view_ids_with_title.ts +++ b/x-pack/plugins/transform/public/app/hooks/use_get_data_views_title_id_map.ts @@ -8,22 +8,30 @@ import { useQuery } from '@tanstack/react-query'; import type { IHttpFetchError } from '@kbn/core-http-browser'; -import type { DataViewListItem } from '@kbn/data-views-plugin/public'; import { TRANSFORM_REACT_QUERY_KEYS } from '../../../common/constants'; import { useAppDependencies } from '../app_dependencies'; -export const useGetDataViewIdsWithTitle = () => { +type DataViewListTitleIdMap = Record; + +export const useGetDataViewsTitleIdMap = () => { const { data } = useAppDependencies(); - return useQuery( + return useQuery( [TRANSFORM_REACT_QUERY_KEYS.GET_DATA_VIEW_IDS_WITH_TITLE], async () => { // Since we let useQuery take care of caching, // clear the cache to ensure we get the latest data view list. - await data.dataViews.clearCache(); - return await data.dataViews.getIdsWithTitle(); + data.dataViews.clearCache(); + + return (await data.dataViews.getIdsWithTitle()).reduce>( + (acc, { id, title }) => { + acc[title] = id; + return acc; + }, + {} + ); } ); }; diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx index 5e9c59ed18255..6c1451da3dec9 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_clone/use_clone_action.tsx @@ -11,7 +11,7 @@ import { i18n } from '@kbn/i18n'; import type { TransformListAction, TransformListRow } from '../../../../common'; import { SECTION_SLUG } from '../../../../common/constants'; -import { useGetDataViewIdsWithTitle, useTransformCapabilities } from '../../../../hooks'; +import { useGetDataViewsTitleIdMap, useTransformCapabilities } from '../../../../hooks'; import { useToastNotifications } from '../../../../app_dependencies'; import { cloneActionNameText, CloneActionName } from './clone_action_name'; @@ -21,22 +21,22 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) => const history = useHistory(); const toastNotifications = useToastNotifications(); - const { data: dataViewListItems } = useGetDataViewIdsWithTitle(); + const { data: dataViewsTitleIdMap } = useGetDataViewsTitleIdMap(); const { canCreateTransform } = useTransformCapabilities(); const clickHandler = useCallback( async (item: TransformListRow) => { try { - if (!dataViewListItems) { + if (!dataViewsTitleIdMap) { return; } const dataViewTitle = Array.isArray(item.config.source.index) ? item.config.source.index.join(',') : item.config.source.index; - const dataViewListItem = dataViewListItems.find((d) => d.title === dataViewTitle); + const dataViewId = dataViewsTitleIdMap[dataViewTitle]; - if (dataViewListItem === undefined) { + if (dataViewId === undefined) { toastNotifications.addDanger( i18n.translate('xpack.transform.clone.noDataViewErrorPromptText', { defaultMessage: @@ -45,9 +45,7 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) => }) ); } else { - history.push( - `/${SECTION_SLUG.CLONE_TRANSFORM}/${item.id}?dataViewId=${dataViewListItem.id}` - ); + history.push(`/${SECTION_SLUG.CLONE_TRANSFORM}/${item.id}?dataViewId=${dataViewId}`); } } catch (e) { toastNotifications.addError(e, { @@ -57,14 +55,14 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) => }); } }, - [dataViewListItems, history, toastNotifications] + [dataViewsTitleIdMap, history, toastNotifications] ); const action: TransformListAction = useMemo( () => ({ name: (item: TransformListRow) => , enabled: () => - dataViewListItems !== undefined && + dataViewsTitleIdMap !== undefined && canCreateTransform && !forceDisable && transformNodes > 0, @@ -74,7 +72,7 @@ export const useCloneAction = (forceDisable: boolean, transformNodes: number) => onClick: clickHandler, 'data-test-subj': 'transformActionClone', }), - [canCreateTransform, dataViewListItems, forceDisable, clickHandler, transformNodes] + [canCreateTransform, dataViewsTitleIdMap, forceDisable, clickHandler, transformNodes] ); return { action }; diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_discover/use_action_discover.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_discover/use_action_discover.tsx index 66394ba3412da..ca78630a6730e 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_discover/use_action_discover.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_discover/use_action_discover.tsx @@ -11,7 +11,7 @@ import { DISCOVER_APP_LOCATOR } from '@kbn/discover-plugin/common'; import type { TransformListAction, TransformListRow } from '../../../../common'; import { useAppDependencies } from '../../../../app_dependencies'; -import { useGetDataViewIdsWithTitle } from '../../../../hooks'; +import { useGetDataViewsTitleIdMap } from '../../../../hooks'; import { isDiscoverActionDisabled, @@ -27,32 +27,32 @@ export const useDiscoverAction = (forceDisable: boolean) => { } = useAppDependencies(); const isDiscoverAvailable = !!capabilities.discover?.show; - const { data: dataViewListItems } = useGetDataViewIdsWithTitle(); + const { data: dataViewsTitleIdMap } = useGetDataViewsTitleIdMap(); const clickHandler = useCallback( (item: TransformListRow) => { const locator = share.url.locators.get(DISCOVER_APP_LOCATOR); - if (!locator || !dataViewListItems) return; - const dataView = dataViewListItems.find((d) => d.title === item.config.dest.index); - if (dataView) { + if (!locator || !dataViewsTitleIdMap) return; + const dataViewId = dataViewsTitleIdMap[item.config.dest.index]; + if (dataViewId) { locator.navigateSync({ - indexPatternId: dataView.id, + indexPatternId: dataViewId, }); } }, - [dataViewListItems, share] + [dataViewsTitleIdMap, share] ); const dataViewExists = useCallback( (item: TransformListRow): boolean => { - if (!dataViewListItems) { + if (!dataViewsTitleIdMap) { return false; } - const dataViewId = dataViewListItems.find((d) => d.title === item.config.dest.index); + const dataViewId = dataViewsTitleIdMap[item.config.dest.index]; return dataViewId !== undefined; }, - [dataViewListItems] + [dataViewsTitleIdMap] ); const action: TransformListAction = useMemo( diff --git a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_edit/use_edit_action.tsx b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_edit/use_edit_action.tsx index ce9f7766616aa..2b15d83065197 100644 --- a/x-pack/plugins/transform/public/app/sections/transform_management/components/action_edit/use_edit_action.tsx +++ b/x-pack/plugins/transform/public/app/sections/transform_management/components/action_edit/use_edit_action.tsx @@ -10,7 +10,7 @@ import React, { useCallback, useMemo, useState } from 'react'; import { i18n } from '@kbn/i18n'; import type { TransformListAction, TransformListRow } from '../../../../common'; -import { useGetDataViewIdsWithTitle, useTransformCapabilities } from '../../../../hooks'; +import { useGetDataViewsTitleIdMap, useTransformCapabilities } from '../../../../hooks'; import { editActionNameText, EditActionName } from './edit_action_name'; import { useToastNotifications } from '../../../../app_dependencies'; @@ -18,7 +18,7 @@ import type { TransformConfigUnion } from '../../../../../../common/types/transf export type EditAction = ReturnType; export const useEditAction = (forceDisable: boolean, transformNodes: number) => { - const { data: dataViewListItems } = useGetDataViewIdsWithTitle(); + const { data: dataViewsTitleIdMap } = useGetDataViewsTitleIdMap(); const { canCreateTransform } = useTransformCapabilities(); const [config, setConfig] = useState(); @@ -32,16 +32,16 @@ export const useEditAction = (forceDisable: boolean, transformNodes: number) => const clickHandler = useCallback( async (item: TransformListRow) => { try { - if (!dataViewListItems) { + if (!dataViewsTitleIdMap) { return; } const dataViewTitle = Array.isArray(item.config.source.index) ? item.config.source.index.join(',') : item.config.source.index; - const dataViewListItem = dataViewListItems.find((d) => d.title === dataViewTitle); + const newDataViewId = dataViewsTitleIdMap[dataViewTitle]; - if (dataViewListItem === undefined) { + if (newDataViewId === undefined) { toastNotifications.addWarning( i18n.translate('xpack.transform.edit.noDataViewErrorPromptText', { defaultMessage: @@ -50,7 +50,7 @@ export const useEditAction = (forceDisable: boolean, transformNodes: number) => }) ); } else { - setDataViewId(dataViewListItem.id); + setDataViewId(newDataViewId); } setConfig(item.config); setIsFlyoutVisible(true); @@ -62,7 +62,7 @@ export const useEditAction = (forceDisable: boolean, transformNodes: number) => }); } }, - [dataViewListItems, toastNotifications] + [dataViewsTitleIdMap, toastNotifications] ); const action: TransformListAction = useMemo( From 928e66c384978bba53b25a8c4a8f9c9a6a3476d3 Mon Sep 17 00:00:00 2001 From: Walter Rafelsberger Date: Tue, 7 May 2024 15:38:20 +0200 Subject: [PATCH 6/6] tweak caching --- .../public/app/hooks/use_get_data_views_title_id_map.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/x-pack/plugins/transform/public/app/hooks/use_get_data_views_title_id_map.ts b/x-pack/plugins/transform/public/app/hooks/use_get_data_views_title_id_map.ts index 5a19aaf13e35c..d7454ffc63a15 100644 --- a/x-pack/plugins/transform/public/app/hooks/use_get_data_views_title_id_map.ts +++ b/x-pack/plugins/transform/public/app/hooks/use_get_data_views_title_id_map.ts @@ -21,11 +21,7 @@ export const useGetDataViewsTitleIdMap = () => { return useQuery( [TRANSFORM_REACT_QUERY_KEYS.GET_DATA_VIEW_IDS_WITH_TITLE], async () => { - // Since we let useQuery take care of caching, - // clear the cache to ensure we get the latest data view list. - data.dataViews.clearCache(); - - return (await data.dataViews.getIdsWithTitle()).reduce>( + return (await data.dataViews.getIdsWithTitle(true)).reduce>( (acc, { id, title }) => { acc[title] = id; return acc;