From 06d6517c6ec01ff5d5a0d2427a6646567a205458 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 1 Apr 2022 12:44:34 +0200 Subject: [PATCH 1/2] Catch potental missing feta of filter --- .../application/main/components/layout/discover_layout.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/discover/public/application/main/components/layout/discover_layout.tsx b/src/plugins/discover/public/application/main/components/layout/discover_layout.tsx index 8be5e2b7ce4be..386aca84c9afc 100644 --- a/src/plugins/discover/public/application/main/components/layout/discover_layout.tsx +++ b/src/plugins/discover/public/application/main/components/layout/discover_layout.tsx @@ -294,7 +294,7 @@ export function DiscoverLayout({ error={dataState.error} hasQuery={!!state.query?.query} hasFilters={ - state.filters && state.filters.filter((f) => !f.meta.disabled).length > 0 + state.filters && state.filters.filter((f) => !f.meta?.disabled).length > 0 } onDisableFilters={onDisableFilters} /> From f48d81d61e67360432eccb775818ddec26ba9c58 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 11 Apr 2022 09:16:13 +0200 Subject: [PATCH 2/2] Extract hasActiveFilter Function --- .../components/layout/discover_layout.tsx | 8 ++- .../main/components/layout/utils.test.ts | 51 +++++++++++++++++++ .../main/components/layout/utils.ts | 15 ++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/plugins/discover/public/application/main/components/layout/utils.test.ts create mode 100644 src/plugins/discover/public/application/main/components/layout/utils.ts diff --git a/src/plugins/discover/public/application/main/components/layout/discover_layout.tsx b/src/plugins/discover/public/application/main/components/layout/discover_layout.tsx index 386aca84c9afc..f8ea65a58b3a9 100644 --- a/src/plugins/discover/public/application/main/components/layout/discover_layout.tsx +++ b/src/plugins/discover/public/application/main/components/layout/discover_layout.tsx @@ -25,7 +25,7 @@ import { useDiscoverServices } from '../../../../utils/use_discover_services'; import { DiscoverNoResults } from '../no_results'; import { LoadingSpinner } from '../loading_spinner/loading_spinner'; import { generateFilters } from '../../../../../../data/public'; -import { DataViewField } from '../../../../../../data_views/public'; +import { DataView, DataViewField, DataViewType } from '../../../../../../data_views/public'; import { DiscoverSidebarResponsive } from '../sidebar'; import { DiscoverLayoutProps } from './types'; import { SEARCH_FIELDS_FROM_SOURCE, SHOW_FIELD_STATISTICS } from '../../../../../common'; @@ -48,7 +48,7 @@ import { import { FieldStatisticsTable } from '../field_stats_table'; import { VIEW_MODE } from '../../../../components/view_mode_toggle'; import { DOCUMENTS_VIEW_CLICK, FIELD_STATISTICS_VIEW_CLICK } from '../field_stats_table/constants'; -import { DataViewType, DataView } from '../../../../../../data_views/public'; +import { hasActiveFilter } from './utils'; /** * Local storage key for sidebar persistence state @@ -293,9 +293,7 @@ export function DiscoverLayout({ data={data} error={dataState.error} hasQuery={!!state.query?.query} - hasFilters={ - state.filters && state.filters.filter((f) => !f.meta?.disabled).length > 0 - } + hasFilters={hasActiveFilter(state.filters)} onDisableFilters={onDisableFilters} /> )} diff --git a/src/plugins/discover/public/application/main/components/layout/utils.test.ts b/src/plugins/discover/public/application/main/components/layout/utils.test.ts new file mode 100644 index 0000000000000..2201ff3dfe476 --- /dev/null +++ b/src/plugins/discover/public/application/main/components/layout/utils.test.ts @@ -0,0 +1,51 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ +import { Filter } from '@kbn/es-query'; +import { hasActiveFilter } from './utils'; + +const testFilter: Filter = { + meta: { + alias: null, + disabled: false, + negate: false, + }, + query: { query: 'hi' }, +}; +const testFilterDisabled: Filter = { + meta: { + alias: null, + disabled: true, + negate: false, + }, + query: { query: 'hi' }, +}; + +const testFilterBroken = {} as Filter; + +describe('hasActiveFilter', () => { + test('only active filters', () => { + const filters = [testFilter]; + const result = hasActiveFilter(filters); + expect(result).toBe(true); + }); + test('only disabled filters', () => { + const filters = [testFilterDisabled]; + const result = hasActiveFilter(filters); + expect(result).toBe(false); + }); + test('disabled and active filters', () => { + const filters = [testFilter, testFilterDisabled]; + const result = hasActiveFilter(filters); + expect(result).toBe(true); + }); + test('broken filter - edge case', () => { + const filters = [testFilterBroken]; + const result = hasActiveFilter(filters); + expect(result).toBe(true); + }); +}); diff --git a/src/plugins/discover/public/application/main/components/layout/utils.ts b/src/plugins/discover/public/application/main/components/layout/utils.ts new file mode 100644 index 0000000000000..bcf9dbb6178ec --- /dev/null +++ b/src/plugins/discover/public/application/main/components/layout/utils.ts @@ -0,0 +1,15 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ +import { Filter } from '@kbn/es-query'; + +/** + * Returns if true there's at least 1 active filter + */ +export function hasActiveFilter(filters: Filter[] | undefined) { + return filters && filters.filter((f) => !f.meta?.disabled).length > 0; +}