1
+ import { useOutletContext , useParams } from '@remix-run/react' ;
1
2
import {
2
3
createContext ,
3
4
useCallback ,
@@ -7,29 +8,35 @@ import {
7
8
useState ,
8
9
} from 'react' ;
9
10
import { ChildrenProps } from '~/components/types' ;
10
- import useDebounce from '~/root/lib/client/hooks/use-debounce' ;
11
11
import { useSocketWatch } from '~/root/lib/client/helpers/socket/useWatch' ;
12
- import { useParams } from '@remix-run/react' ;
12
+ import useDebounce from '~/root/lib/client/hooks/use-debounce' ;
13
+ import { IAccountContext } from '../routes/_main+/$account+/_layout' ;
13
14
import { useConsoleApi } from '../server/gql/api-provider' ;
14
15
15
16
const ctx = createContext < {
16
- clusters : {
17
- [ key : string ] : string ;
18
- } ;
19
- setClusters : React . Dispatch < React . SetStateAction < { [ key : string ] : string } > > ;
17
+ // clusters: {
18
+ // [key: string]: string;
19
+ // };
20
+ // setClusters: React.Dispatch<React.SetStateAction<{ [key: string]: string }>>;
20
21
addToWatchList : ( clusterNames : string [ ] ) => void ;
21
22
removeFromWatchList : ( clusterNames : string [ ] ) => void ;
22
23
} > ( {
23
- clusters : { } ,
24
- setClusters : ( ) => { } ,
24
+ // clusters: {},
25
+ // setClusters: () => {},
25
26
addToWatchList : ( ) => { } ,
26
27
removeFromWatchList : ( ) => { } ,
27
28
} ) ;
28
29
29
- const ClusterStatusProvider = ( { children } : ChildrenProps ) => {
30
- const [ clusters , setClusters ] = useState < {
31
- [ key : string ] : string ;
32
- } > ( { } ) ;
30
+ const ClusterStatusProvider = ( {
31
+ children,
32
+ clustersMap,
33
+ setClustersMap,
34
+ } : ChildrenProps & {
35
+ clustersMap : { [ key : string ] : string } ;
36
+ setClustersMap : React . Dispatch <
37
+ React . SetStateAction < { [ key : string ] : string } >
38
+ > ;
39
+ } ) => {
33
40
const [ watchList , setWatchList ] = useState < {
34
41
[ key : string ] : number ;
35
42
} > ( { } ) ;
@@ -57,47 +64,54 @@ const ClusterStatusProvider = ({ children }: ChildrenProps) => {
57
64
58
65
const caller = ( wl : { [ key : string ] : number } ) => {
59
66
const keys = Object . keys ( wl ) ;
60
- // console.log('nayak', wl, keys, Object.entries(wl));
61
- for ( let i = 0 ; i < keys . length ; i += 1 ) {
62
- ( async ( ) => {
63
- const w = keys [ i ] ;
64
- try {
65
- const { data : cluster } = await api . getClusterStatus ( {
66
- name : w ,
67
- } ) ;
68
- setClusters ( ( s ) => {
69
- return {
70
- ...s ,
71
- [ w ] : cluster . lastOnlineAt ,
72
- } ;
73
- } ) ;
74
- } catch ( e ) {
75
- console . log ( 'error' , e ) ;
76
- }
77
- } ) ( ) ;
78
- }
67
+
68
+ ( async ( ) => {
69
+ try {
70
+ const { data : clustersStatus } = await api . listClusterStatus ( {
71
+ pagination : {
72
+ first : 100 ,
73
+ } ,
74
+ search : {
75
+ allClusters : {
76
+ exact : true ,
77
+ matchType : 'exact' ,
78
+ } ,
79
+ text : {
80
+ array : keys ,
81
+ matchType : 'array' ,
82
+ } ,
83
+ } ,
84
+ } ) ;
85
+
86
+ setClustersMap ( ( s ) => {
87
+ return {
88
+ ...s ,
89
+ ...clustersStatus ,
90
+ } ;
91
+ } ) ;
92
+ } catch ( e ) {
93
+ console . log ( 'error' , e ) ;
94
+ }
95
+ } ) ( ) ;
79
96
} ;
80
97
81
98
useEffect ( ( ) => {
82
- const t2 = setTimeout ( ( ) => {
83
- caller ( watchList ) ;
84
- } , 1000 ) ;
85
-
86
99
const t = setInterval ( ( ) => {
87
100
caller ( watchList ) ;
88
101
} , 30 * 1000 ) ;
89
102
90
103
return ( ) => {
91
- clearTimeout ( t2 ) ;
92
104
clearInterval ( t ) ;
93
105
} ;
94
106
} , [ watchList ] ) ;
95
107
96
108
const { account } = useParams ( ) ;
97
109
98
110
const topic = useCallback ( ( ) => {
99
- return Object . keys ( clusters ) . map ( ( c ) => `account:${ account } .cluster:${ c } ` ) ;
100
- } , [ clusters ] ) ( ) ;
111
+ return Object . keys ( clustersMap ) . map (
112
+ ( c ) => `account:${ account } .cluster:${ c } `
113
+ ) ;
114
+ } , [ clustersMap ] ) ( ) ;
101
115
102
116
useSocketWatch ( ( ) => {
103
117
caller ( watchList ) ;
@@ -128,12 +142,10 @@ const ClusterStatusProvider = ({ children }: ChildrenProps) => {
128
142
< ctx . Provider
129
143
value = { useMemo (
130
144
( ) => ( {
131
- clusters,
132
- setClusters,
133
145
addToWatchList,
134
146
removeFromWatchList,
135
147
} ) ,
136
- [ clusters , setClusters ]
148
+ [ ]
137
149
) }
138
150
>
139
151
{ children }
@@ -150,7 +162,8 @@ export const useClusterStatusV3 = ({
150
162
clusterName ?: string ;
151
163
clusterNames ?: string [ ] ;
152
164
} ) => {
153
- const { clusters, addToWatchList, removeFromWatchList } = useContext ( ctx ) ;
165
+ const { clustersMap } = useOutletContext < IAccountContext > ( ) ;
166
+ const { addToWatchList, removeFromWatchList : _ } = useContext ( ctx ) ;
154
167
useDebounce (
155
168
( ) => {
156
169
if ( ! clusterName && ! clusterNames ) {
@@ -164,18 +177,18 @@ export const useClusterStatusV3 = ({
164
177
}
165
178
166
179
return ( ) => {
167
- if ( clusterName ) {
168
- removeFromWatchList ( [ clusterName ] ) ;
169
- } else if ( clusterNames ) {
170
- removeFromWatchList ( clusterNames ) ;
171
- }
180
+ // if (clusterName) {
181
+ // removeFromWatchList([clusterName]);
182
+ // } else if (clusterNames) {
183
+ // removeFromWatchList(clusterNames);
184
+ // }
172
185
} ;
173
186
} ,
174
187
100 ,
175
188
[ clusterName , clusterNames ]
176
189
) ;
177
190
178
191
return {
179
- clusters ,
192
+ clustersMap ,
180
193
} ;
181
194
} ;
0 commit comments