-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.tsx
315 lines (287 loc) · 8.68 KB
/
index.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
// InvocationTable Component
/**
* Component that renders invocations data and information in a table
* @packageDocumentation
*/
import React, { useRef, useState, useEffect } from 'react';
import clsx from 'clsx';
import {
ColumnProps,
TableCellRenderer,
RowMouseEventHandlerParams,
defaultTableRowRenderer,
TableRowProps,
} from 'react-virtualized';
import styled from 'styled-components';
import { makeStyles, Theme, createStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import { InvocationTableProps, ModalState, HoverState } from './types';
import { parseInvocationTableData } from './utils';
import FileModal from './FileModal';
import InfiniteTable from '../InfiniteTable';
import LoadingRow from '../InfiniteTable/BaseTable/LoadingRow';
import FileButton from './FileButton';
import StatusIcon from '../StatusIcon';
const HeaderHeight = 60;
const RowHeight = 48;
const Container = styled(Paper)`
width: 91.5%;
height: calc(98vh - 120px);
margin-left: auto;
margin-right: auto;
margin-top: 20px;
outline: none;
`;
const FileCell = styled.div`
height: ${RowHeight - 1}px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
border-bottom: 1px solid rgba(224, 224, 224, 1);
`;
const StatusCell = styled.div`
height: ${RowHeight - 1}px;
width: 100%;
border-bottom: 1px solid rgba(224, 224, 224, 1);
margin-left: 5px;
`;
const StatusContainer = styled.div`
height: 100%;
width: 100%;
display: flex;
align-items: center;
`;
const IconStyle: React.CSSProperties = { marginRight: '3px' };
/**
*
* @param fileRenderer function to open file modal button
* @param statusCellRenderer function to render status cell button
*/
export const createColumns = (
fileRenderer: TableCellRenderer,
statusCellRenderer: TableCellRenderer
) => {
const columns: ColumnProps[] = [
{
dataKey: 'status',
label: 'Status',
width: 150,
cellRenderer: statusCellRenderer,
},
{ dataKey: 'name', label: 'Name', width: 400 },
{ dataKey: 'labels', label: 'Labels', width: 120 },
{ dataKey: 'date', label: 'Run Date', width: 200 },
{ dataKey: 'duration', label: 'Duration', width: 120 },
{ dataKey: 'user', label: 'User', width: 400 },
{
dataKey: 'files',
label: '',
width: 100,
cellRenderer: fileRenderer,
flexGrow: 0,
},
];
return columns;
};
const useStyles = makeStyles((theme: Theme) =>
createStyles({
flexContainer: {
display: 'flex',
alignItems: 'center',
boxSizing: 'border-box',
},
tableRow: {
cursor: 'pointer',
outline: 'none',
},
tableRowHover: {
'&:hover': {
backgroundColor: theme.palette.grey[200],
},
},
tableCell: {
flex: 1,
},
tableGrid: {
outline: 'none',
},
tableHeader: {
fontWeight: 550,
fontSize: 16,
},
})
);
/** InvocationTable Component */
export const InvocationTable: React.FC<InvocationTableProps> = ({
invocations,
pageToken,
next,
isNextPageLoading,
}) => {
const classes = useStyles();
const [hoverState, setHoverState] = useState<HoverState>({
isHovered: false,
rowIndex: -1,
});
const containerRef = useRef<HTMLDivElement>(null);
const [modalState, setModalState] = useState<ModalState>({
isOpen: false,
index: 0,
});
const closeModal = () => {
setModalState({
isOpen: false,
index: 0,
});
};
const [width, setWidth] = useState(1920);
const [height, setHeight] = useState(1080);
const rows = invocations.map((invocation) =>
parseInvocationTableData(invocation)
);
const hasNextPage = pageToken !== '';
const isRowLoaded = ({ index }) => !hasNextPage || index < rows.length;
const rowCount = hasNextPage ? rows.length + 1 : rows.length;
const loadMoreRows = isNextPageLoading
? () => {}
: () => next(false, pageToken);
const setDimensions = () => {
setWidth(
containerRef.current ? containerRef.current.offsetWidth : 1920
);
setHeight(
containerRef.current ? containerRef.current.offsetHeight : 1080
);
};
useEffect(() => {
setDimensions();
}, [containerRef]);
useEffect(() => {
window.addEventListener('resize', setDimensions);
return () => window.removeEventListener('resize', setDimensions);
}, []);
const getRowClassName = ({ index }) => {
return clsx(classes.tableRow, classes.flexContainer, {
[classes.tableRowHover]: index !== -1,
});
};
const cellClass = clsx(classes.tableCell, classes.flexContainer);
const headerClass = clsx(
classes.tableCell,
classes.flexContainer,
classes.tableHeader
);
const rowGetter = ({ index }) => {
if (!isRowLoaded({ index })) {
return {};
}
return rows[index];
};
const onRowClick = ({ index }: RowMouseEventHandlerParams) => {
window.open(
`https://source.cloud.google.com/results/${invocations[
index
].getName()}`
);
return false;
};
const getFiles = () => {
if (invocations.length > 0) {
return invocations[modalState.index].getFilesList();
}
return [];
};
const getParent = () => {
if (invocations.length > 0) {
return invocations[modalState.index].getName();
}
return '';
};
const fileCellRenderer: TableCellRenderer = ({ rowIndex }) => {
const openFileModal = (index: number, event: React.MouseEvent) => {
event.stopPropagation();
setModalState({
isOpen: true,
index: index,
});
};
const showFileButton = () => {
return hoverState.isHovered && rowIndex === hoverState.rowIndex;
};
return (
<FileCell>
<FileButton
isVisible={showFileButton()}
id={`FileButton-${rowIndex}`}
onClick={(e) => openFileModal(rowIndex, e)}
/>
</FileCell>
);
};
const statusCellRenderer: TableCellRenderer = ({ rowData }) => {
return (
<StatusCell>
<StatusContainer>
<StatusIcon name={rowData.status} style={IconStyle} />
{rowData.status}
</StatusContainer>
</StatusCell>
);
};
const onRowMouseOver = ({ index }: RowMouseEventHandlerParams) => {
setHoverState({
isHovered: true,
rowIndex: index,
});
};
const onRowMouseOut = ({ index }: RowMouseEventHandlerParams) => {
setHoverState({
isHovered: false,
rowIndex: index,
});
};
const columns = createColumns(fileCellRenderer, statusCellRenderer);
const rowRenderer = (props: TableRowProps) => {
const { index, key, style } = props;
if (!isRowLoaded({ index })) {
return (
<div key={key} style={style}>
<LoadingRow width={width} size={20} />
</div>
);
}
return defaultTableRowRenderer(props);
};
return (
<Container ref={containerRef} elevation={3} id={'InvocationTable'}>
<InfiniteTable
columns={columns}
width={width}
height={height}
rowHeight={RowHeight}
headerHeight={HeaderHeight}
rowCount={rowCount}
rowGetter={rowGetter}
rowClassName={getRowClassName}
onRowClick={onRowClick}
isRowLoaded={isRowLoaded}
loadMoreRows={loadMoreRows}
cellClass={cellClass}
headerClass={headerClass}
onRowMouseOver={onRowMouseOver}
onRowMouseOut={onRowMouseOut}
gridClass={classes.tableGrid}
isNextPageLoading={isNextPageLoading}
rowRenderer={rowRenderer}
/>
<FileModal
isOpen={modalState.isOpen}
close={closeModal}
files={getFiles()}
parent={getParent()}
/>
</Container>
);
};
export default InvocationTable;