RestartProjectDialog.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use client'
  2. import { PermissionAction } from '@supabase/shared-types/out/constants'
  3. import { useRouter } from 'next/router'
  4. import { toast } from 'sonner'
  5. import { ConfirmationModal } from 'ui-patterns/Dialogs/ConfirmationModal'
  6. import { useSetProjectStatus } from '@/data/projects/project-detail-query'
  7. import { useProjectRestartMutation } from '@/data/projects/project-restart-mutation'
  8. import { useProjectRestartServicesMutation } from '@/data/projects/project-restart-services-mutation'
  9. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  10. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  11. import { PROJECT_STATUS } from '@/lib/constants'
  12. interface RestartProjectDialogProps {
  13. visible: boolean
  14. onClose: () => void
  15. /** Restart type: 'project' for full restart, 'database' for fast database reboot */
  16. restartType?: 'project' | 'database'
  17. }
  18. export function RestartProjectDialog({
  19. visible,
  20. onClose,
  21. restartType = 'database',
  22. }: RestartProjectDialogProps) {
  23. const router = useRouter()
  24. const { data: project } = useSelectedProjectQuery()
  25. const { setProjectStatus } = useSetProjectStatus()
  26. const { can: canRestartProject } = useAsyncCheckPermissions(
  27. PermissionAction.INFRA_EXECUTE,
  28. 'reboot'
  29. )
  30. const { mutate: restartProject, isPending: isRestartingProject } = useProjectRestartMutation({
  31. onSuccess: () => {
  32. if (project?.ref) {
  33. setProjectStatus({ ref: project.ref, status: PROJECT_STATUS.RESTARTING })
  34. }
  35. toast.success('Restarting project')
  36. router.push(`/project/${project?.ref}`)
  37. onClose()
  38. },
  39. onError: (error) => {
  40. toast.error(`Unable to restart project: ${error.message}`)
  41. },
  42. })
  43. const { mutate: restartProjectServices, isPending: isRestartingServices } =
  44. useProjectRestartServicesMutation({
  45. onSuccess: () => {
  46. if (project?.ref) {
  47. setProjectStatus({ ref: project.ref, status: PROJECT_STATUS.RESTARTING })
  48. }
  49. toast.success('Restarting database')
  50. router.push(`/project/${project?.ref}`)
  51. onClose()
  52. },
  53. onError: (error) => {
  54. toast.error(`Unable to restart database: ${error.message}`)
  55. },
  56. })
  57. const isLoading = isRestartingProject || isRestartingServices
  58. const handleRestart = () => {
  59. if (!project?.ref) return
  60. if (!canRestartProject) {
  61. return toast.error('You do not have the required permissions to restart this project')
  62. }
  63. if (restartType === 'project') {
  64. restartProject({ ref: project.ref })
  65. } else {
  66. restartProjectServices({
  67. ref: project.ref,
  68. region: project.region,
  69. services: ['postgresql'],
  70. })
  71. }
  72. }
  73. const title = restartType === 'project' ? 'Restart project' : 'Restart database'
  74. const description =
  75. restartType === 'project'
  76. ? 'Are you sure you want to restart your project? There will be a few minutes of downtime.'
  77. : 'Are you sure you want to restart your database? There will be a brief downtime.'
  78. return (
  79. <ConfirmationModal
  80. visible={visible}
  81. variant="warning"
  82. title={title}
  83. description={description}
  84. confirmLabel="Restart"
  85. confirmLabelLoading="Restarting"
  86. loading={isLoading}
  87. disabled={!canRestartProject}
  88. onCancel={onClose}
  89. onConfirm={handleRestart}
  90. />
  91. )
  92. }