Files
supabase/apps/studio/components/interfaces/BranchManagement/SwitchToPreviewModal.tsx
Ali Waseem 5d3b84945e fix(studio): derive switch-to-preview refs from the branch (FE-4219) (#49320)
"Switch to preview" in the delete flow read its refs from the selected
project, so on the branching overview `parent_project_ref` was undefined
and the handler bailed with a `console.error` — persistent branches
couldn't be deleted.

Both refs now come from the `branch` prop, and the not-ready state shows
on the confirm button instead of the console. Covered by a new MSW test
that fails against the old code.

Fixes FE-4219

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved switching branches to Preview mode by using the selected
branch’s project information.
  * Prevented confirmation when no branch is available.
* Preserved success notifications and modal closing after a successful
switch.

* **Tests**
* Added coverage for successful updates, API failures, error feedback,
request details, and disabled confirmation states.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-20 14:55:38 -04:00

47 lines
1.3 KiB
TypeScript

import { toast } from 'sonner'
import { ConfirmationModal } from 'ui-patterns/Dialogs/ConfirmationModal'
import { useBranchUpdateMutation } from '@/data/branches/branch-update-mutation'
import { type Branch } from '@/data/branches/branches-query'
interface SwitchToPreviewModalProps {
open: boolean
branch?: Branch
onClose: () => void
}
export const SwitchToPreviewModal = ({ open, branch, onClose }: SwitchToPreviewModalProps) => {
const { mutate: updateBranch, isPending: isUpdatingBranch } = useBranchUpdateMutation({
onSuccess() {
toast.success('Successfully updated branch')
onClose()
},
})
const onSwitchToPreview = () => {
if (branch === undefined) return
updateBranch({
branchRef: branch.project_ref,
projectRef: branch.parent_project_ref,
persistent: false,
})
}
return (
<ConfirmationModal
variant="default"
visible={open}
confirmLabel="Switch to preview"
title="Switch branch to preview before deleting"
loading={isUpdatingBranch}
disabled={branch === undefined}
onCancel={() => onClose()}
onConfirm={onSwitchToPreview}
>
<p className="text-sm text-foreground-light">
You must switch the branch "{branch?.name}" to preview before deleting it.
</p>
</ConfirmationModal>
)
}