import classnames from 'classnames'; import React from 'react'; import { ResourceTask } from './ResourceTask'; import { ConfirmationModalService } from '../../confirmationModal/confirmationModal.service'; import type { IEnvironmentItemProps } from '../environmentBaseElements/EnvironmentItem'; import { EnvironmentItem } from '../environmentBaseElements/EnvironmentItem'; import type { MdResourceActuationState } from '../graphql/graphql-sdk'; import { FetchResourceStatusDocument, useFetchResourceStatusQuery, useToggleResourceManagementMutation, } from '../graphql/graphql-sdk'; import { Icon, Markdown, useApplicationContextSafe } from '../../presentation'; import { showManagedResourceHistoryModal } from '../resourceHistory/ManagedResourceHistoryModal'; import { showResourceDefinitionModal } from '../resources/ResourceDefinitionModal'; import { ResourceTitle } from '../resources/ResourceTitle'; import { ToggleResourceManagement } from '../resources/ToggleResourceManagement'; import { resourceManager } from '../resources/resourceRegistry'; import type { QueryResource } from './types'; import { getIsDebugMode } from '../utils/debugMode'; import { useLogEvent } from '../utils/logging'; import { Spinner } from '../../widgets'; import './Resource.less'; const statusUtils: { [key in Exclude]: { color?: string; icon: string; defaultReason: string; }; } = { ERROR: { color: 'var(--color-status-error)', icon: 'fas fa-times', defaultReason: 'Failed to update resource' }, NOT_MANAGED: { color: 'var(--color-status-warning)', icon: 'fas fa-pause', defaultReason: 'Resource management is paused', }, WAITING: { icon: 'far fa-hourglass', defaultReason: 'Resource is currently locked and can not be updated' }, PROCESSING: { icon: 'far fa-hourglass', defaultReason: 'Resource is being updated' }, DELETING: { icon: 'far fa-trash-alt', defaultReason: 'Resource is being deleted' }, }; interface IStatusProps { appName: string; environmentName: string; resourceId: string; regions: string[]; account?: string; } const Status = ({ appName, environmentName, resourceId, regions, account }: IStatusProps) => { const { data: resourceStatuses, error, loading } = useFetchResourceStatusQuery({ variables: { appName } }); const [enableResourceManagement] = useToggleResourceManagementMutation({ variables: { payload: { id: resourceId, isPaused: false } }, refetchQueries: [{ query: FetchResourceStatusDocument, variables: { appName } }], }); const state = resourceStatuses?.application?.environments .find((env) => env.name === environmentName) ?.state.resources?.find((resource) => resource.id === resourceId)?.state; if (error || (!loading && !state)) { return (
Failed to fetch resource status
); } if (!state) return ; if (state.status === 'UP_TO_DATE') return null; const isNotManaged = state.status === 'NOT_MANAGED'; const reasonElem = (
{state.reason || statusUtils[state.status].defaultReason} {isNotManaged ? ' (click to enable...)' : undefined}
); return (
{isNotManaged ? ( ) : ( reasonElem )} {state.event && state.event !== state.reason && } {Boolean(state.tasks?.length) && (
    {state.tasks?.map(({ id, name }) => ( ))}
)}
); }; interface IBaseResourceProps { resource: QueryResource; environment: string; } const ResourceMetadata = ({ resource }: IBaseResourceProps) => { const logEvent = useLogEvent('Resource'); const isDebug = getIsDebugMode(); const account = resource.location?.account; const regions = resource.location?.regions || []; return (
{regions.map((region, index) => ( {region} {index < regions.length - 1 && ', '} ))} {account && {account}} { e.preventDefault(); showManagedResourceHistoryModal({ id: resource.id, displayName: resource.displayName || resource.id }); logEvent({ action: 'ViewHistory' }); }} > View history {isDebug && ( { e.preventDefault(); showResourceDefinitionModal({ resource: resource }); logEvent({ action: 'ViewDefinition' }); }} > View definition )}
); }; export const Resource = ({ resource, environment, withPadding, size, className, }: IBaseResourceProps & Pick) => { const icon = resourceManager.getIcon(resource.kind); const app = useApplicationContextSafe(); const account = resource.location?.account; const regions = resource.location?.regions || []; return ( } size={size} withPadding={withPadding} >
); };