|
1 | | -import {useRouteMatch} from 'react-router'; |
| 1 | +import {useEffect, useMemo} from 'react'; |
| 2 | +import {useLocation, useRouteMatch} from 'react-router'; |
| 3 | +import {useDispatch} from 'react-redux'; |
2 | 4 | import cn from 'bem-cn-lite'; |
| 5 | +import qs from 'qs'; |
| 6 | + |
| 7 | +import {Tabs} from '@gravity-ui/uikit'; |
3 | 8 |
|
4 | 9 | import type {AdditionalClusterProps, AdditionalVersionsProps} from '../../types/additionalProps'; |
5 | | -import routes, {CLUSTER_PAGES} from '../../routes'; |
| 10 | +import type {AdditionalNodesInfo} from '../../utils/nodes'; |
| 11 | +import routes from '../../routes'; |
6 | 12 |
|
7 | | -import {ClusterInfo} from './ClusterInfo/ClusterInfo'; |
| 13 | +import {setHeader} from '../../store/reducers/header'; |
| 14 | +import {getClusterInfo} from '../../store/reducers/cluster/cluster'; |
| 15 | +import {getClusterNodes} from '../../store/reducers/clusterNodes/clusterNodes'; |
| 16 | +import {parseNodesToVersionsValues, parseVersionsToVersionToColorMap} from '../../utils/versions'; |
| 17 | +import {useAutofetcher, useTypedSelector} from '../../utils/hooks'; |
| 18 | + |
| 19 | +import {InternalLink} from '../../components/InternalLink'; |
8 | 20 | import Tenants from '../Tenants/Tenants'; |
9 | 21 | import {Nodes} from '../Nodes/Nodes'; |
10 | 22 | import Storage from '../Storage/Storage'; |
| 23 | +import {Versions} from '../Versions/Versions'; |
| 24 | + |
| 25 | +import {ClusterInfo} from './ClusterInfo/ClusterInfo'; |
| 26 | +import {ClusterTab, clusterTabs, clusterTabsIds, getClusterPath} from './utils'; |
11 | 27 |
|
12 | 28 | import './Cluster.scss'; |
13 | 29 |
|
14 | 30 | const b = cn('cluster'); |
15 | 31 |
|
16 | 32 | interface ClusterProps { |
17 | | - additionalTenantsInfo?: any; |
18 | | - additionalNodesInfo?: any; |
| 33 | + additionalTenantsInfo?: unknown; |
| 34 | + additionalNodesInfo?: AdditionalNodesInfo; |
19 | 35 | additionalClusterProps?: AdditionalClusterProps; |
20 | 36 | additionalVersionsProps?: AdditionalVersionsProps; |
21 | 37 | } |
22 | 38 |
|
23 | 39 | function Cluster({ |
| 40 | + additionalClusterProps, |
24 | 41 | additionalTenantsInfo, |
25 | 42 | additionalNodesInfo, |
26 | | - additionalClusterProps, |
27 | 43 | additionalVersionsProps, |
28 | 44 | }: ClusterProps) { |
29 | | - const match = useRouteMatch<{activeTab?: string}>(routes.cluster); |
30 | | - const activeTab = match?.params?.activeTab ?? CLUSTER_PAGES.tenants.id; |
31 | | - const renderRoutes = () => { |
| 45 | + const dispatch = useDispatch(); |
| 46 | + |
| 47 | + const match = useRouteMatch<{activeTab: string}>(routes.cluster); |
| 48 | + const {activeTab = clusterTabsIds.tenants} = match?.params || {}; |
| 49 | + |
| 50 | + const location = useLocation(); |
| 51 | + const queryParams = qs.parse(location.search, { |
| 52 | + ignoreQueryPrefix: true, |
| 53 | + }); |
| 54 | + const {clusterName} = queryParams; |
| 55 | + |
| 56 | + const { |
| 57 | + data: cluster = {}, |
| 58 | + loading: clusterLoading, |
| 59 | + wasLoaded: clusterWasLoaded, |
| 60 | + error: clusterError, |
| 61 | + } = useTypedSelector((state) => state.cluster); |
| 62 | + const { |
| 63 | + nodes, |
| 64 | + loading: nodesLoading, |
| 65 | + wasLoaded: nodesWasLoaded, |
| 66 | + } = useTypedSelector((state) => state.clusterNodes); |
| 67 | + |
| 68 | + const {Name} = cluster; |
| 69 | + |
| 70 | + const infoLoading = (clusterLoading && !clusterWasLoaded) || (nodesLoading && !nodesWasLoaded); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + dispatch(getClusterNodes()); |
| 74 | + }, [dispatch]); |
| 75 | + |
| 76 | + useAutofetcher( |
| 77 | + () => dispatch(getClusterInfo(clusterName ? String(clusterName) : undefined)), |
| 78 | + [dispatch, clusterName], |
| 79 | + true, |
| 80 | + ); |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + dispatch( |
| 84 | + setHeader([ |
| 85 | + { |
| 86 | + text: Name || 'Cluster', |
| 87 | + link: getClusterPath(), |
| 88 | + }, |
| 89 | + ]), |
| 90 | + ); |
| 91 | + }, [dispatch, Name]); |
| 92 | + |
| 93 | + const versionToColor = useMemo(() => { |
| 94 | + if (additionalVersionsProps?.getVersionToColorMap) { |
| 95 | + return additionalVersionsProps?.getVersionToColorMap(); |
| 96 | + } |
| 97 | + return parseVersionsToVersionToColorMap(cluster?.Versions); |
| 98 | + }, [additionalVersionsProps, cluster]); |
| 99 | + |
| 100 | + const versionsValues = useMemo(() => { |
| 101 | + return parseNodesToVersionsValues(nodes, versionToColor); |
| 102 | + }, [nodes, versionToColor]); |
| 103 | + |
| 104 | + const renderTab = () => { |
32 | 105 | switch (activeTab) { |
33 | | - case CLUSTER_PAGES.tenants.id: { |
| 106 | + case clusterTabsIds.tenants: { |
34 | 107 | return <Tenants additionalTenantsInfo={additionalTenantsInfo} />; |
35 | 108 | } |
36 | | - case CLUSTER_PAGES.nodes.id: { |
| 109 | + case clusterTabsIds.nodes: { |
37 | 110 | return <Nodes additionalNodesInfo={additionalNodesInfo} />; |
38 | 111 | } |
39 | | - case CLUSTER_PAGES.storage.id: { |
| 112 | + case clusterTabsIds.storage: { |
40 | 113 | return <Storage additionalNodesInfo={additionalNodesInfo} />; |
41 | 114 | } |
42 | | - case CLUSTER_PAGES.cluster.id: { |
43 | | - return ( |
44 | | - <ClusterInfo |
45 | | - additionalClusterProps={additionalClusterProps} |
46 | | - additionalVersionsProps={additionalVersionsProps} |
47 | | - /> |
48 | | - ); |
| 115 | + case clusterTabsIds.versions: { |
| 116 | + return <Versions versionToColor={versionToColor} />; |
| 117 | + } |
| 118 | + default: { |
| 119 | + return null; |
| 120 | + } |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + const getTabEntityCount = (tabId: ClusterTab) => { |
| 125 | + switch (tabId) { |
| 126 | + case clusterTabsIds.tenants: { |
| 127 | + return cluster?.Tenants; |
| 128 | + } |
| 129 | + case clusterTabsIds.nodes: { |
| 130 | + return cluster?.NodesTotal; |
49 | 131 | } |
50 | 132 | default: { |
51 | 133 | return null; |
52 | 134 | } |
53 | 135 | } |
54 | 136 | }; |
55 | 137 |
|
56 | | - return <div className={b({tab: activeTab})}>{renderRoutes()}</div>; |
| 138 | + return ( |
| 139 | + <div className={b()}> |
| 140 | + <ClusterInfo |
| 141 | + cluster={cluster} |
| 142 | + versionsValues={versionsValues} |
| 143 | + loading={infoLoading} |
| 144 | + error={clusterError} |
| 145 | + additionalClusterProps={additionalClusterProps} |
| 146 | + /> |
| 147 | + |
| 148 | + <div> |
| 149 | + <Tabs |
| 150 | + size="l" |
| 151 | + allowNotSelected={true} |
| 152 | + activeTab={activeTab as string} |
| 153 | + items={clusterTabs.map((item) => { |
| 154 | + return { |
| 155 | + ...item, |
| 156 | + title: `${item.title} ${getTabEntityCount(item.id) || ''}`, |
| 157 | + }; |
| 158 | + })} |
| 159 | + wrapTo={({id}, node) => { |
| 160 | + const path = getClusterPath(id as ClusterTab, queryParams); |
| 161 | + return ( |
| 162 | + <InternalLink to={path} key={id}> |
| 163 | + {node} |
| 164 | + </InternalLink> |
| 165 | + ); |
| 166 | + }} |
| 167 | + /> |
| 168 | + </div> |
| 169 | + |
| 170 | + <div className={b('content')}>{renderTab()}</div> |
| 171 | + </div> |
| 172 | + ); |
57 | 173 | } |
58 | 174 |
|
59 | 175 | export default Cluster; |
0 commit comments