Skip to content

Commit c99b7e2

Browse files
fix(Schema): display keys in right order (#596)
1 parent c92953b commit c99b7e2

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/containers/Tenant/Schema/SchemaViewer/SchemaViewer.tsx

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {useMemo} from 'react';
12
import cn from 'bem-cn-lite';
23

34
import DataTable, {Column} from '@gravity-ui/react-data-table';
@@ -28,6 +29,16 @@ interface SchemaViewerProps {
2829
}
2930

3031
export const SchemaViewer = ({keyColumnIds = [], columns = [], type}: SchemaViewerProps) => {
32+
// Keys should be displayd by their order in keyColumnIds (Primary Key)
33+
const keyColumnsOrderValues = useMemo(() => {
34+
return keyColumnIds.reduce<Record<number, number>>((result, keyColumnId, index) => {
35+
// Put columns with negative values, so they will be the first with ascending sort
36+
// Minus keyColumnIds.length for the first key, -1 for the last
37+
result[keyColumnId] = index - keyColumnIds.length;
38+
return result;
39+
}, {});
40+
}, [keyColumnIds]);
41+
3142
let dataTableColumns: Column<TColumnDescription>[] = [
3243
{
3344
name: SchemaViewerColumns.id,
@@ -36,8 +47,11 @@ export const SchemaViewer = ({keyColumnIds = [], columns = [], type}: SchemaView
3647
{
3748
name: SchemaViewerColumns.key,
3849
width: 40,
50+
// Table should start with key columns on sort click
51+
defaultOrder: DataTable.ASCENDING,
3952
sortAccessor: (row) => {
40-
return row.Id && keyColumnIds.includes(row.Id) ? 1 : 0;
53+
// Values in keyColumnsOrderValues are always negative, so it will be 1 for not key columns
54+
return (row.Id && keyColumnsOrderValues[row.Id]) || 1;
4155
},
4256
render: ({row}) => {
4357
return row.Id && keyColumnIds.includes(row.Id) ? (
@@ -58,6 +72,8 @@ export const SchemaViewer = ({keyColumnIds = [], columns = [], type}: SchemaView
5872
{
5973
name: SchemaViewerColumns.notNull,
6074
width: 100,
75+
// Table should start with notNull columns on sort click
76+
defaultOrder: DataTable.DESCENDING,
6177
render: ({row}) => {
6278
if (row.NotNull) {
6379
return '\u2713';
@@ -75,22 +91,14 @@ export const SchemaViewer = ({keyColumnIds = [], columns = [], type}: SchemaView
7591
);
7692
}
7793

78-
// Display key columns first
79-
const tableData = columns.sort((column) => {
80-
if (column.Id && keyColumnIds.includes(column.Id)) {
81-
return 1;
82-
}
83-
return -1;
84-
});
85-
8694
return (
8795
<div className={b()}>
8896
<DataTable
8997
theme="yandex-cloud"
90-
data={tableData}
98+
data={columns}
9199
columns={dataTableColumns}
92100
settings={DEFAULT_TABLE_SETTINGS}
93-
initialSortOrder={{columnId: SchemaViewerColumns.key, order: DataTable.DESCENDING}}
101+
initialSortOrder={{columnId: SchemaViewerColumns.key, order: DataTable.ASCENDING}}
94102
/>
95103
</div>
96104
);

0 commit comments

Comments
 (0)