Skip to content

Commit 77b21c1

Browse files
committed
fix: fix columns width on long rowset
1 parent 5f26767 commit 77b21c1

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/components/QueryResultTable/QueryResultTable.tsx

Lines changed: 2 additions & 1 deletion
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, prepareQueryResponse} from '../../utils/query';
9+
import {getColumnType, getColumnWidthByType, prepareQueryResponse} from '../../utils/query';
1010
import {isNumeric} from '../../utils/utils';
1111
import type {ResizeableDataTableProps} from '../ResizeableDataTable/ResizeableDataTable';
1212
import {ResizeableDataTable} from '../ResizeableDataTable/ResizeableDataTable';
@@ -35,6 +35,7 @@ const prepareTypedColumns = (columns: ColumnType[]) => {
3535

3636
const column: Column<KeyValueRow> = {
3737
name,
38+
width: getColumnWidthByType(type, name),
3839
align: columnType === 'number' ? DataTable.RIGHT : DataTable.LEFT,
3940
sortAccessor: (row) => {
4041
const value = row[name];

src/utils/query.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ 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+
100104
// eslint-disable-next-line complexity
101105
export const getColumnType = (type: string) => {
102-
switch (type.replace(/\?$/, '')) {
106+
switch (getYQLColumnType(type)) {
103107
case YQLType.Bool:
104108
return 'boolean';
105109
case YQLType.Int8:
@@ -134,6 +138,50 @@ export const getColumnType = (type: string) => {
134138
}
135139
};
136140

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+
137185
/** parse response result from ArrayRow to KeyValueRow */
138186
const parseModernResult = (rows: ArrayRow[], columns: ColumnType[]) => {
139187
return rows.map((row) => {

0 commit comments

Comments
 (0)