diff --git a/ui/src/shared/components/RawFluxDataGrid.tsx b/ui/src/shared/components/RawFluxDataGrid.tsx new file mode 100644 index 0000000000..424cabdf9c --- /dev/null +++ b/ui/src/shared/components/RawFluxDataGrid.tsx @@ -0,0 +1,116 @@ +import React, {PureComponent, CSSProperties} from 'react' +import _ from 'lodash' +import {Grid} from 'react-virtualized' + +const ROW_HEIGHT = 27 +const MIN_COLUMN_WIDTH = 150 +const TIME_COLUMN_WIDTH = 300 + +interface Props { + data: string[][] + maxColumnCount: number + width: number + height: number + scrollLeft: number + scrollTop: number +} + +interface State { + headerRows: number[] +} + +export default class extends PureComponent { + public static getDerivedStateFromProps(nextProps: Props): Partial { + const headerRows = _.reduce( + nextProps.data, + (acc, row, index) => { + if (row[0] === '#datatype') { + acc.push(index) + } + + return acc + }, + [] + ) + + return {headerRows} + } + constructor(props: Props) { + super(props) + + this.state = {headerRows: []} + } + + public render() { + const {maxColumnCount, width, height, scrollTop, scrollLeft} = this.props + + return ( + + ) + } + + private get rowCount(): number { + return this.props.data.length + } + + private columnWidth = ({index}): number => { + const {maxColumnCount, width} = this.props + + const isDateTimeColumn = _.find(this.state.headerRows, i => { + return (this.getCellData(i, index) || '').includes('dateTime') + }) + + if (!isDateTimeColumn) { + return Math.max(MIN_COLUMN_WIDTH, width / maxColumnCount) + } + + return TIME_COLUMN_WIDTH + } + + private get gridStyle(): CSSProperties { + const width = this.calculateWidth() + const height = ROW_HEIGHT * this.rowCount + + return {width, height} + } + + private renderCell = ({columnIndex, key, rowIndex, style}) => { + const datum = this.getCellData(rowIndex, columnIndex) + + return ( +
+
{datum}
+
+ ) + } + + private calculateWidth(): number { + const {maxColumnCount} = this.props + return _.reduce( + _.range(0, maxColumnCount), + (acc, index) => acc + this.columnWidth({index}), + 0 + ) + } + + private getCellData(row, column) { + const {data} = this.props + return data[row][column] + } +} diff --git a/ui/src/shared/components/RawFluxDataTable.tsx b/ui/src/shared/components/RawFluxDataTable.tsx index 3eb548f080..9ffd714d5a 100644 --- a/ui/src/shared/components/RawFluxDataTable.tsx +++ b/ui/src/shared/components/RawFluxDataTable.tsx @@ -1,7 +1,7 @@ // Libraries -import React, {PureComponent, MouseEvent, CSSProperties} from 'react' -import {Grid} from 'react-virtualized' +import React, {PureComponent, MouseEvent} from 'react' import memoizeOne from 'memoize-one' +import RawFluxDataGrid from 'src/shared/components/RawFluxDataGrid' // Components import FancyScrollbar from 'src/shared/components/fancy_scrollbar/FancyScrollbar' @@ -20,8 +20,6 @@ interface State { scrollTop: number } -const ROW_HEIGHT = 30 -const MIN_COLUMN_WIDTH = 100 const PADDING = 10 class RawFluxDataTable extends PureComponent { @@ -50,58 +48,19 @@ class RawFluxDataTable extends PureComponent { scrollLeft={scrollLeft} setScrollTop={this.onScrollbarsScroll} > - {this.renderGrid( - tableWidth, - tableHeight, - data, - maxColumnCount, - scrollLeft, - scrollTop - )} + ) } - private renderGrid( - width: number, - height: number, - data: string[][], - maxColumnCount: number, - scrollLeft: number, - scrollTop: number - ): JSX.Element { - const rowCount = data.length - const columnWidth = Math.max(MIN_COLUMN_WIDTH, width / maxColumnCount) - const style = this.gridStyle(columnWidth, maxColumnCount, rowCount) - - return ( - - ) - } - - private gridStyle( - columnWidth: number, - maxColumnCount: number, - rowCount: number - ): CSSProperties { - const width = columnWidth * maxColumnCount - const height = ROW_HEIGHT * rowCount - - return {width, height} - } - private onScrollbarsScroll = (e: MouseEvent) => { e.preventDefault() e.stopPropagation() @@ -110,26 +69,6 @@ class RawFluxDataTable extends PureComponent { this.setState({scrollLeft, scrollTop}) } - - private renderCell = (data: string[][]) => ({ - columnIndex, - key, - rowIndex, - style, - }) => { - const datum = data[rowIndex][columnIndex] - - return ( -
-
{datum}
-
- ) - } } export default RawFluxDataTable diff --git a/ui/src/shared/utils/rawFluxDataTable.ts b/ui/src/shared/utils/rawFluxDataTable.ts index ad58019e61..7360f65ae2 100644 --- a/ui/src/shared/utils/rawFluxDataTable.ts +++ b/ui/src/shared/utils/rawFluxDataTable.ts @@ -2,7 +2,7 @@ import Papa from 'papaparse' import {parseChunks} from 'src/shared/parsing/flux/response' -interface ParseFilesResult { +export interface ParseFilesResult { data: string[][] maxColumnCount: number }