From 9cd113578a2d4abdd11519700f960f12614ab231 Mon Sep 17 00:00:00 2001 From: Pierre Gayvallet Date: Mon, 2 Nov 2020 17:11:19 +0100 Subject: [PATCH] Fireproof onChange (#82252) (#82289) --- .../public/components/search_bar.tsx | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/global_search_bar/public/components/search_bar.tsx b/x-pack/plugins/global_search_bar/public/components/search_bar.tsx index e73f9d954d5ad..adc55329962e9 100644 --- a/x-pack/plugins/global_search_bar/public/components/search_bar.tsx +++ b/x-pack/plugins/global_search_bar/public/components/search_bar.tsx @@ -159,23 +159,36 @@ export function SearchBar({ } }; - const onChange = (selected: EuiSelectableTemplateSitewideOption[]) => { + const onChange = (selection: EuiSelectableTemplateSitewideOption[]) => { + const selected = selection.find(({ checked }) => checked === 'on'); + if (!selected) { + return; + } + // @ts-ignore - ts error is "union type is too complex to express" - const { url, type, key } = selected.find(({ checked }) => checked === 'on'); + const { url, type } = selected; - if (type === 'application') { - trackUiMetric(METRIC_TYPE.CLICK, [ - 'user_navigated_to_application', - `user_navigated_to_application_${key.toLowerCase().replaceAll(' ', '_')}`, // which application - ]); - } else { - trackUiMetric(METRIC_TYPE.CLICK, [ - 'user_navigated_to_saved_object', - `user_navigated_to_saved_object_${type}`, // which type of saved object - ]); + // errors in tracking should not prevent selection behavior + try { + if (type === 'application') { + const key = selected.keys ?? 'unknown'; + trackUiMetric(METRIC_TYPE.CLICK, [ + 'user_navigated_to_application', + `user_navigated_to_application_${key.toLowerCase().replaceAll(' ', '_')}`, // which application + ]); + } else { + trackUiMetric(METRIC_TYPE.CLICK, [ + 'user_navigated_to_saved_object', + `user_navigated_to_saved_object_${type}`, // which type of saved object + ]); + } + } catch (e) { + // eslint-disable-next-line no-console + console.log('Error trying to track searchbar metrics', e); } navigateToUrl(url); + (document.activeElement as HTMLElement).blur(); if (searchRef) { clearField(searchRef); @@ -218,6 +231,7 @@ export function SearchBar({ onChange={onChange} options={options} popoverButtonBreakpoints={['xs', 's']} + singleSelection={true} popoverButton={