import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { X, Coins, AlertTriangle, RefreshCw, Key, ArrowRightLeft, Loader2 } from 'lucide-react'; import { QRModal } from '@/components/ui/qr-modal'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { nip19 } from 'nostr-tools'; import { useToast } from '@/hooks/useToast'; import { UTXO } from '@/lib/bitcoin'; import { useState } from 'react'; // Helper function to prevent complex template literals const getLightningAddress = (userPubkey?: string, npubCashUserInfo?: { name?: string } | null): string => { if (!userPubkey) return ''; const username = npubCashUserInfo?.name || nip19.npubEncode(userPubkey); return `${username}@npubx.cash`; }; interface ProofBreakdown { mintUrl: string; denominations: { [key: number]: number }; totalBalance: number; } interface MintToRemove { url: string; name?: string; } interface WalletModalProps { // Proof Breakdown Modal showProofBreakdown: boolean; setShowProofBreakdown: (show: boolean) => void; proofBreakdown: ProofBreakdown | null; // Remove Mint Modal showRemoveMintModal: boolean; setShowRemoveMintModal: (show: boolean) => void; mintToRemove: MintToRemove | null; onConfirmRemoveMint: () => void; isLoading: boolean; // Consolidation Modal showConsolidationModal: boolean; setShowConsolidationModal: (show: boolean) => void; utxos: UTXO[]; onConfirmConsolidation: () => void; isConsolidating: boolean; // QR Modals showLightningAddressQR: boolean; setShowLightningAddressQR: (show: boolean) => void; lightningAddressQRCode: string; userPubkey?: string; npubCashUserInfo?: { name?: string } | null; // Invoice Modal currentInvoice?: { amount: number; qrCodeUrl: string; invoice: string; expiry: number; } | null; showInvoiceModal: boolean; setShowInvoiceModal: (show: boolean) => void; mintQuoteUnsubscribeRef: React.MutableRefObject<(() => void) | null>; // Sent Token Modal sentToken: string | null; setSentToken: (token: string | null) => void; sentTokenQRCode: string; // Seed Phrase Info Modal showSeedPhraseInfo: boolean; setShowSeedPhraseInfo: (show: boolean) => void; // Restore functionality onRestoreFromSeedPhrase: (seedPhrase: string, mintUrl: string) => Promise; isRestoring: boolean; mints: Array<{ url: string; name?: string }>; activeMintUrl: string | null; // QR Scanner Modal showQRScanner: boolean; setShowQRScanner: (show: boolean) => void; stopScanning: () => Promise; qrError: string | null; onPasteQRCode?: () => Promise; } export function WalletModals({ showProofBreakdown, setShowProofBreakdown, proofBreakdown, showRemoveMintModal, setShowRemoveMintModal, mintToRemove, onConfirmRemoveMint, isLoading, showConsolidationModal, setShowConsolidationModal, utxos, onConfirmConsolidation, isConsolidating, showLightningAddressQR, setShowLightningAddressQR, lightningAddressQRCode, userPubkey, npubCashUserInfo, currentInvoice, showInvoiceModal, setShowInvoiceModal, mintQuoteUnsubscribeRef, sentToken, setSentToken, sentTokenQRCode, showSeedPhraseInfo, setShowSeedPhraseInfo, onRestoreFromSeedPhrase, isRestoring, mints, activeMintUrl, showQRScanner, setShowQRScanner, stopScanning, qrError, onPasteQRCode, }: WalletModalProps) { const { toast } = useToast(); // Restore state const [seedPhraseInput, setSeedPhraseInput] = useState(''); return ( <> {/* Proof Breakdown Modal */} {showProofBreakdown && proofBreakdown && (
setShowProofBreakdown(false)} > e.stopPropagation()} > Token Breakdown {new URL(proofBreakdown.mintUrl).hostname}
{proofBreakdown.totalBalance.toLocaleString()} sats
Total Balance
Denominations:
{Object.entries(proofBreakdown.denominations) .sort(([a], [b]) => parseInt(a) - parseInt(b)) .map(([amount, count]) => (
{parseInt(amount).toLocaleString()} sats
× {count}
))}
💡 Tip: If you can't pay an invoice, try sending tokens to yourself to get different denominations.
)} {/* Remove Mint Modal */} {showRemoveMintModal && mintToRemove && (
Remove Mint Are you sure you want to remove this mint from your wallet?
{mintToRemove.name || new URL(mintToRemove.url).hostname}
⚠️ This will remove the mint and all associated data. Any tokens from this mint will no longer be accessible.
💡 You can always add this mint back later if needed.
)} {/* Consolidation Modal */} {showConsolidationModal && (
setShowConsolidationModal(false)} > e.stopPropagation()} > Consolidate UTXOs Combine multiple UTXOs into a single transaction
{utxos.length} UTXO{utxos.length !== 1 ? 's' : ''}
Total Value: {utxos.reduce((sum, utxo) => sum + utxo.value, 0).toLocaleString()} sats
🔄 What this does: Combines all your UTXOs into a single transaction, sending the total amount (minus fees) back to your own address.
💰 Benefits: Reduces transaction fees for future payments and simplifies your UTXO management.
⚠️ Note: This will create a new transaction on the Bitcoin network and may take some time to confirm.
)} {/* QR Modals */} setShowLightningAddressQR(false)} title="Lightning Address" description="Scan this QR code to send payments to your Lightning address" qrCodeUrl={lightningAddressQRCode} content={getLightningAddress(userPubkey, npubCashUserInfo)} icon="zap" /> {currentInvoice && ( { if (mintQuoteUnsubscribeRef.current) { mintQuoteUnsubscribeRef.current(); mintQuoteUnsubscribeRef.current = null; } setShowInvoiceModal(false); }} onExpiry={() => { if (mintQuoteUnsubscribeRef.current) { mintQuoteUnsubscribeRef.current(); mintQuoteUnsubscribeRef.current = null; } toast({ variant: "destructive", title: "Invoice Expired", description: "The Lightning invoice has expired. Please create a new one.", }); }} title="Lightning Invoice" description={`Pay this invoice to receive ${currentInvoice.amount} sats`} qrCodeUrl={currentInvoice.qrCodeUrl} content={currentInvoice.invoice} icon="zap" expiryTimestamp={currentInvoice.expiry} /> )} {/* Sent Token Modal */} {sentToken && ( setSentToken(null)} title="Cashu Token" description="Share this token with the recipient to send them your sats" qrCodeUrl={sentTokenQRCode} content={sentToken} icon="qr" /> )} {/* Seed Phrase Restore Modal */} {showSeedPhraseInfo && (
setShowSeedPhraseInfo(false)} >
e.stopPropagation()} >

Restore from Seed Phrase

Enter your seed phrase to restore tokens from the active mint.

{activeMintUrl && (

Active Mint: {mints.find(m => m.url === activeMintUrl)?.name || new URL(activeMintUrl).hostname}

)}
setSeedPhraseInput(e.target.value)} className="mt-1" />

Note: This will restore tokens from the provided seed phrase and add them to your current wallet balance.

)} {/* QR Scanner Modal */} { if (!open) { setShowQRScanner(false); stopScanning(); } }}> {/* Camera view with rounded corners */}
{/* Error message */} {qrError && (
{qrError}
)} {/* Paste Button */}
); }