import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Loader2, AlertTriangle, Clock } from 'lucide-react'; import { useAuth } from '@/contexts/AuthContext'; import { usePezkuwi } from '@/contexts/PezkuwiContext'; import { toast } from 'sonner'; import { acceptFiatOffer, type P2PFiatOffer } from '@shared/lib/p2p-fiat'; interface TradeModalProps { offer: P2PFiatOffer; onClose: () => void; } export function TradeModal({ offer, onClose }: TradeModalProps) { const navigate = useNavigate(); const { user } = useAuth(); const { api, selectedAccount } = usePezkuwi(); const [amount, setAmount] = useState(''); const [loading, setLoading] = useState(false); const cryptoAmount = parseFloat(amount) || 0; const fiatAmount = cryptoAmount * offer.price_per_unit; const isValidAmount = cryptoAmount > 0 && cryptoAmount <= offer.remaining_amount; // Check min/max order amounts const meetsMinOrder = !offer.min_order_amount || cryptoAmount >= offer.min_order_amount; const meetsMaxOrder = !offer.max_order_amount || cryptoAmount <= offer.max_order_amount; const handleInitiateTrade = async () => { if (!api || !selectedAccount || !user) { toast.error('Please connect your wallet and log in'); return; } // Prevent self-trading if (offer.seller_id === user.id) { toast.error('You cannot trade with your own offer'); return; } if (!isValidAmount) { toast.error('Invalid amount'); return; } if (!meetsMinOrder) { toast.error(`Minimum order: ${offer.min_order_amount} ${offer.token}`); return; } if (!meetsMaxOrder) { toast.error(`Maximum order: ${offer.max_order_amount} ${offer.token}`); return; } setLoading(true); try { const tradeId = await acceptFiatOffer({ api, account: selectedAccount, offerId: offer.id, amount: cryptoAmount }); toast.success('Trade initiated! Proceed to payment.'); onClose(); // Navigate to trade page navigate(`/p2p/trade/${tradeId}`); } catch (error) { if (import.meta.env.DEV) console.error('Accept offer error:', error); // Error toast already shown in acceptFiatOffer } finally { setLoading(false); } }; return ( Buy {offer.token} Trading with {offer.seller_wallet.slice(0, 6)}...{offer.seller_wallet.slice(-4)}
{/* Price Info */}
Price {offer.price_per_unit.toFixed(2)} {offer.fiat_currency}
Available {offer.remaining_amount} {offer.token}
{/* Amount Input */}
setAmount(e.target.value)} placeholder="Amount" className="bg-gray-800 border-gray-700 text-white placeholder:text-gray-500 placeholder:opacity-50" /> {offer.min_order_amount && (

Min: {offer.min_order_amount} {offer.token}

)} {offer.max_order_amount && (

Max: {offer.max_order_amount} {offer.token}

)}
{/* Calculation */} {cryptoAmount > 0 && (

You will pay

{fiatAmount.toFixed(2)} {offer.fiat_currency}

)} {/* Warnings */} {!meetsMinOrder && cryptoAmount > 0 && ( Minimum order: {offer.min_order_amount} {offer.token} )} {!meetsMaxOrder && cryptoAmount > 0 && ( Maximum order: {offer.max_order_amount} {offer.token} )} {/* Payment Time Limit */} Payment deadline: {offer.time_limit_minutes} minutes after accepting
); }