-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTenant.tsx
169 lines (145 loc) · 6.19 KB
/
Tenant.tsx
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import React from 'react';
import {Helmet} from 'react-helmet-async';
import {StringParam, useQueryParams} from 'use-query-params';
import {PageError} from '../../components/Errors/PageError/PageError';
import {LoaderWrapper} from '../../components/LoaderWrapper/LoaderWrapper';
import SplitPane from '../../components/SplitPane';
import {setHeaderBreadcrumbs} from '../../store/reducers/header/header';
import {overviewApi} from '../../store/reducers/overview/overview';
import {selectSchemaObjectData} from '../../store/reducers/schema/schema';
import type {AdditionalNodesProps, AdditionalTenantsProps} from '../../types/additionalProps';
import {cn} from '../../utils/cn';
import {DEFAULT_IS_TENANT_SUMMARY_COLLAPSED, DEFAULT_SIZE_TENANT_KEY} from '../../utils/constants';
import {useTypedDispatch, useTypedSelector} from '../../utils/hooks';
import {isAccessError} from '../../utils/response';
import ObjectGeneral from './ObjectGeneral/ObjectGeneral';
import {ObjectSummary} from './ObjectSummary/ObjectSummary';
import i18n from './i18n';
import {
PaneVisibilityActionTypes,
paneVisibilityToggleReducerCreator,
} from './utils/paneVisibilityToggleHelpers';
import './Tenant.scss';
const b = cn('tenant-page');
const getTenantSummaryState = () => {
const collapsed = Boolean(localStorage.getItem(DEFAULT_IS_TENANT_SUMMARY_COLLAPSED));
return {
triggerExpand: false,
triggerCollapse: false,
collapsed,
};
};
interface TenantProps {
additionalTenantProps?: AdditionalTenantsProps;
additionalNodesProps?: AdditionalNodesProps;
}
export function Tenant(props: TenantProps) {
const [summaryVisibilityState, dispatchSummaryVisibilityAction] = React.useReducer(
paneVisibilityToggleReducerCreator(DEFAULT_IS_TENANT_SUMMARY_COLLAPSED),
undefined,
getTenantSummaryState,
);
// TODO: name is used together with database to keep old links valid
// Remove it after some time - 1-2 weeks
const [{database, name, schema}, setQuery] = useQueryParams({
database: StringParam,
name: StringParam,
schema: StringParam,
});
React.useEffect(() => {
if (name && !database) {
setQuery({database: name, name: undefined}, 'replaceIn');
}
}, [database, name, setQuery]);
const tenantName = database ?? name;
if (!tenantName) {
throw new Error('Tenant name is not defined');
}
const previousTenant = React.useRef<string>();
React.useEffect(() => {
if (previousTenant.current !== tenantName) {
const register = async () => {
const {registerYQLCompletionItemProvider} = await import(
'../../utils/monaco/yql/yql.completionItemProvider'
);
registerYQLCompletionItemProvider(tenantName);
};
register().catch(console.error);
previousTenant.current = tenantName;
}
}, [tenantName]);
const dispatch = useTypedDispatch();
React.useEffect(() => {
dispatch(setHeaderBreadcrumbs('tenant', {tenantName}));
}, [tenantName, dispatch]);
const path = schema ?? tenantName;
const {
currentData: currentItem,
error,
isLoading,
} = overviewApi.useGetOverviewQuery({path, database: tenantName});
const preloadedData = useTypedSelector((state) =>
selectSchemaObjectData(state, path, tenantName),
);
// Use preloaded data if there is no current item data yet
const currentPathType =
currentItem?.PathDescription?.Self?.PathType ??
preloadedData?.PathDescription?.Self?.PathType;
const currentPathSubType =
currentItem?.PathDescription?.Self?.PathSubType ??
preloadedData?.PathDescription?.Self?.PathSubType;
const showBlockingError = isAccessError(error);
const onCollapseSummaryHandler = () => {
dispatchSummaryVisibilityAction(PaneVisibilityActionTypes.triggerCollapse);
};
const onExpandSummaryHandler = () => {
dispatchSummaryVisibilityAction(PaneVisibilityActionTypes.triggerExpand);
};
const onSplitStartDragAdditional = () => {
dispatchSummaryVisibilityAction(PaneVisibilityActionTypes.clear);
};
const [initialLoading, setInitialLoading] = React.useState(true);
if (initialLoading && !isLoading) {
setInitialLoading(false);
}
const title = path || i18n('page.title');
return (
<div className={b()}>
<Helmet
defaultTitle={`${title} — YDB Monitoring`}
titleTemplate={`%s — ${title} — YDB Monitoring`}
/>
<LoaderWrapper loading={initialLoading}>
<PageError error={showBlockingError ? error : undefined}>
<SplitPane
defaultSizePaneKey={DEFAULT_SIZE_TENANT_KEY}
defaultSizes={[25, 75]}
triggerCollapse={summaryVisibilityState.triggerCollapse}
triggerExpand={summaryVisibilityState.triggerExpand}
minSize={[36, 200]}
onSplitStartDragAdditional={onSplitStartDragAdditional}
>
<ObjectSummary
type={currentPathType}
subType={currentPathSubType}
tenantName={tenantName}
path={path}
onCollapseSummary={onCollapseSummaryHandler}
onExpandSummary={onExpandSummaryHandler}
isCollapsed={summaryVisibilityState.collapsed}
/>
<div className={b('main')}>
<ObjectGeneral
type={currentPathType}
additionalTenantProps={props.additionalTenantProps}
additionalNodesProps={props.additionalNodesProps}
tenantName={tenantName}
path={path}
/>
</div>
</SplitPane>
</PageError>
</LoaderWrapper>
</div>
);
}