forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryResultDataTable.js
More file actions
281 lines (246 loc) · 7.87 KB
/
Copy pathQueryResultDataTable.js
File metadata and controls
281 lines (246 loc) · 7.87 KB
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
import React from 'react';
import PropTypes from 'prop-types';
import { VariableSizeGrid } from 'react-window';
import throttle from 'lodash/throttle';
import Draggable from 'react-draggable';
import Measure from 'react-measure';
import SpinKitCube from './SpinKitCube.js';
import moment from 'moment';
const renderValue = (input, fieldMeta) => {
if (input === null || input === undefined) {
return <em>null</em>;
} else if (input === true || input === false) {
return input.toString();
} else if (fieldMeta.datatype === 'date') {
return moment.utc(input).format('MM/DD/YYYY HH:mm:ss');
} else if (typeof input === 'object') {
return JSON.stringify(input, null, 2);
} else {
return input;
}
};
// Hide the overflow so the scroll bar never shows in the header grid
const headerStyle = {
overflowX: 'hidden',
overflowY: 'hidden'
};
const headerCellStyle = {
lineHeight: '30px',
backgroundColor: '#f4f4f4',
justifyContent: 'space-between',
borderBottom: '1px solid #CCC',
display: 'flex',
paddingLeft: '.5rem',
paddingRight: '.5rem'
};
const cellStyle = {
lineHeight: '30px',
paddingLeft: '.5rem',
paddingRight: '.5rem',
borderBottom: '1px solid #CCC',
display: 'relative'
};
// NOTE: PureComponent's shallow compare works for this component
// because the isRunning prop will toggle with each query execution
// It would otherwise not rerender on change of prop.queryResult alone
class QueryResultDataTable extends React.PureComponent {
state = {
dimensions: {
width: -1,
height: -1
},
columnWidths: {}
};
static getDerivedStateFromProps(nextProps, prevState) {
const { queryResult } = nextProps;
const { columnWidths } = prevState;
if (queryResult && queryResult.fields) {
queryResult.fields.forEach(field => {
if (!columnWidths[field]) {
const fieldMeta = queryResult.meta[field];
let valueLength = fieldMeta.maxValueLength;
if (field.length > valueLength) {
valueLength = field.length;
}
let columnWidthGuess = valueLength * 20;
if (columnWidthGuess < 100) {
columnWidthGuess = 100;
} else if (columnWidthGuess > 350) {
columnWidthGuess = 350;
}
columnWidths[field] = columnWidthGuess;
}
});
}
return { columnWidths };
}
// NOTE
// An empty dummy column is added to the grid for visual purposes
// If dataKey was found this is a real column of data from the query result
// If not, it's the dummy column at the end, and it should fill the rest of the grid width
getColumnWidth = index => {
const { columnWidths } = this.state;
const { queryResult } = this.props;
const dataKey = queryResult.fields[index];
const { width } = this.state.dimensions;
if (dataKey) {
return columnWidths[dataKey];
}
const totalWidthFilled = queryResult.fields
.map(key => columnWidths[key])
.reduce((prev, curr) => prev + curr, 0);
const fakeColumnWidth = width - totalWidthFilled;
return fakeColumnWidth < 10 ? 10 : fakeColumnWidth;
};
headerGrid = React.createRef();
bodyGrid = React.createRef();
resizeColumn = ({ dataKey, deltaX, columnIndex }) => {
this.setState(
prevState => {
const prevWidths = prevState.columnWidths;
const newWidth = prevWidths[dataKey] + deltaX;
return {
columnWidths: {
...prevWidths,
[dataKey]: newWidth > 100 ? newWidth : 100
}
};
},
() => this.recalc(columnIndex)
);
};
recalc = throttle(columnIndex => {
if (this.headerGrid.current.resetAfterColumnIndex) {
this.headerGrid.current.resetAfterColumnIndex(columnIndex);
this.bodyGrid.current.resetAfterColumnIndex(columnIndex);
}
}, 100);
HeaderCell = ({ columnIndex, rowIndex, style }) => {
const { queryResult } = this.props;
const dataKey = queryResult.fields[columnIndex];
// If dataKey is present this is an actual header to render
if (dataKey) {
return (
<div style={Object.assign({}, style, headerCellStyle)}>
<div>{dataKey}</div>
<Draggable
axis="x"
defaultClassName="DragHandle"
defaultClassNameDragging="DragHandleActive"
onDrag={(event, { deltaX }) => {
this.resizeColumn({ dataKey, deltaX, columnIndex });
}}
position={{ x: 0 }}
zIndex={999}
>
<span className="DragHandleIcon">⋮</span>
</Draggable>
</div>
);
}
// If this is a dummy header cell render an empty header cell
return <div style={Object.assign({}, style, headerCellStyle)} />;
};
Cell = ({ columnIndex, rowIndex, style }) => {
const { queryResult } = this.props;
const dataKey = queryResult.fields[columnIndex];
const finalStyle = Object.assign({}, style, cellStyle);
if (rowIndex % 2 === 0) {
finalStyle.backgroundColor = '#fafafa';
}
// If dataKey is present this is a real data cell to render
if (dataKey) {
const fieldMeta = queryResult.meta[dataKey];
// Account for extra row that was used for header row
const value = queryResult.rows[rowIndex][dataKey];
return (
<div style={finalStyle}>
<div className="truncate">{renderValue(value, fieldMeta)}</div>
</div>
);
}
// If no dataKey this is a dummy cell.
// It should render nothing, but match the row's style
return (
<div style={finalStyle}>
<div className="truncate" />
</div>
);
};
getRowHeight() {
return 30;
}
// When a scroll occurs in the body grid,
// synchronize the scroll position of the header grid
handleGridScroll = ({ scrollLeft }) => {
this.headerGrid.current.scrollTo({ scrollLeft });
};
handleContainerResize = contentRect => {
this.setState({ dimensions: contentRect.bounds });
};
render() {
const { isRunning, queryError, queryResult } = this.props;
const { height, width } = this.state.dimensions;
if (isRunning) {
return (
<div className="h-100 flex-center">
<SpinKitCube />
</div>
);
}
if (queryError) {
return (
<div
style={{ fontSize: '2rem', padding: 24, textAlign: 'center' }}
className={`h-100 bg-error flex-center`}
>
{queryError}
</div>
);
}
if (queryResult && queryResult.rows) {
const rowCount = queryResult.rows.length;
// Add extra column to fill remaining grid width if necessary
const columnCount = queryResult.fields.length + 1;
return (
<Measure bounds onResize={this.handleContainerResize}>
{({ measureRef }) => (
<div ref={measureRef} className="h-100 w-100">
<VariableSizeGrid
columnCount={columnCount}
rowCount={1}
columnWidth={this.getColumnWidth}
rowHeight={this.getRowHeight}
height={30}
width={width}
ref={this.headerGrid}
style={headerStyle}
>
{this.HeaderCell}
</VariableSizeGrid>
<VariableSizeGrid
columnCount={columnCount}
rowCount={rowCount}
columnWidth={this.getColumnWidth}
rowHeight={this.getRowHeight}
width={width}
height={height - 30}
ref={this.bodyGrid}
onScroll={this.handleGridScroll}
>
{this.Cell}
</VariableSizeGrid>
</div>
)}
</Measure>
);
}
return null;
}
}
QueryResultDataTable.propTypes = {
isRunning: PropTypes.bool,
queryError: PropTypes.string,
queryResult: PropTypes.object
};
export default QueryResultDataTable;