2025-04-23 08:58:34 +12:00
import { RotateCcw } from 'lucide-react' ;
2025-05-13 22:15:04 +12:00
import { useRouter } from '@uirouter/react' ;
2025-04-23 08:58:34 +12:00
import { EnvironmentId } from '@/react/portainer/environments/types' ;
import { notifySuccess } from '@/portainer/services/notifications' ;
import { LoadingButton } from '@@/buttons' ;
import { buildConfirmButton } from '@@/modals/utils' ;
import { confirm } from '@@/modals/confirm' ;
import { ModalType } from '@@/modals' ;
import { useHelmRollbackMutation } from '../queries/useHelmRollbackMutation' ;
type Props = {
latestRevision : number ;
2025-05-13 22:15:04 +12:00
selectedRevision? : number ;
2025-04-23 08:58:34 +12:00
environmentId : EnvironmentId ;
releaseName : string ;
namespace ? : string ;
} ;
export function RollbackButton ( {
latestRevision ,
2025-05-13 22:15:04 +12:00
selectedRevision ,
2025-04-23 08:58:34 +12:00
environmentId ,
releaseName ,
namespace ,
} : Props ) {
2025-05-13 22:15:04 +12:00
// when the latest revision is selected, rollback to the previous revision
// otherwise, rollback to the selected revision
const rollbackRevision =
selectedRevision === latestRevision ? latestRevision - 1 : selectedRevision ;
const router = useRouter ( ) ;
2025-04-23 08:58:34 +12:00
const rollbackMutation = useHelmRollbackMutation ( environmentId ) ;
return (
< LoadingButton
onClick = { handleClick }
isLoading = { rollbackMutation . isLoading }
loadingText = "Rolling back..."
data - cy = "rollback-button"
icon = { RotateCcw }
color = "default"
size = "medium"
>
2025-05-13 22:15:04 +12:00
Rollback to # { rollbackRevision }
2025-04-23 08:58:34 +12:00
< / LoadingButton >
) ;
async function handleClick() {
const confirmed = await confirm ( {
title : 'Are you sure?' ,
modalType : ModalType.Warn ,
confirmButton : buildConfirmButton ( 'Rollback' ) ,
2025-05-13 22:15:04 +12:00
message : ` Rolling back will restore the application to revision # ${ rollbackRevision } , which could cause service interruption. Do you wish to continue? ` ,
2025-04-23 08:58:34 +12:00
} ) ;
if ( ! confirmed ) {
return ;
}
rollbackMutation . mutate (
{
releaseName ,
2025-05-13 22:15:04 +12:00
params : { namespace , revision : rollbackRevision } ,
2025-04-23 08:58:34 +12:00
} ,
{
onSuccess : ( ) = > {
notifySuccess (
'Success' ,
2025-05-13 22:15:04 +12:00
` Application rolled back to revision # ${ rollbackRevision } successfully. `
2025-04-23 08:58:34 +12:00
) ;
2025-05-13 22:15:04 +12:00
// set the revision url param to undefined to refresh the page at the latest revision
router . stateService . go ( 'kubernetes.helm' , {
namespace ,
name : releaseName ,
revision : undefined ,
} ) ;
2025-04-23 08:58:34 +12:00
} ,
}
) ;
}
}