-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathdiscover_sidebar.tsx
533 lines (501 loc) · 19.1 KB
/
discover_sidebar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* 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 './discover_sidebar.scss';
import { throttle } from 'lodash';
import React, { useCallback, useEffect, useState, useMemo, useRef, memo } from 'react';
import { i18n } from '@kbn/i18n';
import {
EuiAccordion,
EuiFlexItem,
EuiFlexGroup,
EuiText,
EuiTitle,
EuiSpacer,
EuiNotificationBadge,
EuiPageSideBar,
useResizeObserver,
EuiButton,
} from '@elastic/eui';
import useShallowCompareEffect from 'react-use/lib/useShallowCompareEffect';
import { isEqual } from 'lodash';
import { FormattedMessage } from '@kbn/i18n-react';
import { DataViewPicker } from '@kbn/unified-search-plugin/public';
import { DataViewField, getFieldSubtypeMulti } from '@kbn/data-views-plugin/public';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import { DiscoverField } from './discover_field';
import { DiscoverFieldSearch } from './discover_field_search';
import { FIELDS_LIMIT_SETTING } from '../../../../../common';
import { groupFields } from './lib/group_fields';
import { getDetails } from './lib/get_details';
import { FieldFilterState, getDefaultFieldFilter, setFieldFilterProp } from './lib/field_filter';
import { getDataViewFieldList } from './lib/get_data_view_field_list';
import { DiscoverSidebarResponsiveProps } from './discover_sidebar_responsive';
import { VIEW_MODE } from '../../../../components/view_mode_toggle';
import { DISCOVER_TOUR_STEP_ANCHOR_IDS } from '../../../../components/discover_tour';
import type { DataTableRecord } from '../../../../types';
/**
* Default number of available fields displayed and added on scroll
*/
const FIELDS_PER_PAGE = 50;
export interface DiscoverSidebarProps extends Omit<DiscoverSidebarResponsiveProps, 'documents$'> {
/**
* Current state of the field filter, filtering fields by name, type, ...
*/
fieldFilter: FieldFilterState;
/**
* Change current state of fieldFilter
*/
setFieldFilter: (next: FieldFilterState) => void;
/**
* Callback to close the flyout if sidebar is rendered in a flyout
*/
closeFlyout?: () => void;
/**
* Pass the reference to field editor component to the parent, so it can be properly unmounted
* @param ref reference to the field editor component
*/
setFieldEditorRef?: (ref: () => void | undefined) => void;
/**
* Handles "Edit field" action
* Buttons will be hidden if not provided
* @param fieldName
*/
editField?: (fieldName?: string) => void;
/**
* Handles "Create a data view action" action
* Buttons will be hidden if not provided
*/
createNewDataView?: () => void;
/**
* a statistics of the distribution of fields in the given hits
*/
fieldCounts?: Record<string, number>;
/**
* hits fetched from ES, displayed in the doc table
*/
documents?: DataTableRecord[];
/**
* Discover view mode
*/
viewMode: VIEW_MODE;
showDataViewPicker?: boolean;
}
export function DiscoverSidebarComponent({
alwaysShowActionButtons = false,
columns,
fieldCounts,
fieldFilter,
documents,
onAddField,
onAddFilter,
onRemoveField,
selectedDataView,
setFieldFilter,
trackUiMetric,
useNewFieldsApi = false,
onFieldEdited,
onChangeDataView,
setFieldEditorRef,
closeFlyout,
editField,
viewMode,
createNewDataView,
showDataViewPicker,
}: DiscoverSidebarProps) {
const { uiSettings, dataViewFieldEditor } = useDiscoverServices();
const [fields, setFields] = useState<DataViewField[] | null>(null);
const [scrollContainer, setScrollContainer] = useState<Element | null>(null);
const [fieldsToRender, setFieldsToRender] = useState(FIELDS_PER_PAGE);
const [fieldsPerPage, setFieldsPerPage] = useState(FIELDS_PER_PAGE);
const availableFieldsContainer = useRef<HTMLUListElement | null>(null);
const isPlainRecord = !onAddFilter;
useEffect(() => {
if (documents) {
const newFields = getDataViewFieldList(selectedDataView, fieldCounts);
setFields(newFields);
}
}, [selectedDataView, fieldCounts, documents]);
const scrollDimensions = useResizeObserver(scrollContainer);
const onChangeFieldSearch = useCallback(
(field: string, value: string | boolean | undefined) => {
const newState = setFieldFilterProp(fieldFilter, field, value);
setFieldFilter(newState);
setFieldsToRender(fieldsPerPage);
},
[fieldFilter, setFieldFilter, setFieldsToRender, fieldsPerPage]
);
const getDetailsByField = useCallback(
(ipField: DataViewField) => getDetails(ipField, documents, columns, selectedDataView),
[documents, columns, selectedDataView]
);
const popularLimit = useMemo(() => uiSettings.get(FIELDS_LIMIT_SETTING), [uiSettings]);
const {
selected: selectedFields,
popular: popularFields,
unpopular: unpopularFields,
} = useMemo(
() => groupFields(fields, columns, popularLimit, fieldCounts, fieldFilter, useNewFieldsApi),
[fields, columns, popularLimit, fieldCounts, fieldFilter, useNewFieldsApi]
);
/**
* Popular fields are not displayed in text based lang mode
*/
const restFields = useMemo(
() => (isPlainRecord ? [...popularFields, ...unpopularFields] : unpopularFields),
[isPlainRecord, popularFields, unpopularFields]
);
const paginate = useCallback(() => {
const newFieldsToRender = fieldsToRender + Math.round(fieldsPerPage * 0.5);
setFieldsToRender(Math.max(fieldsPerPage, Math.min(newFieldsToRender, restFields.length)));
}, [setFieldsToRender, fieldsToRender, restFields, fieldsPerPage]);
useEffect(() => {
if (scrollContainer && restFields.length && availableFieldsContainer.current) {
const { clientHeight, scrollHeight } = scrollContainer;
const isScrollable = scrollHeight > clientHeight; // there is no scrolling currently
const allFieldsRendered = fieldsToRender >= restFields.length;
if (!isScrollable && !allFieldsRendered) {
// Not all available fields were rendered with the given fieldsPerPage number
// and no scrolling is available due to the a high zoom out factor of the browser
// In this case the fieldsPerPage needs to be adapted
const fieldsRenderedHeight = availableFieldsContainer.current.clientHeight;
const avgHeightPerItem = Math.round(fieldsRenderedHeight / fieldsToRender);
const newFieldsPerPage = Math.round(clientHeight / avgHeightPerItem) + 10;
if (newFieldsPerPage >= FIELDS_PER_PAGE && newFieldsPerPage !== fieldsPerPage) {
setFieldsPerPage(newFieldsPerPage);
setFieldsToRender(newFieldsPerPage);
}
}
}
}, [
fieldsPerPage,
scrollContainer,
restFields,
fieldsToRender,
setFieldsPerPage,
setFieldsToRender,
scrollDimensions,
]);
const lazyScroll = useCallback(() => {
if (scrollContainer) {
const { scrollTop, clientHeight, scrollHeight } = scrollContainer;
const nearBottom = scrollTop + clientHeight > scrollHeight * 0.9;
if (nearBottom && restFields) {
paginate();
}
}
}, [paginate, scrollContainer, restFields]);
const { fieldTypes, presentFieldTypes } = useMemo(() => {
const result = ['any'];
const dataViewFieldTypes = new Set<string>();
if (Array.isArray(fields)) {
for (const field of fields) {
if (field.type !== '_source') {
// If it's a string type, we want to distinguish between keyword and text
// For this purpose we need the ES type
const type =
field.type === 'string' &&
field.esTypes &&
['keyword', 'text'].includes(field.esTypes[0])
? field.esTypes?.[0]
: field.type;
// _id and _index would map to string, that's why we don't add the string type here
if (type && type !== 'string') {
dataViewFieldTypes.add(type);
}
if (result.indexOf(field.type) === -1) {
result.push(field.type);
}
}
}
}
return { fieldTypes: result, presentFieldTypes: Array.from(dataViewFieldTypes) };
}, [fields]);
const showFieldStats = useMemo(() => viewMode === VIEW_MODE.DOCUMENT_LEVEL, [viewMode]);
const calculateMultiFields = () => {
if (!useNewFieldsApi || !fields) {
return undefined;
}
const map = new Map<string, Array<{ field: DataViewField; isSelected: boolean }>>();
fields.forEach((field) => {
const subTypeMulti = getFieldSubtypeMulti(field);
const parent = subTypeMulti?.multi.parent;
if (!parent) {
return;
}
const multiField = {
field,
isSelected: selectedFields.includes(field),
};
const value = map.get(parent) ?? [];
value.push(multiField);
map.set(parent, value);
});
return map;
};
const [multiFields, setMultiFields] = useState(() => calculateMultiFields());
useShallowCompareEffect(() => {
setMultiFields(calculateMultiFields());
}, [fields, selectedFields, useNewFieldsApi]);
const deleteField = useMemo(
() =>
editField && selectedDataView
? async (fieldName: string) => {
const ref = dataViewFieldEditor.openDeleteModal({
ctx: {
dataView: selectedDataView,
},
fieldName,
onDelete: async () => {
onFieldEdited();
},
});
if (setFieldEditorRef) {
setFieldEditorRef(ref);
}
if (closeFlyout) {
closeFlyout();
}
}
: undefined,
[
selectedDataView,
editField,
setFieldEditorRef,
closeFlyout,
onFieldEdited,
dataViewFieldEditor,
]
);
const getPaginated = useCallback(
(list) => {
return list.slice(0, fieldsToRender);
},
[fieldsToRender]
);
const filterChanged = useMemo(() => isEqual(fieldFilter, getDefaultFieldFilter()), [fieldFilter]);
if (!selectedDataView) {
return null;
}
return (
<EuiPageSideBar
className="dscSidebar"
aria-label={i18n.translate('discover.fieldChooser.filter.indexAndFieldsSectionAriaLabel', {
defaultMessage: 'Index and fields',
})}
id="discover-sidebar"
data-test-subj="discover-sidebar"
>
<EuiFlexGroup
className="dscSidebar__group"
direction="column"
alignItems="stretch"
gutterSize="s"
responsive={false}
>
{Boolean(showDataViewPicker) && (
<DataViewPicker
currentDataViewId={selectedDataView.id}
onChangeDataView={onChangeDataView}
onAddField={editField}
onDataViewCreated={createNewDataView}
trigger={{
label: selectedDataView?.getName() || '',
'data-test-subj': 'dataView-switch-link',
title: selectedDataView?.title || '',
fullWidth: true,
}}
/>
)}
<EuiFlexItem grow={false}>
<form>
<DiscoverFieldSearch
onChange={onChangeFieldSearch}
value={fieldFilter.name}
types={fieldTypes}
presentFieldTypes={presentFieldTypes}
isPlainRecord={isPlainRecord}
/>
</form>
</EuiFlexItem>
<EuiFlexItem className="eui-yScroll">
<div
ref={(el) => {
if (documents && el && !el.dataset.dynamicScroll) {
el.dataset.dynamicScroll = 'true';
setScrollContainer(el);
}
}}
onScroll={throttle(lazyScroll, 100)}
className="eui-yScroll"
>
{Array.isArray(fields) && fields.length > 0 && (
<div>
{selectedFields &&
selectedFields.length > 0 &&
selectedFields[0].displayName !== '_source' ? (
<>
<EuiAccordion
id="dscSelectedFields"
initialIsOpen={true}
buttonContent={
<EuiText size="xs" id="selected_fields">
<strong>
<FormattedMessage
id="discover.fieldChooser.filter.selectedFieldsTitle"
defaultMessage="Selected fields"
/>
</strong>
</EuiText>
}
extraAction={
<EuiNotificationBadge color={filterChanged ? 'subdued' : 'accent'} size="m">
{selectedFields.length}
</EuiNotificationBadge>
}
>
<EuiSpacer size="m" />
<ul
className="dscFieldList"
aria-labelledby="selected_fields"
data-test-subj={`fieldList-selected`}
>
{selectedFields.map((field: DataViewField) => {
return (
<li key={`field${field.name}`} data-attr-field={field.name}>
<DiscoverField
alwaysShowActionButton={alwaysShowActionButtons}
field={field}
dataView={selectedDataView}
onAddField={onAddField}
onRemoveField={onRemoveField}
onAddFilter={onAddFilter}
getDetails={getDetailsByField}
selected={true}
trackUiMetric={trackUiMetric}
multiFields={multiFields?.get(field.name)}
onEditField={editField}
onDeleteField={deleteField}
showFieldStats={showFieldStats}
/>
</li>
);
})}
</ul>
</EuiAccordion>
<EuiSpacer size="s" />{' '}
</>
) : null}
<EuiAccordion
id="dscAvailableFields"
initialIsOpen={true}
buttonContent={
<EuiText size="xs" id="available_fields">
<strong id={DISCOVER_TOUR_STEP_ANCHOR_IDS.addFields}>
<FormattedMessage
id="discover.fieldChooser.filter.availableFieldsTitle"
defaultMessage="Available fields"
/>
</strong>
</EuiText>
}
extraAction={
<EuiNotificationBadge size="m" color={filterChanged ? 'subdued' : 'accent'}>
{restFields.length}
</EuiNotificationBadge>
}
>
<EuiSpacer size="s" />
{!isPlainRecord && popularFields.length > 0 && (
<>
<EuiTitle size="xxxs" className="dscFieldListHeader">
<h4 id="available_fields_popular">
<FormattedMessage
id="discover.fieldChooser.filter.popularTitle"
defaultMessage="Popular"
/>
</h4>
</EuiTitle>
<ul
className="dscFieldList dscFieldList--popular"
aria-labelledby="available_fields available_fields_popular"
data-test-subj={`fieldList-popular`}
>
{popularFields.map((field: DataViewField) => {
return (
<li key={`field${field.name}`} data-attr-field={field.name}>
<DiscoverField
alwaysShowActionButton={alwaysShowActionButtons}
field={field}
dataView={selectedDataView}
onAddField={onAddField}
onRemoveField={onRemoveField}
onAddFilter={onAddFilter}
getDetails={getDetailsByField}
trackUiMetric={trackUiMetric}
multiFields={multiFields?.get(field.name)}
onEditField={editField}
onDeleteField={deleteField}
showFieldStats={showFieldStats}
/>
</li>
);
})}
</ul>
</>
)}
<ul
className="dscFieldList dscFieldList--unpopular"
aria-labelledby="available_fields"
data-test-subj={`fieldList-unpopular`}
ref={availableFieldsContainer}
>
{getPaginated(restFields).map((field: DataViewField) => {
return (
<li key={`field${field.name}`} data-attr-field={field.name}>
<DiscoverField
alwaysShowActionButton={alwaysShowActionButtons}
field={field}
dataView={selectedDataView}
onAddField={onAddField}
onRemoveField={onRemoveField}
onAddFilter={onAddFilter}
getDetails={getDetailsByField}
trackUiMetric={trackUiMetric}
multiFields={multiFields?.get(field.name)}
onEditField={editField}
onDeleteField={deleteField}
showFieldStats={showFieldStats}
/>
</li>
);
})}
</ul>
</EuiAccordion>
</div>
)}
</div>
</EuiFlexItem>
{!!editField && (
<EuiFlexItem grow={false}>
<EuiButton
iconType="indexOpen"
data-test-subj="dataView-add-field_btn"
onClick={() => editField()}
size="s"
>
{i18n.translate('discover.fieldChooser.addField.label', {
defaultMessage: 'Add a field',
})}
</EuiButton>
</EuiFlexItem>
)}
</EuiFlexGroup>
</EuiPageSideBar>
);
}
export const DiscoverSidebar = memo(DiscoverSidebarComponent);