1
1
import { isEmpty } from 'lodash'
2
- import { ReactElement , useContext , useEffect , useState } from 'react'
2
+ import { ReactElement , useContext , useEffect , useMemo , useState } from 'react'
3
3
4
4
import { Button , Flex , Toast } from '@pluralsh/design-system'
5
5
6
6
import { Outlet } from 'react-router-dom'
7
7
8
8
import ConsoleInstancesContext from 'contexts/ConsoleInstancesContext'
9
9
10
- import { FINISHED_LOCAL_CREATE_KEY } from 'components/create-cluster/CreateClusterActions'
10
+ import {
11
+ FINISHED_CONSOLE_INSTANCE_KEY ,
12
+ FINISHED_LOCAL_CREATE_KEY ,
13
+ } from 'components/create-cluster/CreateClusterActions'
11
14
12
15
import { useTheme } from 'styled-components'
13
16
@@ -19,9 +22,23 @@ import ClustersContext from '../../../contexts/ClustersContext'
19
22
20
23
import OverviewHeader from '../OverviewHeader'
21
24
25
+ import CurrentUserContext from 'contexts/CurrentUserContext'
22
26
import ClusterListEmptyState from './ClusterListEmptyState'
23
27
import ClustersHelpSection from './ClustersHelpSection'
24
28
29
+ import { ConsoleInstanceFragment } from 'generated/graphql'
30
+ import {
31
+ CombinedClusterT ,
32
+ CombinedClusterType ,
33
+ } from './all/AllClustersTableCols'
34
+ import { ClusterListElement , fromClusterList } from './clusterListUtils'
35
+
36
+ export type OverviewContextType = {
37
+ selfHostedClusters : ClusterListElement [ ]
38
+ cloudInstances : ConsoleInstanceFragment [ ]
39
+ combinedClusterList : CombinedClusterT [ ]
40
+ }
41
+
25
42
export const CLUSTERS_ROOT_CRUMB = {
26
43
label : 'clusters' ,
27
44
url : '/overview/clusters' ,
@@ -32,18 +49,58 @@ export const CLUSTERS_OVERVIEW_BREADCRUMBS = [
32
49
]
33
50
34
51
export function Clusters ( ) : ReactElement | null {
35
- const [ showToast , setShowToast ] = useState ( false )
52
+ const { spacing } = useTheme ( )
53
+ const [ showSupportToast , setShowSupportToast ] = useState ( false )
54
+ const [ showPluralCloudToast , setShowPluralCloudToast ] = useState ( false )
55
+
56
+ const me = useContext ( CurrentUserContext )
36
57
const { clusters } = useContext ( ClustersContext )
37
58
const { instances } = useContext ( ConsoleInstancesContext )
38
59
const showEmpty = isEmpty ( clusters ) && isEmpty ( instances )
39
60
61
+ // checks if clusters are still empty after finishing plural cloud setup
40
62
useEffect ( ( ) => {
41
63
if ( localStorage . getItem ( FINISHED_LOCAL_CREATE_KEY ) === 'true' ) {
42
- if ( isEmpty ( clusters ) ) setShowToast ( true )
64
+ if ( isEmpty ( clusters ) ) setShowSupportToast ( true )
43
65
localStorage . removeItem ( FINISHED_LOCAL_CREATE_KEY )
44
66
}
45
67
} , [ clusters ] )
46
68
69
+ // shows a success toast after plural cloud instance is created
70
+ useEffect ( ( ) => {
71
+ const id = localStorage . getItem ( FINISHED_CONSOLE_INSTANCE_KEY )
72
+
73
+ if ( id && instances . some ( ( i ) => i . id === id ) ) {
74
+ localStorage . removeItem ( FINISHED_CONSOLE_INSTANCE_KEY )
75
+ setShowPluralCloudToast ( true )
76
+ }
77
+ } , [ instances ] )
78
+
79
+ const ctx : OverviewContextType = useMemo ( ( ) => {
80
+ const selfHostedClusters = fromClusterList ( clusters , me )
81
+ const cloudInstances = instances
82
+ const combinedClusterList = [
83
+ ...instances . map ( ( instance ) => ( {
84
+ type : CombinedClusterType . PluralCloud as const ,
85
+ id : instance . id ,
86
+ name : instance . name ,
87
+ status : instance . status ,
88
+ owner : instance . owner ,
89
+ consoleUrl : instance . url ,
90
+ } ) ) ,
91
+ ...selfHostedClusters . map ( ( cluster ) => ( {
92
+ type : CombinedClusterType . SelfHosted as const ,
93
+ ...cluster ,
94
+ } ) ) ,
95
+ ]
96
+
97
+ return {
98
+ selfHostedClusters,
99
+ cloudInstances,
100
+ combinedClusterList,
101
+ }
102
+ } , [ clusters , me , instances ] )
103
+
47
104
return (
48
105
< Flex
49
106
direction = "column"
@@ -59,13 +116,22 @@ export function Clusters(): ReactElement | null {
59
116
) : (
60
117
< >
61
118
< OverviewHeader />
62
- < Outlet />
119
+ < Outlet context = { ctx } />
63
120
</ >
64
121
) }
65
122
< ContactSupportToast
66
- open = { showToast }
67
- onClose = { ( ) => setShowToast ( false ) }
123
+ open = { showSupportToast }
124
+ onClose = { ( ) => setShowSupportToast ( false ) }
68
125
/>
126
+ < Toast
127
+ show = { showPluralCloudToast }
128
+ marginBottom = { spacing . xsmall }
129
+ severity = "success"
130
+ position = "bottom"
131
+ onClose = { ( ) => setShowPluralCloudToast ( false ) }
132
+ >
133
+ Your instance was created successfully!
134
+ </ Toast >
69
135
</ Flex >
70
136
)
71
137
}
0 commit comments