Skip to content

Commit

Permalink
Fireproof onChange (#82252)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet authored Nov 2, 2020
1 parent 02a238a commit fb629bd
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions x-pack/plugins/global_search_bar/public/components/search_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -218,6 +231,7 @@ export function SearchBar({
onChange={onChange}
options={options}
popoverButtonBreakpoints={['xs', 's']}
singleSelection={true}
popoverButton={
<EuiHeaderSectionItemButton
aria-label={i18n.translate(
Expand Down

0 comments on commit fb629bd

Please sign in to comment.