Skip to content

Commit e3e2fcc

Browse files
committed
Refactor RawResultsTable component
The `RawResultsTable` was using inline styles, while we should prefer to use styled components. This refactors it to use styled components and also improves some other miscelleanous things (extracting the props to a separate type and moving the `Cell` above the `Row` since the latter uses the former).
1 parent 110d930 commit e3e2fcc

1 file changed

Lines changed: 74 additions & 55 deletions

File tree

extensions/ql-vscode/src/view/remote-queries/RawResultsTable.tsx

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
11
import * as React from 'react';
2+
import { useState } from 'react';
3+
import styled from 'styled-components';
24
import { VSCodeLink } from '@vscode/webview-ui-toolkit/react';
35
import { CellValue, RawResultSet, ResultSetSchema } from '../../pure/bqrs-cli-types';
46
import { tryGetRemoteLocation } from '../../pure/bqrs-utils';
5-
import { useState } from 'react';
67
import TextButton from './TextButton';
78
import { convertNonPrintableChars } from '../../text-utils';
89

9-
const borderColor = 'var(--vscode-editor-snippetFinalTabstopHighlightBorder)';
10-
1110
const numOfResultsInContractedMode = 5;
1211

13-
const Row = ({
14-
row,
15-
fileLinkPrefix,
16-
sourceLocationPrefix
17-
}: {
18-
row: CellValue[],
19-
fileLinkPrefix: string,
20-
sourceLocationPrefix: string
21-
}) => (
22-
<>
23-
{row.map((cell, cellIndex) => (
24-
<div key={cellIndex} style={{
25-
borderColor: borderColor,
26-
borderStyle: 'solid',
27-
justifyContent: 'center',
28-
alignItems: 'center',
29-
padding: '0.4rem',
30-
wordBreak: 'break-word'
31-
}}>
32-
<Cell value={cell} fileLinkPrefix={fileLinkPrefix} sourceLocationPrefix={sourceLocationPrefix} />
33-
</div>
34-
))}
35-
</>
36-
);
12+
const StyledRow = styled.div`
13+
border-color: var(--vscode-editor-snippetFinalTabstopHighlightBorder);
14+
border-style: solid;
15+
justify-content: center;
16+
align-items: center;
17+
padding: 0.4rem;
18+
word-break: break-word;
19+
`;
20+
21+
type TableContainerProps = {
22+
columnCount: number;
23+
}
24+
25+
const TableContainer = styled.div<TableContainerProps>`
26+
display: grid;
27+
// Create n equal size columns. We use minmax(0, 1fr) because the
28+
// minimum width of 1fr is auto, not 0.
29+
// https://css-tricks.com/equal-width-columns-in-css-grid-are-kinda-weird/
30+
grid-template-columns: repeat(${props => props.columnCount}, minmax(0, 1fr));
31+
max-width: 45rem;
32+
padding: 0.4rem;
33+
`;
34+
35+
type CellProps = {
36+
value: CellValue;
37+
fileLinkPrefix: string;
38+
sourceLocationPrefix: string;
39+
}
3740

3841
const Cell = ({
3942
value,
4043
fileLinkPrefix,
4144
sourceLocationPrefix
42-
}: {
43-
value: CellValue,
44-
fileLinkPrefix: string
45-
sourceLocationPrefix: string
46-
}) => {
45+
}: CellProps) => {
4746
switch (typeof value) {
4847
case 'string':
4948
case 'number':
@@ -61,44 +60,64 @@ const Cell = ({
6160
}
6261
};
6362

63+
type RowProps = {
64+
row: CellValue[];
65+
fileLinkPrefix: string;
66+
sourceLocationPrefix: string;
67+
}
68+
69+
const Row = ({
70+
row,
71+
fileLinkPrefix,
72+
sourceLocationPrefix
73+
}: RowProps) => (
74+
<>
75+
{row.map((cell, cellIndex) => (
76+
<StyledRow key={cellIndex}>
77+
<Cell
78+
value={cell}
79+
fileLinkPrefix={fileLinkPrefix}
80+
sourceLocationPrefix={sourceLocationPrefix}
81+
/>
82+
</StyledRow>
83+
))}
84+
</>
85+
);
86+
87+
type RawResultsTableProps = {
88+
schema: ResultSetSchema;
89+
results: RawResultSet;
90+
fileLinkPrefix: string;
91+
sourceLocationPrefix: string;
92+
}
93+
6494
const RawResultsTable = ({
6595
schema,
6696
results,
6797
fileLinkPrefix,
6898
sourceLocationPrefix
69-
}: {
70-
schema: ResultSetSchema,
71-
results: RawResultSet,
72-
fileLinkPrefix: string,
73-
sourceLocationPrefix: string
74-
}) => {
99+
}: RawResultsTableProps) => {
75100
const [tableExpanded, setTableExpanded] = useState(false);
76101
const numOfResultsToShow = tableExpanded ? results.rows.length : numOfResultsInContractedMode;
77102
const showButton = results.rows.length > numOfResultsInContractedMode;
78103

79-
// Create n equal size columns. We use minmax(0, 1fr) because the
80-
// minimum width of 1fr is auto, not 0.
81-
// https://css-tricks.com/equal-width-columns-in-css-grid-are-kinda-weird/
82-
const gridTemplateColumns = `repeat(${schema.columns.length}, minmax(0, 1fr))`;
83-
84104
return (
85105
<>
86-
<div style={{
87-
display: 'grid',
88-
gridTemplateColumns: gridTemplateColumns,
89-
maxWidth: '45rem',
90-
padding: '0.4rem'
91-
}}>
106+
<TableContainer columnCount={schema.columns.length}>
92107
{results.rows.slice(0, numOfResultsToShow).map((row, rowIndex) => (
93-
<Row key={rowIndex} row={row} fileLinkPrefix={fileLinkPrefix} sourceLocationPrefix={sourceLocationPrefix} />
108+
<Row
109+
key={rowIndex}
110+
row={row}
111+
fileLinkPrefix={fileLinkPrefix}
112+
sourceLocationPrefix={sourceLocationPrefix}
113+
/>
94114
))}
95-
</div>
96-
{
97-
showButton &&
98-
<TextButton size='x-small' onClick={() => setTableExpanded(!tableExpanded)}>
115+
</TableContainer>
116+
{showButton && (
117+
<TextButton size="x-small" onClick={() => setTableExpanded(!tableExpanded)}>
99118
{tableExpanded ? (<span>View less</span>) : (<span>View all</span>)}
100119
</TextButton>
101-
}
120+
)}
102121
</>
103122
);
104123
};

0 commit comments

Comments
 (0)