Files
mkstack/src/components/wallet/WalletModals.tsx
T

336 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { X, Coins, AlertTriangle, RefreshCw, Key } from 'lucide-react';
import { QRModal } from '@/components/ui/qr-modal';
import { nip19 } from 'nostr-tools';
import { useToast } from '@/hooks/useToast';
// 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;
// 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;
}
export function WalletModals({
showProofBreakdown,
setShowProofBreakdown,
proofBreakdown,
showRemoveMintModal,
setShowRemoveMintModal,
mintToRemove,
onConfirmRemoveMint,
isLoading,
showLightningAddressQR,
setShowLightningAddressQR,
lightningAddressQRCode,
userPubkey,
npubCashUserInfo,
currentInvoice,
showInvoiceModal,
setShowInvoiceModal,
mintQuoteUnsubscribeRef,
sentToken,
setSentToken,
sentTokenQRCode,
showSeedPhraseInfo,
setShowSeedPhraseInfo,
}: WalletModalProps) {
const { toast } = useToast();
return (
<>
{/* Proof Breakdown Modal */}
{showProofBreakdown && proofBreakdown && (
<div
className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
onClick={() => setShowProofBreakdown(false)}
>
<Card
className="w-full max-w-md border-0 shadow-lg bg-background"
onClick={(e) => e.stopPropagation()}
>
<CardHeader className="relative">
<Button
onClick={() => setShowProofBreakdown(false)}
className="absolute top-2 right-2"
size="sm"
variant="ghost"
>
<X className="h-4 w-4" />
</Button>
<CardTitle className="flex items-center pr-8">
<Coins className="h-5 w-5 mr-2" />
Token Breakdown
</CardTitle>
<CardDescription>
{new URL(proofBreakdown.mintUrl).hostname}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="text-center py-2">
<div className="text-2xl font-bold text-primary">
{proofBreakdown.totalBalance.toLocaleString()} sats
</div>
<div className="text-sm text-muted-foreground">Total Balance</div>
</div>
<div className="space-y-2">
<div className="text-sm font-medium text-muted-foreground">Denominations:</div>
{Object.entries(proofBreakdown.denominations)
.sort(([a], [b]) => parseInt(a) - parseInt(b))
.map(([amount, count]) => (
<div key={amount} className="flex items-center justify-between p-2 bg-muted/30 rounded">
<div className="flex items-center gap-2">
<Coins className="h-4 w-4 text-muted-foreground" />
<span className="font-medium">{parseInt(amount).toLocaleString()} sats</span>
</div>
<Badge variant="secondary" className="bg-primary/10 text-primary">
× {count}
</Badge>
</div>
))}
</div>
<div className="text-xs text-muted-foreground bg-muted/30 p-3 rounded">
💡 Tip: If you can't pay an invoice, try sending tokens to yourself to get different denominations.
</div>
</CardContent>
</Card>
</div>
)}
{/* Remove Mint Modal */}
{showRemoveMintModal && mintToRemove && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-[100] p-4">
<Card className="w-full max-w-md border-0 shadow-lg bg-background">
<CardHeader className="relative">
<Button
onClick={() => setShowRemoveMintModal(false)}
className="absolute top-2 right-2"
size="sm"
variant="ghost"
>
<X className="h-4 w-4" />
</Button>
<CardTitle className="flex items-center pr-8">
<AlertTriangle className="h-5 w-5 mr-2 text-destructive" />
Remove Mint
</CardTitle>
<CardDescription>
Are you sure you want to remove this mint from your wallet?
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="bg-muted p-4 rounded-lg border">
<div className="font-medium">{mintToRemove.name || new URL(mintToRemove.url).hostname}</div>
</div>
<div className="text-sm text-muted-foreground bg-muted/30 p-3 rounded space-y-2">
<div>⚠️ This will remove the mint and all associated data. Any tokens from this mint will no longer be accessible.</div>
<div>💡 You can always add this mint back later if needed.</div>
</div>
<div className="flex gap-2 justify-end">
<Button
variant="outline"
onClick={() => setShowRemoveMintModal(false)}
>
Cancel
</Button>
<Button
variant="destructive"
onClick={onConfirmRemoveMint}
disabled={isLoading}
>
{isLoading ? (
<>
<RefreshCw className="h-4 w-4 mr-2 animate-spin" />
Removing...
</>
) : (
<>
<X className="h-4 w-4 mr-2" />
Remove Mint
</>
)}
</Button>
</div>
</CardContent>
</Card>
</div>
)}
{/* QR Modals */}
<QRModal
isOpen={showLightningAddressQR}
onClose={() => 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 && (
<QRModal
isOpen={showInvoiceModal}
onClose={() => {
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 && (
<QRModal
isOpen={!!sentToken}
onClose={() => 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 Info Modal */}
{showSeedPhraseInfo && (
<div
className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"
onClick={() => setShowSeedPhraseInfo(false)}
>
<div
className="w-full max-w-md border-0 shadow-lg bg-background rounded-lg"
onClick={(e) => e.stopPropagation()}
>
<div className="relative p-6">
<button
onClick={() => setShowSeedPhraseInfo(false)}
className="absolute top-4 right-4 p-2 hover:bg-muted rounded-md transition-colors"
>
<X className="h-4 w-4" />
</button>
<div className="flex items-center mb-4 pr-8">
<Key className="h-5 w-5 mr-2" />
<h2 className="text-lg font-semibold">What is a Seed Phrase?</h2>
</div>
<p className="text-muted-foreground mb-6">
Your seed phrase is a secure way to backup and restore your wallet
</p>
<div className="space-y-4 text-sm">
<div>
<h4 className="font-semibold mb-2">🔐 Recovery Key</h4>
<p className="text-muted-foreground">
Your seed phrase is a 12-word recovery key that allows you to restore your wallet
on any device. It's generated using industry-standard BIP39 wordlists.
</p>
</div>
<div>
<h4 className="font-semibold mb-2">💾 Backup Important</h4>
<p className="text-muted-foreground">
Write down your seed phrase and store it securely offline. Anyone with access
to your seed phrase can control your wallet and funds.
</p>
</div>
<div>
<h4 className="font-semibold mb-2">🛡️ Security Tips</h4>
<ul className="text-muted-foreground space-y-1 ml-4">
<li>• Never share your seed phrase with anyone</li>
<li>• Store it offline in a secure location</li>
<li>• Don't take screenshots or store it digitally</li>
<li>• Use it only to restore your wallet</li>
</ul>
</div>
<div className="bg-muted p-3 rounded-md">
<p className="text-xs text-muted-foreground">
<strong>Note:</strong> The seed phrase shown above is masked for privacy.
Click the copy button to access the actual phrase for backup purposes.
</p>
</div>
</div>
</div>
</div>
</div>
)}
</>
);
}