From a984bca79548030e710428da2b86d94bed2101eb Mon Sep 17 00:00:00 2001 From: mmarkauskas Date: Thu, 14 Mar 2024 12:16:38 +0200 Subject: [PATCH] Refactor --- .../Layout/Query/QueryFormTable.tsx | 210 ++++++++++++++++++ src/view/app/Query/query.tsx | 189 +++------------- 2 files changed, 236 insertions(+), 163 deletions(-) create mode 100644 src/view/app/Components/Layout/Query/QueryFormTable.tsx diff --git a/src/view/app/Components/Layout/Query/QueryFormTable.tsx b/src/view/app/Components/Layout/Query/QueryFormTable.tsx new file mode 100644 index 00000000..498df274 --- /dev/null +++ b/src/view/app/Components/Layout/Query/QueryFormTable.tsx @@ -0,0 +1,210 @@ +import { CSSProperties, Fragment, UIEvent } from 'react'; +import DataGrid, { + CopyEvent, + DataGridHandle, + SortColumn, +} from 'react-data-grid'; +import { Box } from '@mui/material'; +import { IFilters } from '@app/common/types'; + +const filterCSS: CSSProperties = { + inlineSize: '100%', + padding: '4px', + fontSize: '14px', +}; + +interface QueryFormTableProps { + queryGridRef: React.RefObject; + selected: any[]; + isFormatted: boolean; + formattedRows: any[]; + rawRows: any[]; + sortColumns: SortColumn[]; + handleScroll: (event: UIEvent) => void; + onSortClick: (inputSortColumns: SortColumn[]) => void; + filters: IFilters; + selectedRows: Set; + setSelectedRows: React.Dispatch>>; + rowKeyGetter: (row: any) => string; + readRecord: (row: any) => void; + handleCopy: (event: CopyEvent) => void; + calculateHeight: () => number; + windowHeight: number; + setRowHeight: () => number; + reloadData: (loaded: number) => void; + configuration: any; + setFilters: (data: IFilters) => void; +} + +const headerRenderer = ({ + column, + sortDirection, + priority, + onSort, + isCellSelected, + reloadData, + configuration, + filters, + setFilters, +}) => { + function handleKeyDown(event) { + if (event.key === ' ' || event.key === 'Enter') { + event.preventDefault(); + onSort(event.ctrlKey || event.metaKey); + } + } + + function handleClick(event) { + onSort(event.ctrlKey || event.metaKey); + } + + let timer; + function handleKeyInputTimeout() { + clearTimeout(timer); + timer = setTimeout(() => { + reloadData(configuration.initialBatchSizeLoad); + }, 500); + } + + function testKeyDown(event) { + if (event.key === 'Enter') { + event.preventDefault(); + reloadData(configuration.initialBatchSizeLoad); + } + } + + function handleInputKeyDown(event) { + const tempFilters = filters; + tempFilters.columns[column.key] = event.target.value; + setFilters(tempFilters); + if (configuration.filterAsYouType === true) { + handleKeyInputTimeout(); + } + } + return ( + +
+ + + {column.name} + + + + {sortDirection === 'ASC' && ( + + )} + {sortDirection === 'DESC' && ( + + )} + + {priority} + + +
+ {filters.enabled && ( +
+ +
+ )} +
+ ); +}; + +const QueryFormTable: React.FC = ({ + queryGridRef, + selected, + isFormatted, + formattedRows, + rawRows, + sortColumns, + handleScroll, + onSortClick, + filters, + selectedRows, + setSelectedRows, + rowKeyGetter, + readRecord, + handleCopy, + calculateHeight, + windowHeight, + setRowHeight, + reloadData, + configuration, + setFilters, +}) => { + const adjustedColumns = selected.map((col) => ({ + ...col, + headerRenderer: (props) => + headerRenderer({ + ...props, + reloadData, + configuration, + filters, + setFilters, + }), + })); + return ( + + + + ); +}; + +export default QueryFormTable; diff --git a/src/view/app/Query/query.tsx b/src/view/app/Query/query.tsx index 6d599b07..1b121ce5 100644 --- a/src/view/app/Query/query.tsx +++ b/src/view/app/Query/query.tsx @@ -7,7 +7,7 @@ import { useState, } from 'react'; -import DataGrid, { +import { SortColumn, SelectColumn, CopyEvent, @@ -21,12 +21,12 @@ import { getOEFormatLength } from '@utils/oe/format/oeFormat'; import { OEDataTypePrimitive } from '@utils/oe/oeDataTypeEnum'; import { IErrorObject, emptyErrorObj } from '@utils/error'; import QueryFormFooter from '@app/Components/Layout/Query/QueryFormFooter'; -import { Box } from '@mui/material'; import QueryFormHead from '@app/Components/Layout/Query/QueryFormHead'; import { IFilters } from '@app/common/types'; import { getVSCodeAPI, getVSCodeConfiguration } from '@utils/vscode'; import { green, red } from '@mui/material/colors'; import { HighlightFieldsCommand } from '@src/common/commands/fieldsCommands'; +import QueryFormTable from '@app/Components/Layout/Query/QueryFormTable'; const filterCSS: CSSProperties = { inlineSize: '100%', @@ -46,12 +46,7 @@ interface IStatisticsObject { connectTime: number; } -function QueryForm({ - tableData, - tableName, - isReadOnly, - ...props -}: IConfigProps) { +function QueryForm({ tableData, tableName, isReadOnly }: IConfigProps) { const [wherePhrase, setWherePhrase] = useState(''); const [isLoading, setIsLoading] = useState(false); const [windowHeight, setWindowHeight] = useState(window.innerHeight); @@ -211,133 +206,8 @@ function QueryForm({ ) .getPropertyValue('font-size') .match(/\d+[.]?\d+/); + message.data.columns.forEach((column) => { - column.headerRenderer = function ({ - column, - sortDirection, - priority, - onSort, - isCellSelected, - }) { - function handleKeyDown(event) { - if ( - event.key === ' ' || - event.key === 'Enter' - ) { - event.preventDefault(); - onSort(event.ctrlKey || event.metaKey); - } - } - - function handleClick(event) { - onSort(event.ctrlKey || event.metaKey); - } - - let timer; - function handleKeyInputTimeout() { - clearTimeout(timer); - timer = setTimeout(() => { - reloadData( - configuration.initialBatchSizeLoad - ); - }, 500); - } - - function testKeyDown(event) { - if (event.key === 'Enter') { - event.preventDefault(); - reloadData( - configuration.initialBatchSizeLoad - ); - } - } - - function handleInputKeyDown(event) { - const tempFilters = filters; - tempFilters.columns[column.key] = - event.target.value; - setFilters(tempFilters); - if ( - configuration.filterAsYouType === true - ) { - handleKeyInputTimeout(); - } - } - - return ( - -
- - - {column.name} - - - - {sortDirection === - 'ASC' && ( - - )} - {sortDirection === - 'DESC' && ( - - )} - - {priority} - - -
- {filters.enabled && ( -
- -
- )} -
- ); - }; column.minWidth = column.name.length * fontSize; column.width = getOEFormatLength(column.format ?? '') * @@ -641,35 +511,28 @@ function QueryForm({ readRow={readRow} isReadOnly={isReadOnly} /> - - - +