mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 5 of 7 in a stack that splits #52331 into reviewable pieces. Resolves part of [FE-4693](https://linear.app/supabase/issue/FE-4693/pilot-migrate-branches-to-grants-8-endpoints-createbranchmodal). There are no functional changes beyond #52331, and the top of the stack is identical to it. See #52331 for the full rationale. ## Stack 1. #52372 Grant infra for branch variants 2. #52373 Branches list query 3. #52374 Page gates on the list grant 4. #52375 Branch creation 5. 👉 #52376 Branch read, delete, reset, and restore 6. #52377 Branch update and push 7. #52378 Branch diff and merge ## Changes `useBranchQuery` becomes `branchQueryOptions`. The delete, reset, and restore mutations take grants. `DeleteBranchModal`, `DeleteBranchPanel`, and the relevant `Overview` actions use `useBranchGrant`, which picks production for the default branch and development for preview branches. Reset previously had no check. ## Testing New `DeleteBranchPanel` test.
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { toast } from 'sonner'
|
|
import TextConfirmModal from 'ui-patterns/Dialogs/TextConfirmModal'
|
|
|
|
import { useBranchDeleteMutation } from '@/data/branches/branch-delete-mutation'
|
|
import { type Branch } from '@/data/branches/branches-query'
|
|
import { useBranchGrant } from '@/hooks/branches/useBranchGrant'
|
|
|
|
export interface DeleteBranchModalProps {
|
|
open: boolean
|
|
branch?: Branch
|
|
onClose: () => void
|
|
onSuccess?: () => void
|
|
}
|
|
|
|
export const DeleteBranchModal = ({ open, branch, onClose, onSuccess }: DeleteBranchModalProps) => {
|
|
const deleteBranchGrant = useBranchGrant('DELETE /v1/branches/{branch_id_or_ref}', {
|
|
projectRef: branch?.parent_project_ref,
|
|
isDefault: branch?.is_default,
|
|
})
|
|
const { mutate: deleteBranch, isPending: isDeleting } = useBranchDeleteMutation({
|
|
onSuccess: () => {
|
|
toast.success('Successfully deleted branch')
|
|
onSuccess?.()
|
|
onClose()
|
|
},
|
|
})
|
|
|
|
const onConfirmDeleteBranch = () => {
|
|
if (!branch) return console.error('No branch selected')
|
|
|
|
if (deleteBranchGrant.status !== 'granted') {
|
|
return toast.error('You need additional permissions to delete this branch')
|
|
}
|
|
|
|
const { project_ref: branchRef, parent_project_ref: projectRef } = branch
|
|
deleteBranch({ grant: deleteBranchGrant.grant, branchRef, projectRef })
|
|
}
|
|
|
|
return (
|
|
<TextConfirmModal
|
|
variant="destructive"
|
|
visible={open}
|
|
onCancel={() => onClose()}
|
|
onConfirm={() => onConfirmDeleteBranch()}
|
|
loading={isDeleting}
|
|
title="Delete branch"
|
|
confirmLabel="Delete branch"
|
|
confirmPlaceholder="Type in name of branch"
|
|
confirmString={branch?.name ?? ''}
|
|
alert={{
|
|
title: 'You cannot recover this branch once deleted',
|
|
}}
|
|
text={
|
|
<>
|
|
This will delete your database preview branch{' '}
|
|
<span className="text-bold text-foreground">{branch?.name}</span>.
|
|
</>
|
|
}
|
|
/>
|
|
)
|
|
}
|