-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
index_table.tsx
185 lines (160 loc) · 5.32 KB
/
index_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
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { sortBy } from 'lodash';
import React from 'react';
import { EuiBasicTable } from '@elastic/eui';
import { injectI18n } from '@kbn/i18n/react';
import { FixDefaultFieldsButton } from './default_fields/button';
import { ReindexButton } from './reindex';
import { AppContext } from '../../../../app_context';
import { EnrichedDeprecationInfo } from '../../../../../../common/types';
const PAGE_SIZES = [10, 25, 50, 100, 250, 500, 1000];
export interface IndexDeprecationDetails {
index: string;
reindex: boolean;
needsDefaultFields: boolean;
blockerForReindexing?: EnrichedDeprecationInfo['blockerForReindexing'];
details?: string;
}
export interface IndexDeprecationTableProps extends ReactIntl.InjectedIntlProps {
indices: IndexDeprecationDetails[];
}
interface IndexDeprecationTableState {
sortField: string;
sortDirection: 'asc' | 'desc';
pageIndex: number;
pageSize: number;
}
export class IndexDeprecationTableUI extends React.Component<
IndexDeprecationTableProps,
IndexDeprecationTableState
> {
constructor(props: IndexDeprecationTableProps) {
super(props);
this.state = {
sortField: 'index',
sortDirection: 'asc',
pageIndex: 0,
pageSize: 10,
};
}
public render() {
const { intl } = this.props;
const { pageIndex, pageSize, sortField, sortDirection } = this.state;
const columns = [
{
field: 'index',
name: intl.formatMessage({
id: 'xpack.upgradeAssistant.checkupTab.deprecations.indexTable.indexColumnLabel',
defaultMessage: 'Index',
}),
sortable: true,
},
{
field: 'details',
name: intl.formatMessage({
id: 'xpack.upgradeAssistant.checkupTab.deprecations.indexTable.detailsColumnLabel',
defaultMessage: 'Details',
}),
},
];
const actionsColumn = this.generateActionsColumn();
if (actionsColumn) {
columns.push(actionsColumn as any);
}
const sorting = {
sort: { field: sortField as keyof IndexDeprecationDetails, direction: sortDirection },
};
const pagination = {
pageIndex,
pageSize,
...this.pageSizeOptions(),
};
return (
<EuiBasicTable
items={this.getRows()}
columns={columns}
sorting={sorting}
pagination={pagination}
onChange={this.onTableChange}
hasActions={false}
/>
);
}
private getRows() {
const { sortField, sortDirection, pageIndex, pageSize } = this.state;
const { indices } = this.props;
let sorted = sortBy(indices, sortField);
if (sortDirection === 'desc') {
sorted = sorted.reverse();
}
const start = pageIndex * pageSize;
return sorted.slice(start, start + pageSize);
}
private onTableChange = (tableProps: any) => {
this.setState({
sortField: tableProps.sort.field,
sortDirection: tableProps.sort.direction,
pageIndex: tableProps.page.index,
pageSize: tableProps.page.size,
});
};
private pageSizeOptions() {
const { indices } = this.props;
const totalItemCount = indices.length;
// If we only have that smallest page size, don't show any page size options.
if (totalItemCount <= PAGE_SIZES[0]) {
return { totalItemCount, pageSizeOptions: [], hidePerPageOptions: true };
}
// Keep a size option if the # of items is larger than the previous option.
// This avoids having a long list of useless page sizes.
const pageSizeOptions = PAGE_SIZES.filter((perPage, idx) => {
return idx === 0 || totalItemCount > PAGE_SIZES[idx - 1];
});
return { totalItemCount, pageSizeOptions, hidePerPageOptions: false };
}
private generateActionsColumn() {
// NOTE: this naive implementation assumes all indices in the table are
// should show the reindex button. This should work for known usecases.
const { indices } = this.props;
const showReindexButton = indices.find((i) => i.reindex === true);
const showNeedsDefaultFieldsButton = indices.find((i) => i.needsDefaultFields === true);
if (!showReindexButton && !showNeedsDefaultFieldsButton) {
return null;
}
return {
actions: [
{
render(indexDep: IndexDeprecationDetails) {
if (showReindexButton) {
return (
<AppContext.Consumer>
{({ http, docLinks }) => {
return (
<ReindexButton
docLinks={docLinks}
reindexBlocker={indexDep.blockerForReindexing}
indexName={indexDep.index!}
http={http}
/>
);
}}
</AppContext.Consumer>
);
} else {
return (
<AppContext.Consumer>
{({ http }) => <FixDefaultFieldsButton indexName={indexDep.index!} http={http} />}
</AppContext.Consumer>
);
}
},
},
],
};
}
}
export const IndexDeprecationTable = injectI18n(IndexDeprecationTableUI);