import type { FunctionComponent } from 'react' ; import React , { createContext , useContext } from 'react' ; import { IlmPolicyStatusResponse } from '../../common/types' ; import { useCheckIlmPolicyStatus } from './reporting_api_client' ; type UseCheckIlmPolicyStatus = ReturnType < typeof useCheckIlmPolicyStatus > ; interface ContextValue { status : undefined | IlmPolicyStatusResponse [ 'status' ] ; isLoading : UseCheckIlmPolicyStatus [ 'isLoading' ] ; recheckStatus : UseCheckIlmPolicyStatus [ 'resendRequest' ] ; } const IlmPolicyStatusContext = createContext < undefined | ContextValue > ( undefined ) ; export const IlmPolicyStatusContextProvider = ( { children } ) => { const { isLoading , data , resendRequest : recheckStatus } = useCheckIlmPolicyStatus ( ) ; return ( < IlmPolicyStatusContext . Provider value = { { isLoading , status : data ?. status , recheckStatus } } > { children } < /IlmPolicyStatusContext.Provider>); } ; export type UseIlmPolicyStatusReturn = ReturnType < typeof useIlmPolicyStatus > ; export const useIlmPolicyStatus = ( ) : ContextValue => { const ctx = useContext ( IlmPolicyStatusContext ) ; if ( ! ctx ) { throw new Error ( '"useIlmPolicyStatus" can only be used inside of "IlmPolicyStatusContext"' ) ; } return ctx ; } ;