Files
supabase/apps/studio/components/interfaces/ErrorHandling/RestartProjectDialog.tsx
T
fffb1e23ca feat: implement profile permissions v2 api (#50966)
## Problem

Migrating Studio's permission checks from the old RLS json logic to
openfga backed permissions.

### New hooks and helpers

- `usePermissionsQueryV2` fetches the new profile openfga permisisons 
- `useAsyncCheckPermissionsV2` the replacement for the old check hook;
takes an FGA permission string (or a list)
- `FGA_PERMISSIONS` permission constants derived from
`@supabase/shared-types`. At the moment, the hook also accepts plain
strings for permissions not yet published in the package
- `useAsyncCheckUserContentPermissions` new hook for permission checks
on user content (SQL snippets, reports, log snippets). This wraps
``useAsyncCheckPermissionsV2` and adds existing content level rules on
top of the fga permission check

### Notes

- ~~Need api to support new permissions: project_operations_write,
billing_read/write, audit_logs_read~~ [DONE]
- ~~Still need to support: organization_config_read/write, auth hooks
and users permissions~~ [DONE]

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
2026-10-21 06:54:16 -04:00

104 lines
3.2 KiB
TypeScript

'use client'
import { useRouter } from 'next/router'
import { toast } from 'sonner'
import { ConfirmationModal } from 'ui-patterns/Dialogs/ConfirmationModal'
import { useSetProjectStatus } from '@/data/projects/project-detail-query'
import { useProjectRestartMutation } from '@/data/projects/project-restart-mutation'
import { useProjectRestartServicesMutation } from '@/data/projects/project-restart-services-mutation'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { PROJECT_STATUS } from '@/lib/constants'
interface RestartProjectDialogProps {
visible: boolean
onClose: () => void
/** Restart type: 'project' for full restart, 'database' for fast database reboot */
restartType?: 'project' | 'database'
}
export function RestartProjectDialog({
visible,
onClose,
restartType = 'database',
}: RestartProjectDialogProps) {
const router = useRouter()
const { data: project } = useSelectedProjectQuery()
const { setProjectStatus } = useSetProjectStatus()
const { can: canRestartProject } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.OPERATIONS_WRITE
)
const { mutate: restartProject, isPending: isRestartingProject } = useProjectRestartMutation({
onSuccess: () => {
if (project?.ref) {
setProjectStatus({ ref: project.ref, status: PROJECT_STATUS.RESTARTING })
}
toast.success('Restarting project')
router.push(`/project/${project?.ref}`)
onClose()
},
onError: (error) => {
toast.error(`Unable to restart project: ${error.message}`)
},
})
const { mutate: restartProjectServices, isPending: isRestartingServices } =
useProjectRestartServicesMutation({
onSuccess: () => {
if (project?.ref) {
setProjectStatus({ ref: project.ref, status: PROJECT_STATUS.RESTARTING })
}
toast.success('Restarting database')
router.push(`/project/${project?.ref}`)
onClose()
},
onError: (error) => {
toast.error(`Unable to restart database: ${error.message}`)
},
})
const isLoading = isRestartingProject || isRestartingServices
const handleRestart = () => {
if (!project?.ref) return
if (!canRestartProject) {
return toast.error('You do not have the required permissions to restart this project')
}
if (restartType === 'project') {
restartProject({ ref: project.ref })
} else {
restartProjectServices({
ref: project.ref,
region: project.region,
services: ['postgresql'],
})
}
}
const title = restartType === 'project' ? 'Restart project' : 'Restart database'
const description =
restartType === 'project'
? 'Are you sure you want to restart your project? There will be a few minutes of downtime.'
: 'Are you sure you want to restart your database? There will be a brief downtime.'
return (
<ConfirmationModal
visible={visible}
variant="warning"
title={title}
description={description}
confirmLabel="Restart"
confirmLabelLoading="Restarting"
loading={isLoading}
disabled={!canRestartProject}
onCancel={onClose}
onConfirm={handleRestart}
/>
)
}