Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GS] Fix onChange error when submitting with no selected item(s) #82252

Merged
merged 1 commit into from
Nov 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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