-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCluster.tsx
More file actions
226 lines (198 loc) · 7.91 KB
/
Cluster.tsx
File metadata and controls
226 lines (198 loc) · 7.91 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import React from 'react';
import {Skeleton, Tabs} from '@gravity-ui/uikit';
import {Helmet} from 'react-helmet-async';
import {Redirect, Route, Switch, useRouteMatch} from 'react-router-dom';
import {StringParam, useQueryParams} from 'use-query-params';
import {AutoRefreshControl} from '../../components/AutoRefreshControl/AutoRefreshControl';
import {EntityStatus} from '../../components/EntityStatusNew/EntityStatus';
import {EFlagToDescription} from '../../components/EntityStatusNew/utils';
import {InternalLink} from '../../components/InternalLink';
import routes, {getLocationObjectFromHref} from '../../routes';
import {useClusterDashboardAvailable} from '../../store/reducers/capabilities/hooks';
import {
clusterApi,
selectClusterTabletsWithFqdn,
selectClusterTitle,
updateDefaultClusterTab,
useClusterBaseInfo,
} from '../../store/reducers/cluster/cluster';
import {setHeaderBreadcrumbs} from '../../store/reducers/header/header';
import type {
AdditionalClusterProps,
AdditionalNodesProps,
AdditionalTenantsProps,
} from '../../types/additionalProps';
import {EFlag} from '../../types/api/enums';
import {cn} from '../../utils/cn';
import {useAutoRefreshInterval, useTypedDispatch, useTypedSelector} from '../../utils/hooks';
import {Nodes} from '../Nodes/Nodes';
import {PaginatedStorage} from '../Storage/PaginatedStorage';
import {TabletsTable} from '../Tablets/TabletsTable';
import {Tenants} from '../Tenants/Tenants';
import {VersionsContainer} from '../Versions/Versions';
import {ClusterOverview} from './ClusterOverview/ClusterOverview';
import type {ClusterTab} from './utils';
import {clusterTabs, clusterTabsIds, getClusterPath, isClusterTab} from './utils';
import './Cluster.scss';
const b = cn('ydb-cluster');
interface ClusterProps {
additionalTenantsProps?: AdditionalTenantsProps;
additionalNodesProps?: AdditionalNodesProps;
additionalClusterProps?: AdditionalClusterProps;
}
export function Cluster({
additionalClusterProps,
additionalTenantsProps,
additionalNodesProps,
}: ClusterProps) {
const container = React.useRef<HTMLDivElement>(null);
const isClusterDashboardAvailable = useClusterDashboardAvailable();
const [autoRefreshInterval] = useAutoRefreshInterval();
const dispatch = useTypedDispatch();
const activeTabId = useClusterTab();
const [{clusterName, backend}] = useQueryParams({
clusterName: StringParam,
backend: StringParam,
});
const viewerClusterTitle = useTypedSelector((state) =>
selectClusterTitle(state, clusterName ?? undefined),
);
const {title: metaClusterTitle} = useClusterBaseInfo();
const clusterTitle = metaClusterTitle ?? viewerClusterTitle;
const {
data: {clusterData: cluster, groupsStats} = {},
isLoading: infoLoading,
error,
} = clusterApi.useGetClusterInfoQuery(clusterName ?? undefined, {
pollingInterval: autoRefreshInterval,
});
const clusterError = error && typeof error === 'object' ? error : undefined;
const clusterTablets = useTypedSelector((state) =>
selectClusterTabletsWithFqdn(state, clusterName ?? undefined),
);
React.useEffect(() => {
dispatch(setHeaderBreadcrumbs('cluster', {}));
}, [dispatch]);
const getClusterTitle = () => {
if (infoLoading) {
return <Skeleton className={b('title-skeleton')} />;
}
const clusterStatus = cluster?.Overall || EFlag.Grey;
return (
<EntityStatus className={b('title')}>
{clusterTitle}
<EntityStatus.Label
status={clusterStatus}
note={EFlagToDescription[clusterStatus]}
/>
</EntityStatus>
);
};
const activeTab = React.useMemo(
() => clusterTabs.find(({id}) => id === activeTabId),
[activeTabId],
);
return (
<div className={b()} ref={container}>
<Helmet
defaultTitle={`${clusterTitle} — YDB Monitoring`}
titleTemplate={`%s — ${clusterTitle} — YDB Monitoring`}
>
{activeTab ? <title>{activeTab.title}</title> : null}
</Helmet>
<div className={b('header')}>{getClusterTitle()}</div>
<div className={b('sticky-wrapper')}>
<AutoRefreshControl className={b('auto-refresh-control')} />
</div>
{isClusterDashboardAvailable && (
<ClusterOverview
cluster={cluster ?? {}}
groupStats={groupsStats}
loading={infoLoading}
error={clusterError || cluster?.error}
additionalClusterProps={additionalClusterProps}
/>
)}
<div className={b('tabs-sticky-wrapper')}>
<Tabs
size="l"
allowNotSelected={true}
activeTab={activeTabId}
items={clusterTabs}
wrapTo={({id}, node) => {
const path = getClusterPath(id as ClusterTab, {clusterName, backend});
return (
<InternalLink
to={path}
key={id}
onClick={() => {
dispatch(updateDefaultClusterTab(id));
}}
>
{node}
</InternalLink>
);
}}
/>
</div>
<Switch>
<Route
path={
getLocationObjectFromHref(getClusterPath(clusterTabsIds.tablets)).pathname
}
>
<TabletsTable loading={infoLoading} tablets={clusterTablets} />
</Route>
<Route
path={
getLocationObjectFromHref(getClusterPath(clusterTabsIds.tenants)).pathname
}
>
<Tenants additionalTenantsProps={additionalTenantsProps} />
</Route>
<Route
path={getLocationObjectFromHref(getClusterPath(clusterTabsIds.nodes)).pathname}
>
<Nodes parentRef={container} additionalNodesProps={additionalNodesProps} />
</Route>
<Route
path={
getLocationObjectFromHref(getClusterPath(clusterTabsIds.storage)).pathname
}
>
<PaginatedStorage parentRef={container} />
</Route>
<Route
path={
getLocationObjectFromHref(getClusterPath(clusterTabsIds.versions)).pathname
}
>
<VersionsContainer cluster={cluster} loading={infoLoading} />
</Route>
<Route
render={() => (
<Redirect to={getLocationObjectFromHref(getClusterPath(activeTabId))} />
)}
/>
</Switch>
</div>
);
}
function useClusterTab() {
const dispatch = useTypedDispatch();
const defaultTab = useTypedSelector((state) => state.cluster.defaultClusterTab);
const match = useRouteMatch<{activeTab: string}>(routes.cluster);
const {activeTab: activeTabFromParams} = match?.params || {};
let activeTab: ClusterTab;
if (isClusterTab(activeTabFromParams)) {
activeTab = activeTabFromParams;
} else {
activeTab = defaultTab;
}
React.useEffect(() => {
if (activeTab !== defaultTab) {
dispatch(updateDefaultClusterTab(activeTab));
}
}, [activeTab, defaultTab, dispatch]);
return activeTab;
}