@@ -30,6 +30,8 @@ import ServiceAccount from './serviceAccount';
30
30
import StatefulSet from './statefulSet' ;
31
31
import StorageClass from './storageClass' ;
32
32
33
+ const CLUSTER_FETCH_INTERVAL = 60 * 1000 ; // ms
34
+
33
35
const classList = [
34
36
ClusterRole ,
35
37
ClusterRoleBinding ,
@@ -73,30 +75,48 @@ interface Config {
73
75
}
74
76
75
77
// Hook for getting or fetching the clusters configuration.
76
- export function useClustersConf ( ) {
78
+ export function useClustersConf ( ) : ConfigState [ 'clusters' ] {
77
79
const dispatch = useDispatch ( ) ;
78
80
const clusters = useTypedSelector ( state => state . config . clusters ) ;
81
+ const [ retry , setRetry ] = React . useState ( ! clusters || Object . keys ( clusters ) . length === 0 ) ;
79
82
80
83
React . useEffect ( ( ) => {
81
- if ( Object . keys ( clusters ) . length === 0 ) {
84
+ let retryHandler = 0 ;
85
+
86
+ if ( retry ) {
87
+ setRetry ( false ) ;
88
+
82
89
request ( '/config' , { } , false , false )
83
90
. then ( ( config : Config ) => {
84
- const clusters : ConfigState [ 'clusters' ] = { } ;
91
+ const clustersToConfig : ConfigState [ 'clusters' ] = { } ;
85
92
config ?. clusters . forEach ( ( cluster : Cluster ) => {
86
- clusters [ cluster . name ] = cluster ;
93
+ clustersToConfig [ cluster . name ] = cluster ;
87
94
} ) ;
88
95
89
96
const configToStore = {
90
97
...config ,
91
- clusters
98
+ clusters : clustersToConfig
92
99
} ;
93
- dispatch ( setConfig ( configToStore ) ) ;
100
+
101
+ if ( ! clusters || Object . keys ( clusters ) . length !== 0 ||
102
+ Object . keys ( clustersToConfig ) . length !== 0 ) {
103
+ dispatch ( setConfig ( configToStore ) ) ;
104
+ }
94
105
} )
95
- . catch ( ( err : Error ) => console . error ( err ) ) ;
96
- return ;
106
+ . catch ( ( err : Error ) => {
107
+ console . error ( err ) ;
108
+ retryHandler = window . setInterval ( ( ) => setRetry ( true ) , CLUSTER_FETCH_INTERVAL ) ;
109
+ } ) ;
110
+
111
+ return function cleanup ( ) {
112
+ if ( retryHandler !== 0 ) {
113
+ window . clearInterval ( retryHandler ) ;
114
+ retryHandler = 0 ;
115
+ }
116
+ } ;
97
117
}
98
118
} ,
99
- [ clusters , dispatch ] ) ;
119
+ [ clusters , dispatch , retry ] ) ;
100
120
101
121
return clusters ;
102
122
}
0 commit comments