import { useCallback, useState } from 'react'; import { Deployment, DeploymentStatus, Domain, Environment, Project, } from 'gql-client'; import { Dialog, DialogTitle, DialogContent, DialogActions, Tooltip, } from '@mui/material'; import { Avatar } from 'components/shared/Avatar'; import { BranchStrokeIcon, CheckRoundFilledIcon, ClockOutlineIcon, CommitIcon, LoadingIcon, WarningIcon, } from 'components/shared/CustomIcon'; import { Heading } from 'components/shared/Heading'; import { OverflownText } from 'components/shared/OverflownText'; import { Tag, TagTheme } from 'components/shared/Tag'; import { Button } from 'components/shared/Button'; import { getInitials } from 'utils/geInitials'; import { relativeTimeMs } from 'utils/time'; import { SHORT_COMMIT_HASH_LENGTH } from '../../../../constants'; import { formatAddress } from '../../../../utils/format'; import { DeploymentMenu } from './DeploymentMenu'; const DEPLOYMENT_LOGS_STYLE = { backgroundColor: 'rgba(0,0,0, .9)', padding: '2em', borderRadius: '0.5em', marginLeft: '0.5em', marginRight: '0.5em', color: 'gray', fontSize: 'small', }; interface DeployDetailsCardProps { deployment: Deployment; currentDeployment: Deployment; onUpdate: () => Promise; project: Project; prodBranchDomains: Domain[]; } const STATUS_COLORS: { [key in DeploymentStatus]: TagTheme['type']; } = { [DeploymentStatus.Building]: 'emphasized', [DeploymentStatus.Ready]: 'positive', [DeploymentStatus.Error]: 'negative', [DeploymentStatus.Deleting]: 'neutral', }; const DeploymentDetailsCard = ({ deployment, currentDeployment, onUpdate, project, prodBranchDomains, }: DeployDetailsCardProps) => { const [openDialog, setOpenDialog] = useState(false); const [deploymentLogs, setDeploymentLogs] = useState( 'No deployment logs available', ); const handleOpenDialog = () => setOpenDialog(true); const handleCloseDialog = () => setOpenDialog(false); const getIconByDeploymentStatus = (status: DeploymentStatus) => { if ( status === DeploymentStatus.Building || status === DeploymentStatus.Deleting ) { return ; } if (status === DeploymentStatus.Ready) { return ; } if (status === DeploymentStatus.Error) { return ; } }; const fetchDeploymentLogs = async () => { setDeploymentLogs('Loading logs...'); handleOpenDialog(); const statusUrl = `${deployment.deployer.deployerApiUrl}/${deployment.applicationDeploymentRequestId}`; const statusRes = await fetch(statusUrl, { cache: 'no-store' }).then( (res) => res.json(), ); if (!statusRes.logAvailable) { setDeploymentLogs(statusRes.lastState); } else { const logsUrl = `${deployment.deployer.deployerApiUrl}/log/${deployment.applicationDeploymentRequestId}`; const logsRes = await fetch(logsUrl, { cache: 'no-store' }).then((res) => res.text(), ); setDeploymentLogs(logsRes); } }; const renderDeploymentStatus = useCallback( (className?: string) => { return (
{deployment.status}
); }, [deployment.status, deployment.commitHash], ); return (
{/* DEPLOYMENT URL */} {deployment.url && ( {deployment.url} )} {deployment.deployer.deployerLrn && ( Deployer LRN: {deployment.deployer.deployerLrn} )} {deployment.environment === Environment.Production ? `Production ${deployment.isCurrent ? '(Current)' : ''}` : 'Preview'}
{/* DEPLOYMENT STATUS */} {renderDeploymentStatus('w-[10%] max-w-[110px] hidden md:flex h-fit')} {/* DEPLOYMENT COMMIT DETAILS */}
{deployment.branch} {deployment.commitHash.substring(0, SHORT_COMMIT_HASH_LENGTH)}{' '} {deployment.commitMessage}
{renderDeploymentStatus('flex md:hidden h-fit')}
{/* DEPLOYMENT INFOs */}
{relativeTimeMs(deployment.createdAt)}
{formatAddress(deployment.createdBy.name ?? '')}
Deployment logs {deploymentLogs &&
{deploymentLogs}
}
); }; export default DeploymentDetailsCard;