import React , { useEffect } from "react" ; import { Entities } from "../contexts/api" ; import { getDuration , getTestKey } from "../util/helpers" ; import EntityResult from "../components/entity-result" ; import { ErrorNotification } from "../components/notifications" ; import { EntityResultSupportedTypes } from "../contexts/ui" ; import { loadTestResult , loadTaskResult } from "../api/actions" ; import { GetTaskResultCommandResult } from "@garden-io/core/build/src/commands/get/get-task-result" ; import { GetTestResultCommandResult } from "@garden-io/core/build/src/commands/get/get-test-result" ; import { useApi } from "../hooks" ; const ErrorMsg = ( { error , type } ) => ( < ErrorNotification > Error occured while trying to get { type } result : { error . message } < /ErrorNotification>); function prepareData ( data ? ) { if ( ! data ) { return { } ; } const startedAt = data . startedAt ; const completedAt = data . completedAt ; const duration = startedAt && completedAt && getDuration ( startedAt , completedAt ) ; return { duration , startedAt , completedAt , output : data . log , artifacts : data . artifacts , } ; } interface Props { type : EntityResultSupportedTypes ; name : string ; moduleName : string ; cardProps ? : any ; onClose : ( ) => void ; } function isEntityDisabled ( { name , type , moduleName , entities , } ) { if ( type === "test" ) { const testKey = getTestKey ( { moduleName , testName : name } ) ; const test = entities . tests [ testKey ] ; return test . config . disabled || test . config . moduleDisabled ; } else if ( type === "task" || type === "run" ) { const task = entities . tasks [ name ] ; return task . config . disabled || task . config . moduleDisabled ; } else if ( type === "deploy" ) { const service = entities . services [ name ] ; return service . config . disabled || service . config . moduleDisabled ; } else { const module = entities . modules [ name ] ; return module . disabled ; } } /**
 * Returns the InfoPane for a given node type.
 *
 * If the node is of type "test" or "run", it loads the results as well.
 */ export default ( { name , moduleName , type , onClose , cardProps } ) => { const { dispatch , store : { entities , requestStates } , } = useApi ( ) ; const { tasks , tests } = entities ; const disabled = isEntityDisabled ( { name , moduleName , type , entities } ) ; const loadResults = ( ) => { if ( disabled ) { return ; } if ( type === "test" ) { loadTestResult ( { dispatch , name , moduleName } ) ; } else if ( type === "run" || type === "task" ) { loadTaskResult ( { name , dispatch } ) ; } } ; useEffect ( loadResults , [ name , moduleName , disabled ] ) ; if ( disabled ) { return null ; } if ( type === "test" ) { const testKey = getTestKey ( { moduleName , testName : name } ) ; const test = tests [ testKey ] ; if ( requestStates . testResult . error ) { return < ErrorMsg error = { requestStates . testResult . error } type = { type } / > ; } const results = prepareData ( test . result ) ; return ( < EntityResult onRefresh = { loadResults } loading = { requestStates . testResult . pending } onClose = { onClose } name = { name } type = { type } moduleName = { moduleName } cardProps = { cardProps } { ... results } />); } else if ( type === "task" || type === "run" ) { const task = tasks [ name ] ; const taskResult = task . result ; if ( requestStates . taskResult . error ) { return < ErrorMsg error = { requestStates . taskResult . error } type = { type } / > ; } const results = prepareData ( taskResult ) ; return ( < EntityResult onRefresh = { loadResults } loading = { requestStates . taskResult . pending } onClose = { onClose } name = { name } type = { type } moduleName = { moduleName } cardProps = { cardProps } { ... results } />); } else { return < EntityResult onClose = { onClose } name = { name } type = { type } moduleName = { moduleName } / > ; } } ;