|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { ChipConfirmModal, ChipModalError, ChipModalField } from '@sim/emcn' |
| 5 | +import { createLogger } from '@sim/logger' |
| 6 | +import { sleep } from '@sim/utils/helpers' |
| 7 | +import { formatQuotedNameList, normalizeEmail } from '@sim/utils/string' |
| 8 | +import { signOut } from '@/lib/auth/auth-client' |
| 9 | +import { useAccountDeletionPlan, useDeleteAccount } from '@/hooks/queries/account-deletion' |
| 10 | +import { clearUserData } from '@/stores' |
| 11 | + |
| 12 | +const logger = createLogger('DeleteAccountModal') |
| 13 | + |
| 14 | +/** Matches the naming used in the server's blocker sentences. */ |
| 15 | +const MAX_NAMES_LISTED = 3 |
| 16 | + |
| 17 | +/** How long the post-deletion sign-out and store cleanup may take before the redirect goes anyway. */ |
| 18 | +const SIGN_OUT_TIMEOUT_MS = 3000 |
| 19 | + |
| 20 | +interface DeleteAccountModalProps { |
| 21 | + open: boolean |
| 22 | + onOpenChange: (open: boolean) => void |
| 23 | + /** The signed-in account's email, which must be retyped to confirm. */ |
| 24 | + email: string |
| 25 | +} |
| 26 | + |
| 27 | +function names(workspaces: { name: string }[]): string { |
| 28 | + return formatQuotedNameList( |
| 29 | + workspaces.map((workspace) => workspace.name), |
| 30 | + MAX_NAMES_LISTED |
| 31 | + ) |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Confirms and performs account deletion. |
| 36 | + * |
| 37 | + * The dialog is deliberately explicit rather than alarming: it names every |
| 38 | + * workspace that goes, every workspace that changes hands, and — when the account |
| 39 | + * cannot be deleted yet — exactly what has to happen first. Retyping the account's |
| 40 | + * own email address is the only guard, which is the point: the decision should |
| 41 | + * cost a deliberate action, not a hunt for the right button. |
| 42 | + */ |
| 43 | +export function DeleteAccountModal({ open, onOpenChange, email }: DeleteAccountModalProps) { |
| 44 | + const [confirmEmail, setConfirmEmail] = useState('') |
| 45 | + const { data: plan, isFetching: isPlanFetching, error: planError } = useAccountDeletionPlan(open) |
| 46 | + const deleteAccount = useDeleteAccount() |
| 47 | + |
| 48 | + const blockers = plan?.blockers ?? [] |
| 49 | + const toDelete = plan?.workspacesToDelete ?? [] |
| 50 | + const toTransfer = plan?.workspacesToTransfer ?? [] |
| 51 | + const isBlocked = blockers.length > 0 |
| 52 | + const isConfirmed = normalizeEmail(confirmEmail) === normalizeEmail(email) |
| 53 | + const isPending = deleteAccount.isPending |
| 54 | + |
| 55 | + const close = () => { |
| 56 | + onOpenChange(false) |
| 57 | + setConfirmEmail('') |
| 58 | + deleteAccount.reset() |
| 59 | + } |
| 60 | + |
| 61 | + const handleDelete = () => { |
| 62 | + deleteAccount.mutate( |
| 63 | + { confirmEmail }, |
| 64 | + { |
| 65 | + onSuccess: async () => { |
| 66 | + /** |
| 67 | + * The session row is already gone, so signing out can only fail by |
| 68 | + * telling us so — what matters is that its cookie is dropped and no |
| 69 | + * cached client state survives the redirect. The race bounds that |
| 70 | + * cleanup: the account is deleted either way, so a request left hanging |
| 71 | + * must not strand the user on "Deleting..." forever. The redirect is a |
| 72 | + * full document load, which discards anything the cleanup missed. |
| 73 | + */ |
| 74 | + await Promise.race([ |
| 75 | + Promise.allSettled([signOut(), clearUserData()]), |
| 76 | + sleep(SIGN_OUT_TIMEOUT_MS), |
| 77 | + ]) |
| 78 | + window.location.href = '/login?fromLogout=true' |
| 79 | + }, |
| 80 | + onError: (error) => { |
| 81 | + logger.error('Account deletion failed', { error }) |
| 82 | + }, |
| 83 | + } |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + const errorMessage = |
| 88 | + deleteAccount.error?.message ?? |
| 89 | + (planError ? 'Could not check whether this account can be deleted. Try again.' : null) |
| 90 | + |
| 91 | + return ( |
| 92 | + <ChipConfirmModal |
| 93 | + open={open} |
| 94 | + onOpenChange={(next) => { |
| 95 | + if (!next) close() |
| 96 | + }} |
| 97 | + size='md' |
| 98 | + title='Delete account' |
| 99 | + confirm={{ |
| 100 | + label: 'Delete account', |
| 101 | + pendingLabel: 'Deleting...', |
| 102 | + onClick: handleDelete, |
| 103 | + pending: isPending, |
| 104 | + disabled: isBlocked || isPlanFetching || !isConfirmed || !plan, |
| 105 | + disabledTooltip: isBlocked |
| 106 | + ? 'Resolve the items above first' |
| 107 | + : isConfirmed |
| 108 | + ? undefined |
| 109 | + : 'Enter your account email to confirm', |
| 110 | + }} |
| 111 | + > |
| 112 | + {isBlocked ? ( |
| 113 | + <div className='flex flex-col gap-2 px-2'> |
| 114 | + <p className='text-[var(--text-primary)] text-sm'>Your account can’t be deleted yet:</p> |
| 115 | + <ul className='flex list-disc flex-col gap-1 pl-4'> |
| 116 | + {blockers.map((blocker) => ( |
| 117 | + <li key={blocker.code} className='text-[var(--text-secondary)] text-sm'> |
| 118 | + {blocker.message} |
| 119 | + </li> |
| 120 | + ))} |
| 121 | + </ul> |
| 122 | + </div> |
| 123 | + ) : ( |
| 124 | + <div className='flex flex-col gap-2 px-2'> |
| 125 | + <p className='text-[var(--text-primary)] text-sm'> |
| 126 | + This permanently deletes <span className='font-medium'>{email}</span> along with its |
| 127 | + workflows, chats, files, knowledge bases and credentials.{' '} |
| 128 | + <span className='text-[var(--text-error)]'>This cannot be undone.</span> |
| 129 | + </p> |
| 130 | + {toDelete.length > 0 && ( |
| 131 | + <p className='text-[var(--text-secondary)] text-sm'> |
| 132 | + {toDelete.length === 1 ? 'The workspace ' : 'The workspaces '} |
| 133 | + <span className='text-[var(--text-primary)]'>{names(toDelete)}</span> and everything |
| 134 | + in {toDelete.length === 1 ? 'it' : 'them'} will be deleted. |
| 135 | + </p> |
| 136 | + )} |
| 137 | + {toTransfer.length > 0 && ( |
| 138 | + <p className='text-[var(--text-secondary)] text-sm'> |
| 139 | + Billing for <span className='text-[var(--text-primary)]'>{names(toTransfer)}</span>{' '} |
| 140 | + moves to another admin. Nothing in {toTransfer.length === 1 ? 'it' : 'them'} changes. |
| 141 | + </p> |
| 142 | + )} |
| 143 | + </div> |
| 144 | + )} |
| 145 | + {!isBlocked && ( |
| 146 | + <ChipModalField |
| 147 | + type='email' |
| 148 | + title='Confirm your email' |
| 149 | + value={confirmEmail} |
| 150 | + onChange={setConfirmEmail} |
| 151 | + placeholder={email} |
| 152 | + autoComplete='off' |
| 153 | + disabled={isPending || isPlanFetching} |
| 154 | + required |
| 155 | + /> |
| 156 | + )} |
| 157 | + <ChipModalError>{errorMessage}</ChipModalError> |
| 158 | + </ChipConfirmModal> |
| 159 | + ) |
| 160 | +} |
0 commit comments