-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[UnifiedFieldList] Add new fields ingested in background with valid mappings #172329
Changes from 11 commits
26d3d6d
7b999db
3a6c239
307a16d
54daa2f
ca50f4d
999a6a9
1a30c70
fa237a4
15cc582
3cea884
065a50c
e9fd00e
63907e4
454592c
75cc235
12616b4
3a74600
15e5168
139b651
ab514e4
5cd087f
b424159
9b70dec
30dfec6
f747c2e
9ed0c49
5420c77
f340fae
26c4d72
ab2c743
921a598
ec55f89
c76c87e
165c3cd
cbee199
9170300
d62dba7
02a4537
621a1df
8617b3d
223f546
8549c3f
f751a4e
d024027
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,7 +7,7 @@ | |||
*/ | ||||
|
||||
import { groupBy } from 'lodash'; | ||||
import { useEffect, useMemo, useState } from 'react'; | ||||
import { useEffect, useMemo, useState, useRef } from 'react'; | ||||
import { i18n } from '@kbn/i18n'; | ||||
import { type CoreStart } from '@kbn/core-lifecycle-browser'; | ||||
import { type DataView, type DataViewField } from '@kbn/data-views-plugin/common'; | ||||
|
@@ -41,6 +41,7 @@ export interface GroupedFieldsParams<T extends FieldListItem> { | |||
onOverrideFieldGroupDetails?: OverrideFieldGroupDetails; | ||||
onSupportedFieldFilter?: (field: T) => boolean; | ||||
onSelectedFieldFilter?: (field: T) => boolean; | ||||
isCompatibleField?: (fields: DataViewField) => boolean; | ||||
kertal marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
} | ||||
|
||||
export interface GroupedFieldsResult<T extends FieldListItem> { | ||||
|
@@ -52,6 +53,8 @@ export interface GroupedFieldsResult<T extends FieldListItem> { | |||
fieldsExistInIndex: boolean; | ||||
screenReaderDescriptionId?: string; | ||||
}; | ||||
allFields: T[] | null; // `null` is for loading indicator | ||||
kertal marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
hasNewFields: boolean; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it being used somewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it's here
|
||||
} | ||||
|
||||
export function useGroupedFields<T extends FieldListItem = DataViewField>({ | ||||
|
@@ -65,6 +68,7 @@ export function useGroupedFields<T extends FieldListItem = DataViewField>({ | |||
onOverrideFieldGroupDetails, | ||||
onSupportedFieldFilter, | ||||
onSelectedFieldFilter, | ||||
isCompatibleField, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will check, it definitely looks similar, so not having to introduce another prop is a good option 👍 |
||||
}: GroupedFieldsParams<T>): GroupedFieldsResult<T> { | ||||
const fieldsExistenceReader = useExistingFieldsReader(); | ||||
const fieldListFilters = useFieldFilters<T>({ | ||||
|
@@ -73,6 +77,8 @@ export function useGroupedFields<T extends FieldListItem = DataViewField>({ | |||
getCustomFieldType, | ||||
onSupportedFieldFilter, | ||||
}); | ||||
const allFieldsInclNew = useRef(allFields); | ||||
const hasNewFields = useRef(false); | ||||
const onFilterFieldList = fieldListFilters.onFilterField; | ||||
const [dataView, setDataView] = useState<DataView | null>(null); | ||||
const isAffectedByTimeFilter = Boolean(dataView?.timeFieldName); | ||||
|
@@ -120,13 +126,30 @@ export function useGroupedFields<T extends FieldListItem = DataViewField>({ | |||
}; | ||||
|
||||
const selectedFields = sortedSelectedFields || []; | ||||
const sortedFields = [...(allFields || [])].sort(sortFields); | ||||
const newFields = dataViewId | ||||
? fieldsExistenceReader | ||||
.getNewFields(dataViewId) | ||||
.filter((field) => (isCompatibleField ? isCompatibleField(field) : true)) | ||||
: []; | ||||
// remove fields from allFields that are available in newFields, because they can be provided in unmapped state | ||||
const allFieldsWithoutNewFields = !newFields.length | ||||
? allFields | ||||
: allFields?.filter((field) => !newFields.find((newField) => newField.name === field.name)); | ||||
// append new fields to the end of the list allFieldsWithoutNewFields | ||||
const allFieldsWithNewFields = allFieldsWithoutNewFields | ||||
? [...allFieldsWithoutNewFields, ...newFields] | ||||
: newFields; | ||||
|
||||
const sortedFields = [...((allFieldsWithNewFields as unknown as T[]) || [])].sort(sortFields); | ||||
allFieldsInclNew.current = sortedFields; | ||||
hasNewFields.current = Boolean(newFields.length); | ||||
const groupedFields = { | ||||
...getDefaultFieldGroups(), | ||||
...groupBy(sortedFields, (field) => { | ||||
if (!sortedSelectedFields && onSelectedFieldFilter && onSelectedFieldFilter(field)) { | ||||
selectedFields.push(field); | ||||
} | ||||
|
||||
if (onSupportedFieldFilter && !onSupportedFieldFilter(field)) { | ||||
return 'skippedFields'; | ||||
} | ||||
|
@@ -311,18 +334,20 @@ export function useGroupedFields<T extends FieldListItem = DataViewField>({ | |||
|
||||
return fieldGroupDefinitions; | ||||
}, [ | ||||
sortedSelectedFields, | ||||
allFields, | ||||
onSupportedFieldFilter, | ||||
onSelectedFieldFilter, | ||||
onOverrideFieldGroupDetails, | ||||
dataView, | ||||
dataViewId, | ||||
hasFieldDataHandler, | ||||
fieldsExistenceInfoUnavailable, | ||||
popularFieldsLimit, | ||||
isAffectedByGlobalFilter, | ||||
isAffectedByTimeFilter, | ||||
popularFieldsLimit, | ||||
sortedSelectedFields, | ||||
dataViewId, | ||||
fieldsExistenceInfoUnavailable, | ||||
onOverrideFieldGroupDetails, | ||||
hasFieldDataHandler, | ||||
fieldsExistenceReader, | ||||
onSelectedFieldFilter, | ||||
onSupportedFieldFilter, | ||||
dataView, | ||||
isCompatibleField, | ||||
]); | ||||
|
||||
const fieldGroups: FieldListGroups<T> = useMemo(() => { | ||||
|
@@ -381,6 +406,8 @@ export function useGroupedFields<T extends FieldListItem = DataViewField>({ | |||
return { | ||||
fieldListGroupedProps, | ||||
fieldListFiltersProps: fieldListFilters.fieldListFiltersProps, | ||||
allFields: allFieldsInclNew.current, | ||||
hasNewFields: hasNewFields.current, | ||||
}; | ||||
} | ||||
|
||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note, this code was just moved to have access to the new modified list of allFields