forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
correlations_table.tsx
118 lines (104 loc) · 3.51 KB
/
correlations_table.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
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { useCallback, useMemo, useState } from 'react';
import { debounce } from 'lodash';
import { EuiBasicTable, EuiBasicTableColumn } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FETCH_STATUS } from '../../../hooks/use_fetcher';
import { useUiTracker } from '../../../../../observability/public';
import { useTheme } from '../../../hooks/use_theme';
import type { SelectedSignificantTerm } from '../../../../common/search_strategies/correlations/types';
export type { SelectedSignificantTerm };
const PAGINATION_SIZE_OPTIONS = [5, 10, 20, 50];
export type SelectedSignificantCorrelationTerm<
T extends SelectedSignificantTerm
> = Pick<T, 'fieldName' | 'fieldValue'>;
interface Props<T> {
significantTerms?: T[];
status: FETCH_STATUS;
percentageColumnName?: string;
setSelectedSignificantTerm: (term: T | null) => void;
selectedTerm?: { fieldName: string; fieldValue: string };
onFilter?: () => void;
columns: Array<EuiBasicTableColumn<T>>;
}
export function CorrelationsTable<T extends SelectedSignificantTerm>({
significantTerms,
status,
setSelectedSignificantTerm,
columns,
selectedTerm,
}: Props<T>) {
const euiTheme = useTheme();
const trackApmEvent = useUiTracker({ app: 'apm' });
const trackSelectSignificantCorrelationTerm = useCallback(
() =>
debounce(
() => trackApmEvent({ metric: 'select_significant_term' }),
1000
),
[trackApmEvent]
);
const [pageIndex, setPageIndex] = useState(0);
const [pageSize, setPageSize] = useState(10);
const { pagination, pageOfItems } = useMemo(() => {
const pageStart = pageIndex * pageSize;
const itemCount = significantTerms?.length ?? 0;
return {
pageOfItems: significantTerms?.slice(pageStart, pageStart + pageSize),
pagination: {
pageIndex,
pageSize,
totalItemCount: itemCount,
pageSizeOptions: PAGINATION_SIZE_OPTIONS,
},
};
}, [pageIndex, pageSize, significantTerms]);
const onTableChange = useCallback(({ page }) => {
const { index, size } = page;
setPageIndex(index);
setPageSize(size);
}, []);
const tableColumns: Array<EuiBasicTableColumn<T>> = columns ?? [];
return (
<EuiBasicTable
items={pageOfItems ?? []}
noItemsMessage={
status === FETCH_STATUS.LOADING ? loadingText : noDataText
}
loading={status === FETCH_STATUS.LOADING}
columns={tableColumns}
rowProps={(term) => {
return {
onMouseEnter: () => {
setSelectedSignificantTerm(term);
trackSelectSignificantCorrelationTerm();
},
onMouseLeave: () => setSelectedSignificantTerm(null),
style:
selectedTerm &&
selectedTerm.fieldValue === term.fieldValue &&
selectedTerm.fieldName === term.fieldName
? {
backgroundColor: euiTheme.eui.euiColorLightestShade,
}
: null,
};
}}
pagination={pagination}
onChange={onTableChange}
/>
);
}
const loadingText = i18n.translate(
'xpack.apm.correlations.correlationsTable.loadingText',
{ defaultMessage: 'Loading' }
);
const noDataText = i18n.translate(
'xpack.apm.correlations.correlationsTable.noDataText',
{ defaultMessage: 'No data' }
);