@@ -22,13 +22,9 @@ type KymaResourceType = {
2222 } ;
2323} ;
2424
25- export function useModuleStatus ( resource : KymaResourceType ) {
26- const fetch = useFetch ( ) ;
27- const [ data , setData ] = useState < any > ( null ) ;
28- const [ loading , setLoading ] = useState ( true ) ;
29- const [ error , setError ] = useState < Error | null > ( null ) ;
30-
31- const path = resource ?. metadata ?. namespace
25+ const getResourcePath = ( resource : KymaResourceType ) => {
26+ if ( ! resource ) return '' ;
27+ return resource ?. metadata ?. namespace
3228 ? `/apis/${ resource ?. apiVersion } /namespaces/${
3329 resource ?. metadata ?. namespace
3430 } /${ pluralize ( resource ?. kind || '' ) . toLowerCase ( ) } /${
@@ -37,6 +33,15 @@ export function useModuleStatus(resource: KymaResourceType) {
3733 : `/apis/${ resource ?. apiVersion } /${ pluralize (
3834 resource ?. kind || '' ,
3935 ) . toLowerCase ( ) } /${ resource ?. metadata ?. name } `;
36+ } ;
37+
38+ export function useModuleStatus ( resource : KymaResourceType ) {
39+ const fetch = useFetch ( ) ;
40+ const [ data , setData ] = useState < any > ( null ) ;
41+ const [ loading , setLoading ] = useState ( true ) ;
42+ const [ error , setError ] = useState < Error | null > ( null ) ;
43+
44+ const path = getResourcePath ( resource ) ;
4045
4146 useEffect ( ( ) => {
4247 async function fetchModule ( ) {
@@ -61,6 +66,58 @@ export function useModuleStatus(resource: KymaResourceType) {
6166 return { data, loading, error } ;
6267}
6368
69+ export function useGetAllModulesStatuses ( modules : any [ ] ) {
70+ const fetch = useFetch ( ) ;
71+ const [ data , setData ] = useState < Record < string , any > > ( { } ) ;
72+ const [ loading , setLoading ] = useState ( true ) ;
73+ const [ error , setError ] = useState < Error | null > ( null ) ;
74+
75+ useEffect ( ( ) => {
76+ async function fetchModules ( ) {
77+ if ( ! modules || modules . length === 0 ) return ;
78+ setLoading ( true ) ;
79+ try {
80+ const results = await Promise . all (
81+ modules . map ( async module => {
82+ const resource = module ?. resource ?? module ;
83+
84+ if ( ! resource ) return null ;
85+ const path = getResourcePath ( resource ) ;
86+
87+ try {
88+ const response = await fetch ( { relativeUrl : path } ) ;
89+ const status = ( await response . json ( ) ) ?. status ;
90+ return {
91+ key : resource ?. metadata ?. name ?? resource ?. name ,
92+ status : status ?. state || 'Unknown' ,
93+ } ;
94+ } catch ( e ) {
95+ return {
96+ key : resource ?. metadata ?. name ?? resource ?. name ,
97+ status : null ,
98+ error : e ,
99+ } ;
100+ }
101+ } ) ,
102+ ) ;
103+
104+ setData ( results ) ;
105+ } catch ( e ) {
106+ if ( e instanceof Error ) {
107+ setError ( e ) ;
108+ }
109+ } finally {
110+ setLoading ( false ) ;
111+ }
112+ }
113+
114+ fetchModules ( ) ;
115+ // eslint-disable-next-line react-hooks/exhaustive-deps
116+ } , [ JSON . stringify ( modules ) ] ) ;
117+
118+ return { data, loading, error } ;
119+ }
120+
64121export const findModuleStatus = (
65122 kymaResource : KymaResourceType ,
66123 moduleName : string ,
0 commit comments