From 141044e51ec77ea7c5e68c9311a381aecb515497 Mon Sep 17 00:00:00 2001 From: Sander Philipse <94373878+sphilipse@users.noreply.github.com> Date: Tue, 18 Jun 2024 23:35:24 +0200 Subject: [PATCH] [Search] Fix search index rerendering unnecessarily (#186412) ## Summary This fixes the search index rerendering on every change, even if nothing actually changed. --- .../components/search_index/search_index.tsx | 81 ++++++++++++------- 1 file changed, 54 insertions(+), 27 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/search_index.tsx b/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/search_index.tsx index 09e6b494f94e4b..7c13a3f524ccf3 100644 --- a/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/search_index.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/enterprise_search_content/components/search_index/search_index.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import React, { useEffect } from 'react'; +import React, { useEffect, useMemo } from 'react'; import { useParams } from 'react-router-dom'; @@ -15,12 +15,15 @@ import { EuiTabbedContent, EuiTabbedContentTab } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; +import { ClientConfigType } from '../../../../../common/types'; + import { generateEncodedPath } from '../../../shared/encode_path_params'; import { ErrorStatePrompt } from '../../../shared/error_state'; import { HttpLogic } from '../../../shared/http'; import { KibanaLogic } from '../../../shared/kibana'; import { SEARCH_INDEX_PATH, SEARCH_INDEX_TAB_PATH } from '../../routes'; +import { ElasticsearchViewIndex } from '../../types'; import { isConnectorIndex, isCrawlerIndex } from '../../utils/indices'; import { ConnectorConfiguration } from '../connector_detail/connector_configuration'; import { EnterpriseSearchContentPageTemplate } from '../layout/page_template'; @@ -223,20 +226,6 @@ export const SearchIndex: React.FC = () => { ...(hasDefaultIngestPipeline ? [PIPELINES_TAB] : []), ]; - const selectedTab = tabs.find((tab) => tab.id === tabId); - - const onTabClick = (tab: EuiTabbedContentTab) => { - KibanaLogic.values.navigateToUrl( - generateEncodedPath( - tab.id === SearchIndexTabId.OVERVIEW ? SEARCH_INDEX_PATH : SEARCH_INDEX_TAB_PATH, - { - indexName, - tabId: tab.id, - } - ) - ); - }; - return ( { rightSideItems: getHeaderActions(index), }} > - {isCrawlerIndex(index) && !index.connector ? ( - - ) : isCrawlerIndex(index) && (Boolean(errorConnectingMessage) || !config.host) ? ( - - ) : ( - <> - {indexName === index?.name && ( - - )} - {isCrawlerIndex(index) && } - - )} + ); }; + +interface ContentProps { + config?: ClientConfigType; + errorConnectingMessage: string; + index?: ElasticsearchViewIndex; + tabId?: string; + tabs: EuiTabbedContentTab[]; +} + +const Content: React.FC = ({ + config, + errorConnectingMessage, + index, + tabs, + tabId, +}) => { + const selectedTab = useMemo(() => tabs.find((tab) => tab.id === tabId), [tabId]); + + const onTabClick = (tab: EuiTabbedContentTab) => { + KibanaLogic.values.navigateToUrl( + generateEncodedPath( + tab.id === SearchIndexTabId.OVERVIEW ? SEARCH_INDEX_PATH : SEARCH_INDEX_TAB_PATH, + { + indexName: index?.name || '', + tabId: tab.id, + } + ) + ); + }; + + if (isCrawlerIndex(index) && !index.connector) { + return ; + } + if (isCrawlerIndex(index) && (Boolean(errorConnectingMessage) || !config?.host)) { + return ; + } + return ( + <> + + {isCrawlerIndex(index) && } + + ); +};