-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathContent.tsx
More file actions
277 lines (249 loc) · 8.63 KB
/
Content.tsx
File metadata and controls
277 lines (249 loc) · 8.63 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import React from 'react';
import {connect} from 'react-redux';
import type {RedirectProps} from 'react-router-dom';
import {Redirect, Route, Switch} from 'react-router-dom';
import {AccessDenied} from '../../components/Errors/403';
import {PageError} from '../../components/Errors/PageError/PageError';
import {LoaderWrapper} from '../../components/LoaderWrapper/LoaderWrapper';
import {useSlots} from '../../components/slots';
import type {SlotMap} from '../../components/slots/SlotMap';
import type {SlotComponent} from '../../components/slots/types';
import routes from '../../routes';
import type {RootState} from '../../store';
import {authenticationApi} from '../../store/reducers/authentication/authentication';
import {
useCapabilitiesLoaded,
useCapabilitiesQuery,
useClusterWithoutAuthInUI,
useMetaCapabilitiesLoaded,
useMetaCapabilitiesQuery,
} from '../../store/reducers/capabilities/hooks';
import {nodesListApi} from '../../store/reducers/nodesList';
import {cn} from '../../utils/cn';
import {useDatabaseFromQuery} from '../../utils/hooks/useDatabaseFromQuery';
import {lazyComponent} from '../../utils/lazyComponent';
import Authentication from '../Authentication/Authentication';
import {getClusterPath} from '../Cluster/utils';
import Header from '../Header/Header';
import {
ClusterSlot,
ClustersSlot,
NodeSlot,
PDiskPageSlot,
RedirectSlot,
RoutesSlot,
StorageGroupSlot,
TabletSlot,
TenantSlot,
VDiskPageSlot,
} from './appSlots';
import './App.scss';
const b = cn('app');
type RouteSlot = {
path: string;
slot: SlotComponent<any>;
component: React.ComponentType<any>;
wrapper?: React.ComponentType<any>;
exact?: boolean;
};
const routesSlots: RouteSlot[] = [
{
path: routes.cluster,
slot: ClusterSlot,
component: lazyComponent(() => import('../Cluster/Cluster'), 'Cluster'),
wrapper: DataWrapper,
},
{
path: routes.tenant,
slot: TenantSlot,
component: lazyComponent(() => import('../Tenant/Tenant'), 'Tenant'),
wrapper: DataWrapper,
},
{
path: routes.node,
slot: NodeSlot,
component: lazyComponent(() => import('../Node/Node'), 'Node'),
wrapper: DataWrapper,
},
{
path: routes.pDisk,
slot: PDiskPageSlot,
component: lazyComponent(() => import('../PDiskPage/PDiskPage'), 'PDiskPage'),
wrapper: DataWrapper,
},
{
path: routes.vDisk,
slot: VDiskPageSlot,
component: lazyComponent(() => import('../VDiskPage/VDiskPage'), 'VDiskPage'),
wrapper: DataWrapper,
},
{
path: routes.storageGroup,
slot: StorageGroupSlot,
component: lazyComponent(
() => import('../StorageGroupPage/StorageGroupPage'),
'StorageGroupPage',
),
wrapper: DataWrapper,
},
{
path: routes.tablet,
slot: TabletSlot,
component: lazyComponent(() => import('../Tablet'), 'Tablet'),
wrapper: DataWrapper,
},
];
const Clusters = lazyComponent(() => import('../Clusters/Clusters'), 'Clusters');
function renderRouteSlot(slots: SlotMap, route: RouteSlot) {
return (
<Route
key={route.path}
path={route.path}
exact={route.exact}
render={(props) => {
const slot = slots.get(route.slot);
let content;
if (!slot) {
const Component = route.component;
content = <Component {...props} />;
} else if (typeof slot.rendered === 'function') {
content = slot.rendered({component: route.component, ...props});
} else {
content = slot.rendered;
}
const Wrapper = route.wrapper ?? React.Fragment;
return (
<main className={b('main')}>
<Wrapper>{content}</Wrapper>
</main>
);
}}
/>
);
}
interface ContentProps {
singleClusterMode: boolean;
children: React.ReactNode;
}
export function Content(props: ContentProps) {
const {singleClusterMode} = props;
const slots = useSlots(props);
const additionalRoutes = slots.get(RoutesSlot);
const redirect = slots.get(RedirectSlot);
const redirectProps: RedirectProps =
redirect?.props ?? (singleClusterMode ? {to: getClusterPath()} : {to: routes.clusters});
return (
<Switch>
{additionalRoutes?.rendered}
<Route>
<Header />
<Switch>
{singleClusterMode
? null
: renderRouteSlot(slots, {
path: routes.clusters,
exact: true,
component: Clusters,
slot: ClustersSlot,
wrapper: GetMetaCapabilities,
})}
{/* Single cluster routes */}
{routesSlots.map((route) => {
return renderRouteSlot(slots, route);
})}
<Route
path={redirectProps.from || redirectProps.path}
exact={redirectProps.exact}
strict={redirectProps.strict}
render={() => <Redirect to={redirectProps.to} push={redirectProps.push} />}
/>
</Switch>
</Route>
</Switch>
);
}
function DataWrapper({children}: {children: React.ReactNode}) {
return (
// capabilities is going to work without authentication, but not all running systems are supporting this yet
<GetCapabilities>
<GetUser>
<GetNodesList />
{/* this GetCapabilities will be removed */}
<GetCapabilities>{children}</GetCapabilities>
</GetUser>
</GetCapabilities>
);
}
function GetUser({children}: {children: React.ReactNode}) {
const database = useDatabaseFromQuery();
const {isLoading, error} = authenticationApi.useWhoamiQuery({database});
return (
<LoaderWrapper loading={isLoading} size="l">
<PageError error={error}>{children}</PageError>
</LoaderWrapper>
);
}
function GetNodesList() {
nodesListApi.useGetNodesListQuery(undefined);
return null;
}
function GetCapabilities({children}: {children: React.ReactNode}) {
useCapabilitiesQuery();
const capabilitiesLoaded = useCapabilitiesLoaded();
useMetaCapabilitiesQuery();
// It is always true if there is no meta, since request finishes with an error
const metaCapabilitiesLoaded = useMetaCapabilitiesLoaded();
return (
<LoaderWrapper loading={!capabilitiesLoaded || !metaCapabilitiesLoaded} size="l">
{children}
</LoaderWrapper>
);
}
// Only for Clusters page, there is no need to request cluster capabilities there (GetCapabilities)
// This wrapper is not used in GetCapabilities so the page does not wait for 2 consecutive capabilities requests
function GetMetaCapabilities({children}: {children: React.ReactNode}) {
useMetaCapabilitiesQuery();
// It is always true if there is no meta, since request finishes with an error
const metaCapabilitiesLoaded = useMetaCapabilitiesLoaded();
return (
<LoaderWrapper loading={!metaCapabilitiesLoaded} size="l">
{children}
</LoaderWrapper>
);
}
interface ContentWrapperProps {
singleClusterMode: boolean;
isAuthenticated: boolean;
children: React.ReactNode;
}
function ContentWrapper(props: ContentWrapperProps) {
const {singleClusterMode, isAuthenticated} = props;
const authUnavailable = useClusterWithoutAuthInUI();
const renderNotAuthenticated = () => {
if (authUnavailable) {
return <AccessDenied />;
}
return <Authentication />;
};
return (
<Switch>
{!authUnavailable && (
<Route path={routes.auth}>
<Authentication closable />
</Route>
)}
<Route>
<div className={b({embedded: singleClusterMode})}>
{isAuthenticated ? props.children : renderNotAuthenticated()}
</div>
</Route>
</Switch>
);
}
function mapStateToProps(state: RootState) {
return {
isAuthenticated: state.authentication.isAuthenticated,
singleClusterMode: state.singleClusterMode,
};
}
export default connect(mapStateToProps)(ContentWrapper);