@@ -3,6 +3,27 @@ import React, { useEffect, useState } from 'react';
33
44import { useFetch } from 'shared/hooks/BackendAPI/useFetch' ;
55import { ColumnLayoutState } from 'state/columnLayoutAtom' ;
6+ import { resolveType } from './components/ModuleStatus' ;
7+
8+ export const enum ModuleTemplateStatus {
9+ Ready = 'Ready' ,
10+ Processing = 'Processing' ,
11+ Deleting = 'Deleting' ,
12+ Unknown = 'Unknown' ,
13+ Unmanaged = 'Unmanaged' ,
14+ Warning = 'Warning' ,
15+ Error = 'Error' ,
16+ NotInstalled = 'Not installed' ,
17+ }
18+
19+ type ConditionType = {
20+ lastTransitionTime : string ;
21+ lastUpdateTime : string ;
22+ message : string ;
23+ reason : string ;
24+ status : string ;
25+ type : string ;
26+ } ;
627
728export type KymaResourceSpecModuleType = {
829 name : string ;
@@ -105,7 +126,7 @@ export function useGetAllModulesStatuses(modules: any[]) {
105126 const status = ( await response . json ( ) ) ?. status ;
106127 return {
107128 key : resource ?. metadata ?. name ?? resource ?. name ,
108- status : status ?. state || ' Unknown' ,
129+ status : status ?. state || ModuleTemplateStatus . Unknown ,
109130 } ;
110131 } catch ( e ) {
111132 return {
@@ -152,6 +173,14 @@ export const findModuleSpec = (
152173 ) ;
153174} ;
154175
176+ type ModuleManagerType = {
177+ name : string ;
178+ namespace : string ;
179+ group : string ;
180+ version : string ;
181+ kind : string ;
182+ } ;
183+
155184export type ModuleTemplateType = {
156185 metadata : {
157186 name : string ;
@@ -167,6 +196,7 @@ export type ModuleTemplateType = {
167196 info ?: {
168197 documentation ?: string ;
169198 } ;
199+ manager : ModuleManagerType ;
170200 } ;
171201} ;
172202
@@ -244,3 +274,69 @@ export const checkSelectedModule = (
244274 }
245275 return false ;
246276} ;
277+
278+ export function useGetManagerStatus ( manager ?: ModuleManagerType ) {
279+ const fetch = useFetch ( ) ;
280+ const [ data , setData ] = useState < any > ( ModuleTemplateStatus . Unknown ) ;
281+ const [ error , setError ] = useState < Error | null > ( null ) ;
282+
283+ useEffect ( ( ) => {
284+ if ( manager ) {
285+ const path = getResourcePath ( {
286+ apiVersion : `${ manager ?. group } /${ manager ?. version } ` ,
287+ kind : manager ?. kind ,
288+ metadata : {
289+ name : manager ?. name ,
290+ namespace : manager ?. namespace ,
291+ } ,
292+ } as KymaResourceType ) ;
293+ async function fetchResource ( ) {
294+ try {
295+ const response = await fetch ( { relativeUrl : path } ) ;
296+ const status = ( await response . json ( ) ) ?. status ;
297+ const latest = status ?. conditions
298+ ?. filter ( ( condition : ConditionType ) => condition ?. status === 'True' )
299+ ?. reduce (
300+ ( acc : ConditionType , condition : ConditionType ) =>
301+ new Date ( acc ?. lastUpdateTime ) . getTime ( ) >
302+ new Date ( condition ?. lastUpdateTime ) . getTime ( )
303+ ? acc
304+ : condition ,
305+ { } ,
306+ ) ;
307+ if ( latest ?. type ) {
308+ setData ( latest . type ) ;
309+ }
310+ } catch ( error ) {
311+ if ( error instanceof Error ) {
312+ setError ( error ) ;
313+ }
314+ }
315+ }
316+
317+ fetchResource ( ) ;
318+ }
319+ // eslint-disable-next-line react-hooks/exhaustive-deps
320+ } , [ manager ] ) ;
321+
322+ return { data, error } ;
323+ }
324+
325+ export const resolveInstallationStateName = (
326+ state ?: string ,
327+ managerExists ?: boolean ,
328+ managerResourceState ?: string ,
329+ ) => {
330+ if ( state === ModuleTemplateStatus . Unmanaged && ! managerExists ) {
331+ return ModuleTemplateStatus . NotInstalled ;
332+ }
333+
334+ if ( state === ModuleTemplateStatus . Unmanaged && managerExists ) {
335+ if ( resolveType ( state ) !== resolveType ( managerResourceState ?? '' ) ) {
336+ return ModuleTemplateStatus . Processing ;
337+ }
338+ return managerResourceState || ModuleTemplateStatus . Unknown ;
339+ }
340+
341+ return state || ModuleTemplateStatus . Unknown ;
342+ } ;
0 commit comments