/
table-wz-api.tsx
323 lines (297 loc) · 9.27 KB
/
table-wz-api.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
/*
* Wazuh app - Table with search bar
* Copyright (C) 2015-2022 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import React, { ReactNode, useCallback, useEffect, useState } from 'react';
import {
EuiTitle,
EuiLoadingSpinner,
EuiFlexGroup,
EuiFlexItem,
EuiText,
EuiButtonEmpty,
EuiToolTip,
EuiIcon,
EuiCheckboxGroup,
} from '@elastic/eui';
import { TableWithSearchBar } from './table-with-search-bar';
import { TableDefault } from './table-default';
import { WzRequest } from '../../../react-services/wz-request';
import { ExportTableCsv } from './components/export-table-csv';
import { useStateStorage } from '../hooks';
/**
* Search input custom filter button
*/
interface CustomFilterButton {
label: string;
field: string;
value: string;
}
const getFilters = filters => {
const { default: defaultFilters, ...restFilters } = filters;
return Object.keys(restFilters).length ? restFilters : defaultFilters;
};
export function TableWzAPI({
actionButtons,
postActionButtons,
addOnTitle,
extra,
setReload,
...rest
}: {
actionButtons?:
| ReactNode
| ReactNode[]
| (({ filters }: { filters }) => ReactNode);
postActionButtons?:
| ReactNode
| ReactNode[]
| (({ filters }: { filters }) => ReactNode);
title?: string;
addOnTitle?: ReactNode;
description?: string;
extra?: ReactNode;
downloadCsv?: boolean | string;
searchTable?: boolean;
endpoint: string;
buttonOptions?: CustomFilterButton[];
onFiltersChange?: Function;
showReload?: boolean;
searchBarProps?: any;
reload?: boolean;
onDataChange?: Function;
setReload?: (newValue: number) => void;
}) {
const [totalItems, setTotalItems] = useState(0);
const [filters, setFilters] = useState({});
const [isLoading, setIsLoading] = useState(false);
const onFiltersChange = filters =>
typeof rest.onFiltersChange === 'function'
? rest.onFiltersChange(filters)
: null;
const onDataChange = data =>
typeof rest.onDataChange === 'function' ? rest.onDataChange(data) : null;
/**
* Changing the reloadFootprint timestamp will trigger reloading the table
*/
const [reloadFootprint, setReloadFootprint] = useState(rest.reload || 0);
const [selectedFields, setSelectedFields] = useStateStorage(
rest.tableColumns.some(({ show }) => show)
? rest.tableColumns.filter(({ show }) => show).map(({ field }) => field)
: rest.tableColumns.map(({ field }) => field),
rest?.saveStateStorage?.system,
rest?.saveStateStorage?.key
? `${rest?.saveStateStorage?.key}-visible-fields`
: undefined,
);
const [isOpenFieldSelector, setIsOpenFieldSelector] = useState(false);
const onSearch = useCallback(async function (
endpoint,
filters,
pagination,
sorting,
) {
try {
const { pageIndex, pageSize } = pagination;
const { field, direction } = sorting.sort;
setIsLoading(true);
setFilters(filters);
onFiltersChange(filters);
const params = {
...getFilters(filters),
offset: pageIndex * pageSize,
limit: pageSize,
sort: `${direction === 'asc' ? '+' : '-'}${field}`,
};
const response = await WzRequest.apiReq('GET', endpoint, { params });
const { affected_items: items, total_affected_items: totalItems } = (
(response || {}).data || {}
).data;
setIsLoading(false);
setTotalItems(totalItems);
const result = {
items: rest.mapResponseItem ? items.map(rest.mapResponseItem) : items,
totalItems,
};
onDataChange(result);
return result;
} catch (error) {
setIsLoading(false);
setTotalItems(0);
if (error?.name) {
/* This replaces the error name. The intention is that an AxiosError
doesn't appear in the toast message.
TODO: This should be managed by the service that does the request instead of only changing
the name in this case.
*/
error.name = 'RequestError';
}
throw error;
}
},
[]);
const renderActionButtons = (actionButtons, filters) => {
if (Array.isArray(actionButtons)) {
return actionButtons.map((button, key) => (
<EuiFlexItem key={key} grow={false}>
{button}
</EuiFlexItem>
));
}
if (typeof actionButtons === 'object') {
return <EuiFlexItem grow={false}>{actionButtons}</EuiFlexItem>;
}
if (typeof actionButtons === 'function') {
return actionButtons({ filters: getFilters(filters) });
}
};
/**
* Generate a new reload footprint and set reload to propagate refresh
*/
const triggerReload = () => {
setReloadFootprint(Date.now());
if (setReload) {
setReload(Date.now());
}
};
useEffect(() => {
if (rest.reload) triggerReload();
}, [rest.reload]);
const ReloadButton = (
<EuiFlexItem grow={false}>
<EuiButtonEmpty iconType='refresh' onClick={() => triggerReload()}>
Refresh
</EuiButtonEmpty>
</EuiFlexItem>
);
const header = (
<>
<EuiFlexGroup wrap alignItems='center' responsive={false}>
<EuiFlexItem>
<EuiFlexGroup wrap alignItems='center' responsive={false}>
<EuiFlexItem className='wz-flex-basis-auto' grow={false}>
{rest.title && (
<EuiTitle size='s'>
<h1>
{rest.title}{' '}
{isLoading ? (
<EuiLoadingSpinner size='s' />
) : (
<span>({totalItems})</span>
)}
</h1>
</EuiTitle>
)}
</EuiFlexItem>
{addOnTitle ? (
<EuiFlexItem className='wz-flex-basis-auto' grow={false}>
{addOnTitle}
</EuiFlexItem>
) : null}
</EuiFlexGroup>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiFlexGroup wrap alignItems={'center'} responsive={false}>
{/* Render optional custom action button */}
{renderActionButtons(actionButtons, filters)}
{/* Render optional reload button */}
{rest.showReload && ReloadButton}
{/* Render optional export to CSV button */}
{rest.downloadCsv && (
<ExportTableCsv
endpoint={rest.endpoint}
totalItems={totalItems}
filters={getFilters(filters)}
title={
typeof rest.downloadCsv === 'string'
? rest.downloadCsv
: rest.title
}
/>
)}
{/* Render optional post custom action button */}
{renderActionButtons(postActionButtons, filters)}
{rest.showFieldSelector && (
<EuiFlexItem grow={false}>
<EuiToolTip content='Select visible fields' position='left'>
<EuiButtonEmpty
onClick={() => setIsOpenFieldSelector(state => !state)}
>
<EuiIcon type='managementApp' color='primary' />
</EuiButtonEmpty>
</EuiToolTip>
</EuiFlexItem>
)}
</EuiFlexGroup>
</EuiFlexItem>
</EuiFlexGroup>
{isOpenFieldSelector && (
<EuiFlexGroup>
<EuiFlexItem>
<EuiCheckboxGroup
options={rest.tableColumns.map(item => ({
id: item.field,
label: item.name,
checked: selectedFields.includes(item.field),
}))}
onChange={optionID => {
setSelectedFields(state => {
if (state.includes(optionID)) {
if (state.length > 1) {
return state.filter(field => field !== optionID);
}
return state;
}
return [...state, optionID];
});
}}
className='columnsSelectedCheckboxs'
idToSelectedMap={{}}
/>
</EuiFlexItem>
</EuiFlexGroup>
)}
</>
);
const tableColumns = rest.tableColumns.filter(({ field }) =>
selectedFields.includes(field),
);
const table = rest.searchTable ? (
<TableWithSearchBar
onSearch={onSearch}
{...{ ...rest, reload: reloadFootprint }}
tableColumns={tableColumns}
selectedFields={selectedFields}
/>
) : (
<TableDefault
onSearch={onSearch}
{...{ ...rest, reload: reloadFootprint }}
/>
);
return (
<EuiFlexGroup direction='column' gutterSize='s' responsive={false}>
<EuiFlexItem>{header}</EuiFlexItem>
{rest.description && (
<EuiFlexItem>
<EuiText color='subdued'>{rest.description}</EuiText>
</EuiFlexItem>
)}
{extra ? <EuiFlexItem>{extra}</EuiFlexItem> : null}
<EuiFlexItem>{table}</EuiFlexItem>
</EuiFlexGroup>
);
}
// Set default props
TableWzAPI.defaultProps = {
title: null,
downloadCsv: false,
searchBar: false,
};