|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { |
| 5 | + AlertTriangle, |
| 6 | + CheckCircle2, |
| 7 | + ExternalLink, |
| 8 | + Loader2, |
| 9 | +} from 'lucide-react'; |
| 10 | + |
| 11 | +import { |
| 12 | + Dialog, |
| 13 | + DialogContent, |
| 14 | + DialogDescription, |
| 15 | + DialogFooter, |
| 16 | + DialogHeader, |
| 17 | + DialogTitle, |
| 18 | +} from '@/components/ui/dialog'; |
| 19 | +import { Input } from '@/components/ui/input'; |
| 20 | +import { BoundlessButton } from '@/components/buttons'; |
| 21 | +import { |
| 22 | + ESCROW_PHASE_LABEL, |
| 23 | + type BountyOperateOverview, |
| 24 | +} from '@/features/bounties'; |
| 25 | +import { useBountyCancel } from '@/hooks/use-bounty-cancel'; |
| 26 | +import { getTransactionExplorerUrl } from '@/lib/wallet-utils'; |
| 27 | + |
| 28 | +const PHASE_LABEL = { |
| 29 | + ...ESCROW_PHASE_LABEL, |
| 30 | + starting: 'Preparing cancellation…', |
| 31 | + polling: 'Refunding escrow on-chain…', |
| 32 | +}; |
| 33 | + |
| 34 | +/** Lifecycle statuses that mean the bounty can no longer be cancelled. */ |
| 35 | +const TERMINAL_STATUSES = new Set(['completed', 'cancelled']); |
| 36 | + |
| 37 | +const CONFIRM_PHRASE = 'CANCEL'; |
| 38 | + |
| 39 | +export default function BountySettingsPanel({ |
| 40 | + organizationId, |
| 41 | + bountyId, |
| 42 | + overview, |
| 43 | +}: { |
| 44 | + organizationId: string; |
| 45 | + bountyId: string; |
| 46 | + overview: BountyOperateOverview; |
| 47 | +}) { |
| 48 | + const cancelOp = useBountyCancel({ organizationId, bountyId }); |
| 49 | + const [confirmOpen, setConfirmOpen] = useState(false); |
| 50 | + const [confirmText, setConfirmText] = useState(''); |
| 51 | + |
| 52 | + const isCancelled = overview.status === 'cancelled'; |
| 53 | + const isTerminal = TERMINAL_STATUSES.has(overview.status); |
| 54 | + // A draft has no on-chain escrow to refund; cancel only applies once funded. |
| 55 | + const isPublished = !!overview.escrowEventId; |
| 56 | + const running = cancelOp.isRunning; |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className='max-w-2xl space-y-6'> |
| 60 | + <div className='rounded-2xl border border-red-500/30 bg-red-500/[0.04] p-5'> |
| 61 | + <div className='flex items-start gap-3'> |
| 62 | + <AlertTriangle className='mt-0.5 h-5 w-5 shrink-0 text-red-400' /> |
| 63 | + <div className='min-w-0 flex-1'> |
| 64 | + <h3 className='text-sm font-semibold text-white'> |
| 65 | + Cancel this bounty |
| 66 | + </h3> |
| 67 | + <p className='mt-1 text-sm text-zinc-400'> |
| 68 | + Cancelling closes the bounty and refunds the locked escrow. |
| 69 | + Contributors are refunded first, then any residual returns to you |
| 70 | + as the owner. This runs once on-chain and cannot be undone. |
| 71 | + </p> |
| 72 | + |
| 73 | + {cancelOp.isCompleted || isCancelled ? ( |
| 74 | + <div className='mt-4 space-y-2 rounded-xl border border-zinc-800 bg-zinc-900/50 p-4'> |
| 75 | + <div className='flex items-center gap-2 text-sm text-white'> |
| 76 | + <CheckCircle2 className='h-4 w-4 text-emerald-400' /> |
| 77 | + This bounty is cancelled and the escrow was refunded. |
| 78 | + </div> |
| 79 | + {cancelOp.txHash && ( |
| 80 | + <a |
| 81 | + href={getTransactionExplorerUrl(cancelOp.txHash)} |
| 82 | + target='_blank' |
| 83 | + rel='noreferrer' |
| 84 | + className='text-primary inline-flex items-center gap-1 text-xs hover:underline' |
| 85 | + > |
| 86 | + View refund transaction |
| 87 | + <ExternalLink className='h-3 w-3' /> |
| 88 | + </a> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + ) : running ? ( |
| 92 | + <div className='mt-4 flex items-center gap-2 rounded-lg border border-zinc-800 bg-zinc-900/50 p-3 text-sm text-zinc-300'> |
| 93 | + <Loader2 className='text-primary h-4 w-4 animate-spin' /> |
| 94 | + {PHASE_LABEL[cancelOp.phase] || 'Working…'} |
| 95 | + </div> |
| 96 | + ) : isTerminal ? ( |
| 97 | + <p className='mt-4 text-sm text-zinc-500'> |
| 98 | + This bounty is {overview.status.replace(/_/g, ' ')} and can no |
| 99 | + longer be cancelled. |
| 100 | + </p> |
| 101 | + ) : !isPublished ? ( |
| 102 | + <p className='mt-4 text-sm text-zinc-500'> |
| 103 | + This bounty is still a draft with no funded escrow. Delete it |
| 104 | + from your drafts instead. |
| 105 | + </p> |
| 106 | + ) : ( |
| 107 | + <BoundlessButton |
| 108 | + variant='destructive' |
| 109 | + className='mt-4' |
| 110 | + onClick={() => { |
| 111 | + setConfirmText(''); |
| 112 | + setConfirmOpen(true); |
| 113 | + }} |
| 114 | + > |
| 115 | + Cancel bounty & refund |
| 116 | + </BoundlessButton> |
| 117 | + )} |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + |
| 122 | + {/* Explicit-confirm gate before the irreversible on-chain refund. */} |
| 123 | + <Dialog open={confirmOpen} onOpenChange={setConfirmOpen}> |
| 124 | + <DialogContent className='border-zinc-800 bg-zinc-950 text-white'> |
| 125 | + <DialogHeader> |
| 126 | + <DialogTitle>Cancel this bounty?</DialogTitle> |
| 127 | + <DialogDescription> |
| 128 | + The escrow refunds in one on-chain transaction, contributors |
| 129 | + first, then your owner residual. This cannot be undone or |
| 130 | + reversed. Type{' '} |
| 131 | + <span className='font-mono font-semibold text-red-400'> |
| 132 | + {CONFIRM_PHRASE} |
| 133 | + </span>{' '} |
| 134 | + to confirm. |
| 135 | + </DialogDescription> |
| 136 | + </DialogHeader> |
| 137 | + <div className='space-y-3'> |
| 138 | + <div className='rounded-lg border border-zinc-800 bg-zinc-900/40 p-3 text-sm'> |
| 139 | + <div className='flex items-center justify-between'> |
| 140 | + <span className='text-zinc-400'>Reward pool</span> |
| 141 | + <span className='font-semibold text-white'> |
| 142 | + {overview.rewardAmount.toLocaleString()}{' '} |
| 143 | + {overview.rewardCurrency} |
| 144 | + </span> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + <Input |
| 148 | + autoFocus |
| 149 | + value={confirmText} |
| 150 | + onChange={e => setConfirmText(e.target.value)} |
| 151 | + placeholder={`Type ${CONFIRM_PHRASE} to confirm`} |
| 152 | + className='border-zinc-800 bg-zinc-900/50 text-white placeholder:text-zinc-600' |
| 153 | + /> |
| 154 | + </div> |
| 155 | + <DialogFooter> |
| 156 | + <BoundlessButton |
| 157 | + variant='outline' |
| 158 | + onClick={() => setConfirmOpen(false)} |
| 159 | + > |
| 160 | + Keep bounty |
| 161 | + </BoundlessButton> |
| 162 | + <BoundlessButton |
| 163 | + variant='destructive' |
| 164 | + disabled={confirmText.trim() !== CONFIRM_PHRASE} |
| 165 | + onClick={() => { |
| 166 | + setConfirmOpen(false); |
| 167 | + void cancelOp.cancel(); |
| 168 | + }} |
| 169 | + > |
| 170 | + Cancel bounty & refund |
| 171 | + </BoundlessButton> |
| 172 | + </DialogFooter> |
| 173 | + </DialogContent> |
| 174 | + </Dialog> |
| 175 | + </div> |
| 176 | + ); |
| 177 | +} |
0 commit comments