Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: reset preview branches (#22210)

Alaister Youngbd3807c35630
diff --git a/apps/studio/components/interfaces/BranchManagement/BranchPanels.tsx b/apps/studio/components/interfaces/BranchManagement/BranchPanels.tsx
index 2f8471df8b7..3f0823ad92a 100644
--- a/apps/studio/components/interfaces/BranchManagement/BranchPanels.tsx
+++ b/apps/studio/components/interfaces/BranchManagement/BranchPanels.tsx
@@ -1,12 +1,14 @@
import { useParams } from 'common'
import dayjs from 'dayjs'
-import { GitPullRequest } from 'lucide-react'
+import { GitPullRequest, RefreshCw, Trash2 } from 'lucide-react'
import Link from 'next/link'
-import { PropsWithChildren, ReactNode } from 'react'
+import { PropsWithChildren, ReactNode, useState } from 'react'
+import toast from 'react-hot-toast'
import { useInView } from 'react-intersection-observer'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import { useBranchQuery } from 'data/branches/branch-query'
+import { useBranchResetMutation } from 'data/branches/branch-reset-mutation'
import type { Branch } from 'data/branches/branches-query'
import {
Badge,
@@ -19,8 +21,9 @@ import {
IconExternalLink,
IconMoreVertical,
IconShield,
- IconTrash,
+ Modal,
} from 'ui'
+import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import BranchStatusBadge from './BranchStatusBadge'
interface BranchManagementSectionProps {
@@ -111,6 +114,23 @@ export const BranchRow = ({
}
)
+ const [showConfirmResetModal, setShowConfirmResetModal] = useState(false)
+
+ const { mutate, isLoading: isResetting } = useBranchResetMutation({
+ onSuccess() {
+ toast.success('Success! Please allow a few seconds for the branch to reset.')
+ setShowConfirmResetModal(false)
+ },
+ })
+
+ function onConfirmReset() {
+ if (!projectRef) {
+ throw new Error('Invalid project reference')
+ }
+
+ mutate({ id: branch.id, projectRef })
+ }
+
return (
<div className="w-full flex items-center justify-between px-6 py-2.5" ref={ref}>
<div className="flex items-center gap-x-4">
@@ -204,16 +224,43 @@ export const BranchRow = ({
<Button type="text" icon={<IconMoreVertical />} className="px-1" />
</DropdownMenuTrigger>
<DropdownMenuContent className="p-0 w-56" side="bottom" align="end">
+ <DropdownMenuItem
+ className="gap-x-2"
+ onSelect={() => setShowConfirmResetModal(true)}
+ onClick={() => setShowConfirmResetModal(true)}
+ >
+ <RefreshCw size={14} />
+ Reset Branch
+ </DropdownMenuItem>
<DropdownMenuItem
className="gap-x-2"
onSelect={() => onSelectDeleteBranch?.()}
onClick={() => onSelectDeleteBranch?.()}
>
- <IconTrash size="tiny" />
- <p>Delete branch</p>
+ <Trash2 size={14} />
+ Delete branch
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
+
+ <ConfirmationModal
+ danger
+ visible={showConfirmResetModal}
+ buttonLabel="Reset branch"
+ header="Confirm branch reset"
+ loading={isResetting}
+ onSelectCancel={() => {
+ setShowConfirmResetModal(false)
+ }}
+ onSelectConfirm={onConfirmReset}
+ >
+ <Modal.Content>
+ <p className="py-4 text-sm text-foreground-light">
+ Are you sure you want to reset the "{branch.name}" branch? All data will be
+ deleted.
+ </p>
+ </Modal.Content>
+ </ConfirmationModal>
</div>
)}
</div>
diff --git a/apps/studio/data/branches/branch-reset-mutation.ts b/apps/studio/data/branches/branch-reset-mutation.ts
new file mode 100644
index 00000000000..9637e3347da
--- /dev/null
+++ b/apps/studio/data/branches/branch-reset-mutation.ts
@@ -0,0 +1,51 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
+import { post } from 'data/fetchers'
+import type { ResponseError } from 'types'
+import { branchKeys } from './keys'
+
+export type BranchResetVariables = {
+ id: string
+ projectRef: string
+}
+
+export async function resetBranch({ id }: Pick<BranchResetVariables, 'id'>) {
+ const { data, error } = await post('/v1/branches/{branch_id}/reset', {
+ params: { path: { branch_id: id } },
+ })
+
+ if (error) throw error
+ return data
+}
+
+type BranchResetData = Awaited<ReturnType<typeof resetBranch>>
+
+export const useBranchResetMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<BranchResetData, ResponseError, BranchResetVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+ return useMutation<BranchResetData, ResponseError, BranchResetVariables>(
+ (vars) => resetBranch(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await queryClient.invalidateQueries(branchKeys.list(projectRef))
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to reset branch: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}