From 32f34ddcd734c76a61a55e2aa440a054eaac9701 Mon Sep 17 00:00:00 2001 From: Constance Chen Date: Mon, 4 Jan 2021 15:02:41 -0800 Subject: [PATCH 1/2] Var rename - so it doesn't sound negative or like a bug, but is instead an anticipated load/use case - remove unncessary function - move declaration down to right before it's used --- .../app_search/components/engine/engine_router.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.tsx index d3501a5ee7af3..1b54549de4f89 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.tsx @@ -75,8 +75,6 @@ export const EngineRouter: React.FC = () => { const { engineName: engineNameParam } = useParams() as { engineName: string }; const engineBreadcrumb = [ENGINES_TITLE, engineNameParam]; - const isEngineInStateStale = () => engineName !== engineNameParam; - useEffect(() => { setEngineName(engineNameParam); initializeEngine(); @@ -93,7 +91,8 @@ export const EngineRouter: React.FC = () => { return ; } - if (isEngineInStateStale() || dataLoading) return ; + const isLoadingNewEngine = engineName !== engineNameParam; + if (isLoadingNewEngine || dataLoading) return ; return ( From 382a378cf748a7073d0721b39c096a83673bdf49 Mon Sep 17 00:00:00 2001 From: Constance Chen Date: Mon, 4 Jan 2021 15:12:53 -0800 Subject: [PATCH 2/2] [Proposal] Other misc cleanup - Rename engineNameParam to engineNameFromUrl to make reading flow a little bit more nicely + hopefully make the var source a bit clearer - Change other references back to engineName for simplicity (they should really only be running after engineName has already been set, so there shouldn't be any race conditions there - moving engineBreadcrumb to after Loading should also solve that) --- .../components/engine/engine_router.test.tsx | 3 +-- .../components/engine/engine_router.tsx | 16 ++++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.test.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.test.tsx index 74976c1c61f0c..cbaa347d65732 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.test.tsx @@ -57,8 +57,7 @@ describe('EngineRouter', () => { }); it('redirects to engines list and flashes an error if the engine param was not found', () => { - (useParams as jest.Mock).mockReturnValue({ engineName: '404-engine' }); - setMockValues({ ...values, engineNotFound: true }); + setMockValues({ ...values, engineNotFound: true, engineName: '404-engine' }); const wrapper = shallow(); expect(wrapper.find(Redirect).prop('to')).toEqual('/engines'); diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.tsx b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.tsx index 1b54549de4f89..ce7675cef9bb1 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/engine/engine_router.tsx @@ -69,31 +69,31 @@ export const EngineRouter: React.FC = () => { }, } = useValues(AppLogic); + const { engineName: engineNameFromUrl } = useParams() as { engineName: string }; const { engineName, dataLoading, engineNotFound } = useValues(EngineLogic); const { setEngineName, initializeEngine, clearEngine } = useActions(EngineLogic); - const { engineName: engineNameParam } = useParams() as { engineName: string }; - const engineBreadcrumb = [ENGINES_TITLE, engineNameParam]; - useEffect(() => { - setEngineName(engineNameParam); + setEngineName(engineNameFromUrl); initializeEngine(); return clearEngine; - }, [engineNameParam]); + }, [engineNameFromUrl]); if (engineNotFound) { setQueuedErrorMessage( i18n.translate('xpack.enterpriseSearch.appSearch.engine.notFound', { - defaultMessage: "No engine with name '{engineNameParam}' could be found.", - values: { engineNameParam }, + defaultMessage: "No engine with name '{engineName}' could be found.", + values: { engineName }, }) ); return ; } - const isLoadingNewEngine = engineName !== engineNameParam; + const isLoadingNewEngine = engineName !== engineNameFromUrl; if (isLoadingNewEngine || dataLoading) return ; + const engineBreadcrumb = [ENGINES_TITLE, engineName]; + return ( {canViewEngineAnalytics && (