Skip to content

Commit 81874d6

Browse files
committed
fix: dynamic column width
1 parent 77b21c1 commit 81874d6

File tree

2 files changed

+18
-53
lines changed

2 files changed

+18
-53
lines changed

src/components/QueryResultTable/QueryResultTable.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {Column, Settings} from '@gravity-ui/react-data-table';
66
import type {ColumnType, KeyValueRow} from '../../types/api/query';
77
import {cn} from '../../utils/cn';
88
import {DEFAULT_TABLE_SETTINGS} from '../../utils/constants';
9-
import {getColumnType, getColumnWidthByType, prepareQueryResponse} from '../../utils/query';
9+
import {getColumnType, prepareQueryResponse} from '../../utils/query';
1010
import {isNumeric} from '../../utils/utils';
1111
import type {ResizeableDataTableProps} from '../ResizeableDataTable/ResizeableDataTable';
1212
import {ResizeableDataTable} from '../ResizeableDataTable/ResizeableDataTable';
@@ -25,17 +25,30 @@ const TABLE_SETTINGS: Settings = {
2525

2626
export const b = cn('ydb-query-result-table');
2727

28-
const prepareTypedColumns = (columns: ColumnType[]) => {
28+
const WIDTH_PREDICTION_ROWS_COUNT = 100;
29+
const MAX_COLUMN_WIDTH = 600;
30+
31+
const prepareTypedColumns = (columns: ColumnType[], data?: KeyValueRow[]) => {
2932
if (!columns.length) {
3033
return [];
3134
}
3235

36+
const dataSlice = data?.slice(0, WIDTH_PREDICTION_ROWS_COUNT);
37+
3338
return columns.map(({name, type}) => {
3439
const columnType = getColumnType(type);
3540

41+
const maxColumnContentLength =
42+
dataSlice?.reduce(
43+
(max, row) => Math.max(max, row[name] ? String(row[name]).length : max),
44+
name.length,
45+
) || name.length;
46+
47+
const headerPadding = columnType === 'number' ? 40 : 20;
48+
3649
const column: Column<KeyValueRow> = {
3750
name,
38-
width: getColumnWidthByType(type, name),
51+
width: Math.min(maxColumnContentLength * 10 + headerPadding, MAX_COLUMN_WIDTH),
3952
align: columnType === 'number' ? DataTable.RIGHT : DataTable.LEFT,
4053
sortAccessor: (row) => {
4154
const value = row[name];
@@ -83,7 +96,7 @@ export const QueryResultTable = (props: QueryResultTableProps) => {
8396

8497
const data = React.useMemo(() => prepareQueryResponse(rawData), [rawData]);
8598
const columns = React.useMemo(() => {
86-
return rawColumns ? prepareTypedColumns(rawColumns) : prepareGenericColumns(data);
99+
return rawColumns ? prepareTypedColumns(rawColumns, data) : prepareGenericColumns(data);
87100
}, [data, rawColumns]);
88101
const settings = React.useMemo(
89102
() => ({

src/utils/query.ts

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,9 @@ export const QUERY_SYNTAX = {
9797
pg: 'pg',
9898
} as const;
9999

100-
export const getYQLColumnType = (type: string): YQLType => {
101-
return type.replace(/\?$/, '') as YQLType;
102-
};
103-
104100
// eslint-disable-next-line complexity
105101
export const getColumnType = (type: string) => {
106-
switch (getYQLColumnType(type)) {
102+
switch (type.replace(/\?$/, '')) {
107103
case YQLType.Bool:
108104
return 'boolean';
109105
case YQLType.Int8:
@@ -138,50 +134,6 @@ export const getColumnType = (type: string) => {
138134
}
139135
};
140136

141-
const columnTypeToDefaultWidth: Record<YQLType, number> = {
142-
// Numeric
143-
[YQLType.Bool]: 80,
144-
[YQLType.Int8]: 80,
145-
[YQLType.Int16]: 90,
146-
[YQLType.Int32]: 140,
147-
[YQLType.Int64]: 220,
148-
[YQLType.Uint8]: 80,
149-
[YQLType.Uint16]: 90,
150-
[YQLType.Uint32]: 140,
151-
[YQLType.Uint64]: 220,
152-
[YQLType.Float]: 120,
153-
[YQLType.Double]: 220,
154-
[YQLType.Decimal]: 220,
155-
156-
// String
157-
[YQLType.String]: 240,
158-
[YQLType.Utf8]: 240,
159-
[YQLType.Json]: 340,
160-
[YQLType.JsonDocument]: 340,
161-
[YQLType.Yson]: 340,
162-
[YQLType.Uuid]: 190,
163-
164-
// Date and time
165-
[YQLType.Date]: 300,
166-
[YQLType.Datetime]: 300,
167-
[YQLType.Timestamp]: 300,
168-
[YQLType.Interval]: 300,
169-
[YQLType.TzDate]: 300,
170-
[YQLType.TzDateTime]: 300,
171-
[YQLType.TzTimestamp]: 300,
172-
};
173-
174-
const COLUMN_DEFAULT_WIDTH = 200;
175-
176-
export const getColumnWidthByType = (type: string, columnName: string) => {
177-
const yqlType = getYQLColumnType(type);
178-
179-
return Math.max(
180-
columnTypeToDefaultWidth[yqlType] || COLUMN_DEFAULT_WIDTH,
181-
columnName.length * 15,
182-
);
183-
};
184-
185137
/** parse response result from ArrayRow to KeyValueRow */
186138
const parseModernResult = (rows: ArrayRow[], columns: ColumnType[]) => {
187139
return rows.map((row) => {

0 commit comments

Comments
 (0)