Files
supabase/apps/studio/components/interfaces/Account/Preferences/DeleteAccountButton.tsx
Danny White 16ea380714 fix(studio): reuse the account deletion confirmation pattern (#52350)
## Problem

The account deletion dialog repeats the organisation requirement and
leaves the form area blank while the membership check loads or fails.
Its confirmation form also differs from the other deletion dialogs in
Studio.

## Solution

```text
Request deletion
├─ Checking memberships → loading state
├─ Membership check fails → retry
├─ Organisation memberships found → leave all organisations first
└─ No memberships → shared email confirmation
```

Use `TextConfirmModal` for email validation and the shared copy control.

| Before | After |
| --- | --- |
| <img width="1098" height="576" alt="Image"
src="https://github.com/user-attachments/assets/01371989-b913-4b31-ac28-5da29447cf77"
/> | <img width="838" height="436" alt="Image"
src="https://github.com/user-attachments/assets/b3a2dc4d-eba0-4d2e-a698-f3c9fa14312f"
/> |
| <img width="876" height="674" alt="Image"
src="https://github.com/user-attachments/assets/002c5e0e-b081-40ff-aeba-1bb7a24d18d1"
/> | <img width="854" height="630" alt="Image"
src="https://github.com/user-attachments/assets/806a9e17-753f-4d8d-8100-ce037b84a8ff"
/> |


## Review instructions

1. In Preferences, select **Request deletion**. With organisation
memberships, confirm the dialog says **Leave all organizations first**.
Without memberships, check that the dialog shows your email and the
placeholder **Type your email address in here**. When the copy control
is enabled, select the email and check that it copies the address.
Submit a different email to check the inline validation. [Open
Preferences](https://studio-staging-git-dnywh-fix-account-deletion-copy-supabase.vercel.app/dashboard/account/me)
2. With the copy control enabled, verify that copying the email leaves
the form open and keeps the input unchanged.
2026-11-10 22:53:03 +00:00

172 lines
5.4 KiB
TypeScript

import { SupportCategories } from '@supabase/shared-types/out/constants'
import { LOCAL_STORAGE_KEYS, safeLocalStorage } from 'common'
import { useState } from 'react'
import { toast } from 'sonner'
import {
Button,
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogSection,
DialogTitle,
} from 'ui'
import { NO_PROJECT_MARKER } from '@/components/interfaces/Support/SupportForm.utils'
import { TextConfirmModal } from '@/components/ui/TextConfirmModalWrapper'
import { useSendSupportTicketMutation } from '@/data/feedback/support-ticket-send'
import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
import { useProfile } from '@/lib/profile'
const setDeletionRequestFlag = () => {
const expiryDate = new Date()
expiryDate.setDate(expiryDate.getDate() + 30)
safeLocalStorage.setItem(LOCAL_STORAGE_KEYS.ACCOUNT_DELETION_REQUEST, expiryDate.toString())
}
const hasActiveDeletionRequest = () => {
const expiryDateStr = safeLocalStorage.getItem(LOCAL_STORAGE_KEYS.ACCOUNT_DELETION_REQUEST)
if (!expiryDateStr) return false
const expiryDate = new Date(expiryDateStr)
const now = new Date()
if (now > expiryDate) {
safeLocalStorage.removeItem(LOCAL_STORAGE_KEYS.ACCOUNT_DELETION_REQUEST)
return false
}
return true
}
export const DeleteAccountButton = () => {
const { profile } = useProfile()
const [isOpen, setIsOpen] = useState(false)
const {
data: organizations,
isSuccess,
isPending: isCheckingOrganizations,
isError: hasOrganizationCheckFailed,
isFetching: isFetchingOrganizations,
refetch: refetchOrganizations,
} = useOrganizationsQuery()
const belongsToOrganizations = isSuccess && organizations.length > 0
const canRequestDeletion = isSuccess && organizations.length === 0
const organizationCheckDescription = isCheckingOrganizations
? 'Checking your organization memberships…'
: 'Could not check your organization memberships.'
const accountEmail = profile?.primary_email
const { mutate: submitSupportTicket, isPending } = useSendSupportTicketMutation({
onSuccess: () => {
setIsOpen(false)
setDeletionRequestFlag()
toast.success(
'Successfully submitted account deletion request - we will reach out to you via email once the request is completed!',
{ duration: 8000 }
)
},
onError: (error) => {
toast.error(`Failed to submit account deletion request: ${error}`)
},
})
const handleSubmitDeletionRequest = () => {
if (!canRequestDeletion || !accountEmail || isPending) return
if (hasActiveDeletionRequest()) {
return toast.error('You have already submitted a deletion request within the last 30 days.')
}
const payload = {
subject: 'Account Deletion Request',
message: 'I want to delete my account.',
category: SupportCategories.ACCOUNT_DELETION,
severity: 'Low',
allowSupportAccess: false,
verified: true,
projectRef: NO_PROJECT_MARKER,
}
submitSupportTicket(payload)
}
return (
<>
<Button
variant="danger"
loading={!accountEmail}
aria-haspopup="dialog"
onClick={() => setIsOpen(true)}
>
Request deletion
</Button>
{canRequestDeletion && accountEmail ? (
<TextConfirmModal
visible={isOpen}
size="small"
variant="destructive"
title="Request account deletion"
confirmString={accountEmail}
confirmPlaceholder="Type your email address in here"
confirmLabel="Submit deletion request"
errorMessage="Enter the email address shown above"
input={{ autoFocus: true, disabled: isPending }}
loading={isPending}
onConfirm={handleSubmitDeletionRequest}
onCancel={() => setIsOpen(false)}
>
<DialogDescription className="text-foreground">
Deleting your account is permanent and cannot be undone.
</DialogDescription>
</TextConfirmModal>
) : (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogContent size="small" hideClose={belongsToOrganizations}>
<DialogHeader>
<DialogTitle>
{belongsToOrganizations
? 'Leave all organizations first'
: 'Request account deletion'}
</DialogTitle>
</DialogHeader>
<DialogSection className="border-t">
<DialogDescription className="text-foreground">
{belongsToOrganizations
? 'Leave or delete every organization you belong to before requesting account deletion.'
: organizationCheckDescription}
</DialogDescription>
</DialogSection>
{hasOrganizationCheckFailed && (
<DialogFooter>
<Button
variant="primary"
loading={isFetchingOrganizations}
onClick={() => refetchOrganizations()}
>
Retry
</Button>
</DialogFooter>
)}
{belongsToOrganizations && (
<DialogFooter>
<DialogClose asChild>
<Button variant="default">Close</Button>
</DialogClose>
</DialogFooter>
)}
</DialogContent>
</Dialog>
)}
</>
)
}