Merge pull request #1546 from github/koesie10/refactor-raw-results-table
Refactor `RawResultsTable` component
This commit is contained in:
@@ -1,49 +1,48 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import { useState } from 'react';
|
||||||
|
import styled from 'styled-components';
|
||||||
import { VSCodeLink } from '@vscode/webview-ui-toolkit/react';
|
import { VSCodeLink } from '@vscode/webview-ui-toolkit/react';
|
||||||
import { CellValue, RawResultSet, ResultSetSchema } from '../../pure/bqrs-cli-types';
|
import { CellValue, RawResultSet, ResultSetSchema } from '../../pure/bqrs-cli-types';
|
||||||
import { tryGetRemoteLocation } from '../../pure/bqrs-utils';
|
import { tryGetRemoteLocation } from '../../pure/bqrs-utils';
|
||||||
import { useState } from 'react';
|
|
||||||
import TextButton from './TextButton';
|
import TextButton from './TextButton';
|
||||||
import { convertNonPrintableChars } from '../../text-utils';
|
import { convertNonPrintableChars } from '../../text-utils';
|
||||||
|
|
||||||
const borderColor = 'var(--vscode-editor-snippetFinalTabstopHighlightBorder)';
|
|
||||||
|
|
||||||
const numOfResultsInContractedMode = 5;
|
const numOfResultsInContractedMode = 5;
|
||||||
|
|
||||||
const Row = ({
|
const StyledRow = styled.div`
|
||||||
row,
|
border-color: var(--vscode-editor-snippetFinalTabstopHighlightBorder);
|
||||||
fileLinkPrefix,
|
border-style: solid;
|
||||||
sourceLocationPrefix
|
justify-content: center;
|
||||||
}: {
|
align-items: center;
|
||||||
row: CellValue[],
|
padding: 0.4rem;
|
||||||
fileLinkPrefix: string,
|
word-break: break-word;
|
||||||
sourceLocationPrefix: string
|
`;
|
||||||
}) => (
|
|
||||||
<>
|
type TableContainerProps = {
|
||||||
{row.map((cell, cellIndex) => (
|
columnCount: number;
|
||||||
<div key={cellIndex} style={{
|
}
|
||||||
borderColor: borderColor,
|
|
||||||
borderStyle: 'solid',
|
const TableContainer = styled.div<TableContainerProps>`
|
||||||
justifyContent: 'center',
|
display: grid;
|
||||||
alignItems: 'center',
|
// Create n equal size columns. We use minmax(0, 1fr) because the
|
||||||
padding: '0.4rem',
|
// minimum width of 1fr is auto, not 0.
|
||||||
wordBreak: 'break-word'
|
// https://css-tricks.com/equal-width-columns-in-css-grid-are-kinda-weird/
|
||||||
}}>
|
grid-template-columns: repeat(${props => props.columnCount}, minmax(0, 1fr));
|
||||||
<Cell value={cell} fileLinkPrefix={fileLinkPrefix} sourceLocationPrefix={sourceLocationPrefix} />
|
max-width: 45rem;
|
||||||
</div>
|
padding: 0.4rem;
|
||||||
))}
|
`;
|
||||||
</>
|
|
||||||
);
|
type CellProps = {
|
||||||
|
value: CellValue;
|
||||||
|
fileLinkPrefix: string;
|
||||||
|
sourceLocationPrefix: string;
|
||||||
|
}
|
||||||
|
|
||||||
const Cell = ({
|
const Cell = ({
|
||||||
value,
|
value,
|
||||||
fileLinkPrefix,
|
fileLinkPrefix,
|
||||||
sourceLocationPrefix
|
sourceLocationPrefix
|
||||||
}: {
|
}: CellProps) => {
|
||||||
value: CellValue,
|
|
||||||
fileLinkPrefix: string
|
|
||||||
sourceLocationPrefix: string
|
|
||||||
}) => {
|
|
||||||
switch (typeof value) {
|
switch (typeof value) {
|
||||||
case 'string':
|
case 'string':
|
||||||
case 'number':
|
case 'number':
|
||||||
@@ -61,44 +60,64 @@ const Cell = ({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type RowProps = {
|
||||||
|
row: CellValue[];
|
||||||
|
fileLinkPrefix: string;
|
||||||
|
sourceLocationPrefix: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Row = ({
|
||||||
|
row,
|
||||||
|
fileLinkPrefix,
|
||||||
|
sourceLocationPrefix
|
||||||
|
}: RowProps) => (
|
||||||
|
<>
|
||||||
|
{row.map((cell, cellIndex) => (
|
||||||
|
<StyledRow key={cellIndex}>
|
||||||
|
<Cell
|
||||||
|
value={cell}
|
||||||
|
fileLinkPrefix={fileLinkPrefix}
|
||||||
|
sourceLocationPrefix={sourceLocationPrefix}
|
||||||
|
/>
|
||||||
|
</StyledRow>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
|
||||||
|
type RawResultsTableProps = {
|
||||||
|
schema: ResultSetSchema;
|
||||||
|
results: RawResultSet;
|
||||||
|
fileLinkPrefix: string;
|
||||||
|
sourceLocationPrefix: string;
|
||||||
|
}
|
||||||
|
|
||||||
const RawResultsTable = ({
|
const RawResultsTable = ({
|
||||||
schema,
|
schema,
|
||||||
results,
|
results,
|
||||||
fileLinkPrefix,
|
fileLinkPrefix,
|
||||||
sourceLocationPrefix
|
sourceLocationPrefix
|
||||||
}: {
|
}: RawResultsTableProps) => {
|
||||||
schema: ResultSetSchema,
|
|
||||||
results: RawResultSet,
|
|
||||||
fileLinkPrefix: string,
|
|
||||||
sourceLocationPrefix: string
|
|
||||||
}) => {
|
|
||||||
const [tableExpanded, setTableExpanded] = useState(false);
|
const [tableExpanded, setTableExpanded] = useState(false);
|
||||||
const numOfResultsToShow = tableExpanded ? results.rows.length : numOfResultsInContractedMode;
|
const numOfResultsToShow = tableExpanded ? results.rows.length : numOfResultsInContractedMode;
|
||||||
const showButton = results.rows.length > numOfResultsInContractedMode;
|
const showButton = results.rows.length > numOfResultsInContractedMode;
|
||||||
|
|
||||||
// Create n equal size columns. We use minmax(0, 1fr) because the
|
|
||||||
// minimum width of 1fr is auto, not 0.
|
|
||||||
// https://css-tricks.com/equal-width-columns-in-css-grid-are-kinda-weird/
|
|
||||||
const gridTemplateColumns = `repeat(${schema.columns.length}, minmax(0, 1fr))`;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div style={{
|
<TableContainer columnCount={schema.columns.length}>
|
||||||
display: 'grid',
|
|
||||||
gridTemplateColumns: gridTemplateColumns,
|
|
||||||
maxWidth: '45rem',
|
|
||||||
padding: '0.4rem'
|
|
||||||
}}>
|
|
||||||
{results.rows.slice(0, numOfResultsToShow).map((row, rowIndex) => (
|
{results.rows.slice(0, numOfResultsToShow).map((row, rowIndex) => (
|
||||||
<Row key={rowIndex} row={row} fileLinkPrefix={fileLinkPrefix} sourceLocationPrefix={sourceLocationPrefix} />
|
<Row
|
||||||
|
key={rowIndex}
|
||||||
|
row={row}
|
||||||
|
fileLinkPrefix={fileLinkPrefix}
|
||||||
|
sourceLocationPrefix={sourceLocationPrefix}
|
||||||
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</TableContainer>
|
||||||
{
|
{showButton && (
|
||||||
showButton &&
|
<TextButton size="x-small" onClick={() => setTableExpanded(!tableExpanded)}>
|
||||||
<TextButton size='x-small' onClick={() => setTableExpanded(!tableExpanded)}>
|
|
||||||
{tableExpanded ? (<span>View less</span>) : (<span>View all</span>)}
|
{tableExpanded ? (<span>View less</span>) : (<span>View all</span>)}
|
||||||
</TextButton>
|
</TextButton>
|
||||||
}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user