-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSchemaViewer.tsx
More file actions
111 lines (95 loc) · 3.69 KB
/
SchemaViewer.tsx
File metadata and controls
111 lines (95 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import React from 'react';
import {ResizeableDataTable} from '../../../../components/ResizeableDataTable/ResizeableDataTable';
import {TableSkeleton} from '../../../../components/TableSkeleton/TableSkeleton';
import {overviewApi} from '../../../../store/reducers/overview/overview';
import {viewSchemaApi} from '../../../../store/reducers/viewSchema/viewSchema';
import type {EPathType} from '../../../../types/api/schema';
import {DEFAULT_TABLE_SETTINGS} from '../../../../utils/constants';
import {useAutoRefreshInterval} from '../../../../utils/hooks';
import {
isColumnEntityType,
isExternalTableType,
isRowTableType,
isViewType,
} from '../../utils/schema';
import {KeysView} from './KeysView';
import {
SCHEMA_COLUMNS_WIDTH_LS_KEY,
getColumnTableColumns,
getExternalTableColumns,
getRowTableColumns,
getViewColumns,
} from './columns';
import {prepareSchemaData, prepareViewSchema} from './prepareData';
import {b} from './shared';
import './SchemaViewer.scss';
interface SchemaViewerProps {
type?: EPathType;
path: string;
tenantName: string;
extended?: boolean;
}
export const SchemaViewer = ({type, path, tenantName, extended = false}: SchemaViewerProps) => {
const [autoRefreshInterval] = useAutoRefreshInterval();
// Refresh table only in Diagnostics
const pollingInterval = extended ? autoRefreshInterval : undefined;
const {currentData: tableSchemaData, isFetching: isTableSchemaFetching} =
overviewApi.useGetOverviewQuery(
{path, database: tenantName},
{pollingInterval, skip: isViewType(type)},
);
const {currentData: viewColumnsData, isFetching: isViewSchemaFetching} =
viewSchemaApi.useGetViewSchemaQuery(
{path, database: tenantName},
{pollingInterval, skip: !isViewType(type)},
);
const loading =
(isViewSchemaFetching && viewColumnsData === undefined) ||
(isTableSchemaFetching && tableSchemaData === undefined);
const tableData = React.useMemo(() => {
if (isViewType(type)) {
return prepareViewSchema(viewColumnsData);
}
return prepareSchemaData(type, tableSchemaData);
}, [tableSchemaData, type, viewColumnsData]);
const hasAutoIncrement = React.useMemo(() => {
return tableData.some((i) => i.autoIncrement);
}, [tableData]);
const hasDefaultValue = React.useMemo(() => {
return tableData.some((i) => i.defaultValue);
}, [tableData]);
const columns = React.useMemo(() => {
if (isViewType(type)) {
return getViewColumns(tableData);
}
if (isExternalTableType(type)) {
return getExternalTableColumns(tableData);
}
if (isColumnEntityType(type)) {
return getColumnTableColumns(tableData);
}
if (isRowTableType(type)) {
return getRowTableColumns(tableData, extended, hasAutoIncrement, hasDefaultValue);
}
return [];
}, [type, extended, hasAutoIncrement, hasDefaultValue, tableData]);
if (loading) {
return <TableSkeleton />;
}
return (
<React.Fragment>
<div className={b('keys-wrapper')}>
<KeysView tableData={tableData} extended={extended} type="primary" />
<KeysView tableData={tableData} extended={extended} type="partitioning" />
</div>
<div className={b()}>
<ResizeableDataTable
columnsWidthLSKey={SCHEMA_COLUMNS_WIDTH_LS_KEY}
data={tableData}
columns={columns}
settings={DEFAULT_TABLE_SETTINGS}
/>
</div>
</React.Fragment>
);
};