import React, { useState } from 'react'; import toast from 'react-hot-toast'; import { Deployment, Domain, Environment, Project } from 'gql-client'; import { Button } from 'components/shared/Button'; import { GlobeIcon, HorizontalDotIcon, LinkIcon, RefreshIcon, RocketIcon, UndoIcon, } from 'components/shared/CustomIcon'; import { Menu, MenuHandler, MenuItem, MenuList, } from '@material-tailwind/react'; import { ComponentPropsWithRef } from 'react'; import ConfirmDialog from '../../../shared/ConfirmDialog'; import AssignDomainDialog from './AssignDomainDialog'; import DeploymentDialogBodyCard from './DeploymentDialogBodyCard'; import { Typography } from '@material-tailwind/react'; import { useGQLClient } from '../../../../context/GQLClientContext'; import { cn } from 'utils/classnames'; interface DeploymentMenuProps extends ComponentPropsWithRef<'div'> { deployment: Deployment; currentDeployment: Deployment; onUpdate: () => Promise; project: Project; prodBranchDomains: Domain[]; } export const DeploymentMenu = ({ deployment, currentDeployment, onUpdate, project, prodBranchDomains, className, ...props }: DeploymentMenuProps) => { const client = useGQLClient(); const [changeToProduction, setChangeToProduction] = useState(false); const [redeployToProduction, setRedeployToProduction] = useState(false); const [rollbackDeployment, setRollbackDeployment] = useState(false); const [assignDomainDialog, setAssignDomainDialog] = useState(false); const updateDeployment = async () => { const isUpdated = await client.updateDeploymentToProd(deployment.id); if (isUpdated) { await onUpdate(); toast.success('Deployment changed to production'); } else { toast.error('Unable to change deployment to production'); } }; const redeployToProd = async () => { const isRedeployed = await client.redeployToProd(deployment.id); if (isRedeployed) { await onUpdate(); toast.success('Redeployed to production'); } else { toast.error('Unable to redeploy to production'); } }; const rollbackDeploymentHandler = async () => { const isRollbacked = await client.rollbackDeployment( project.id, deployment.id, ); if (isRollbacked) { await onUpdate(); toast.success('Deployment rolled back'); } else { toast.error('Unable to rollback deployment'); } }; return ( <>
{/* Dialogs */} setChangeToProduction((preVal) => !preVal)} open={changeToProduction} confirmButtonTitle="Change" color="blue" handleConfirm={async () => { await updateDeployment(); setChangeToProduction((preVal) => !preVal); }} >
Upon confirmation, this deployment will be changed to production. The new deployment will be associated with these domains: {prodBranchDomains.length > 0 && prodBranchDomains.map((value) => { return ( ^ {value.name} ); })}
setRedeployToProduction((preVal) => !preVal)} open={redeployToProduction} confirmButtonTitle="Redeploy" color="blue" handleConfirm={async () => { await redeployToProd(); setRedeployToProduction((preVal) => !preVal); }} >
Upon confirmation, new deployment will be created with the same source code as current deployment. These domains will point to your new deployment: {deployment.domain?.name && ( {deployment.domain?.name} )}
{Boolean(currentDeployment) && ( setRollbackDeployment((preVal) => !preVal)} open={rollbackDeployment} confirmButtonTitle="Rollback" color="blue" handleConfirm={async () => { await rollbackDeploymentHandler(); setRollbackDeployment((preVal) => !preVal); }} >
Upon confirmation, this deployment will replace your current deployment These domains will point to your new deployment: ^ {currentDeployment.domain?.name}
)} setAssignDomainDialog(!assignDomainDialog)} /> ); };