mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
545 lines
19 KiB
TypeScript
545 lines
19 KiB
TypeScript
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<void>;
|
||
isRestoring: boolean;
|
||
mints: Array<{ url: string; name?: string }>;
|
||
activeMintUrl: string | null;
|
||
|
||
// QR Scanner Modal
|
||
showQRScanner: boolean;
|
||
setShowQRScanner: (show: boolean) => void;
|
||
stopScanning: () => Promise<void>;
|
||
qrError: string | null;
|
||
onPasteQRCode?: () => Promise<void>;
|
||
}
|
||
|
||
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 && (
|
||
<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>
|
||
)}
|
||
|
||
{/* Consolidation Modal */}
|
||
{showConsolidationModal && (
|
||
<div
|
||
className="fixed inset-0 bg-black/50 flex items-center justify-center z-[100] p-4"
|
||
onClick={() => setShowConsolidationModal(false)}
|
||
>
|
||
<Card
|
||
className="w-full max-w-md border-0 shadow-lg bg-background"
|
||
onClick={(e) => e.stopPropagation()}
|
||
>
|
||
<CardHeader className="relative">
|
||
<Button
|
||
onClick={() => setShowConsolidationModal(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">
|
||
<ArrowRightLeft className="h-5 w-5 mr-2 text-primary" />
|
||
Consolidate UTXOs
|
||
</CardTitle>
|
||
<CardDescription>
|
||
Combine multiple UTXOs into a single transaction
|
||
</CardDescription>
|
||
</CardHeader>
|
||
<CardContent className="space-y-4">
|
||
<div className="bg-muted p-4 rounded-lg border">
|
||
<div className="text-center">
|
||
<div className="text-2xl font-bold text-primary mb-2">
|
||
{utxos.length} UTXO{utxos.length !== 1 ? 's' : ''}
|
||
</div>
|
||
<div className="text-sm text-muted-foreground">
|
||
Total Value: {utxos.reduce((sum, utxo) => sum + utxo.value, 0).toLocaleString()} sats
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="text-sm text-muted-foreground bg-muted/30 p-3 rounded space-y-2">
|
||
<div>🔄 <strong>What this does:</strong> Combines all your UTXOs into a single transaction, sending the total amount (minus fees) back to your own address.</div>
|
||
<div>💰 <strong>Benefits:</strong> Reduces transaction fees for future payments and simplifies your UTXO management.</div>
|
||
<div>⚠️ <strong>Note:</strong> This will create a new transaction on the Bitcoin network and may take some time to confirm.</div>
|
||
</div>
|
||
|
||
<div className="flex justify-center">
|
||
<Button
|
||
onClick={() => {
|
||
onConfirmConsolidation();
|
||
setShowConsolidationModal(false);
|
||
}}
|
||
disabled={isConsolidating || utxos.length <= 1}
|
||
>
|
||
{isConsolidating ? (
|
||
<>
|
||
<RefreshCw className="h-4 w-4 mr-2 animate-spin" />
|
||
Consolidating...
|
||
</>
|
||
) : (
|
||
<>
|
||
<ArrowRightLeft className="h-4 w-4 mr-2" />
|
||
Consolidate UTXOs
|
||
</>
|
||
)}
|
||
</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 Restore 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">Restore from Seed Phrase</h2>
|
||
</div>
|
||
|
||
<p className="text-muted-foreground mb-4">
|
||
Enter your seed phrase to restore tokens from the active mint.
|
||
</p>
|
||
|
||
{activeMintUrl && (
|
||
<div className="bg-muted p-3 rounded-md mb-4">
|
||
<p className="text-sm text-muted-foreground">
|
||
<strong>Active Mint:</strong> {mints.find(m => m.url === activeMintUrl)?.name || new URL(activeMintUrl).hostname}
|
||
</p>
|
||
</div>
|
||
)}
|
||
|
||
<div className="space-y-4">
|
||
<div>
|
||
<Label htmlFor="seed-phrase">Seed Phrase</Label>
|
||
<Input
|
||
id="seed-phrase"
|
||
type="text"
|
||
placeholder="Enter your 12-word seed phrase..."
|
||
value={seedPhraseInput}
|
||
onChange={(e) => setSeedPhraseInput(e.target.value)}
|
||
className="mt-1"
|
||
/>
|
||
</div>
|
||
|
||
<div className="bg-muted p-3 rounded-md">
|
||
<p className="text-xs text-muted-foreground">
|
||
<strong>Note:</strong> This will restore tokens from the provided seed phrase and add them to your current wallet balance.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="mt-6">
|
||
<Button
|
||
onClick={async () => {
|
||
if (!seedPhraseInput.trim()) {
|
||
toast({
|
||
variant: "destructive",
|
||
title: "Missing Information",
|
||
description: "Please enter your seed phrase.",
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (!activeMintUrl) {
|
||
toast({
|
||
variant: "destructive",
|
||
title: "No Active Mint",
|
||
description: "Please select a mint first.",
|
||
});
|
||
return;
|
||
}
|
||
|
||
try {
|
||
await onRestoreFromSeedPhrase(seedPhraseInput.trim(), activeMintUrl);
|
||
setSeedPhraseInput('');
|
||
setShowSeedPhraseInfo(false);
|
||
} catch (error) {
|
||
toast({
|
||
variant: "destructive",
|
||
title: "Restore Failed",
|
||
description: error instanceof Error ? error.message : "Failed to restore from seed phrase",
|
||
});
|
||
}
|
||
}}
|
||
disabled={isRestoring || !seedPhraseInput.trim() || !activeMintUrl}
|
||
className="w-full"
|
||
>
|
||
{isRestoring ? (
|
||
<>
|
||
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||
Restoring...
|
||
</>
|
||
) : (
|
||
'Restore Tokens'
|
||
)}
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* QR Scanner Modal */}
|
||
<Dialog open={showQRScanner} onOpenChange={(open) => {
|
||
if (!open) {
|
||
setShowQRScanner(false);
|
||
stopScanning();
|
||
}
|
||
}}>
|
||
<DialogContent
|
||
className="max-w-none w-screen h-screen !h-[100dvh] p-6 bg-background border-0 rounded-none !top-0 !left-0 !right-0 !bottom-0 !translate-x-0 !translate-y-0 flex flex-col"
|
||
style={{
|
||
position: 'fixed',
|
||
top: 0,
|
||
left: 0,
|
||
right: 0,
|
||
bottom: 0,
|
||
transform: 'none',
|
||
maxWidth: '100vw',
|
||
maxHeight: '100vh',
|
||
}}
|
||
>
|
||
{/* Camera view with rounded corners */}
|
||
<div
|
||
id="qr-reader"
|
||
className="flex-1 rounded-2xl overflow-hidden"
|
||
style={{
|
||
width: '100%',
|
||
height: '100%',
|
||
}}
|
||
/>
|
||
|
||
{/* Error message */}
|
||
{qrError && (
|
||
<div className="absolute bottom-28 left-6 right-6 p-3 bg-destructive text-destructive-foreground rounded-lg text-center z-50">
|
||
{qrError}
|
||
</div>
|
||
)}
|
||
|
||
{/* Paste Button */}
|
||
<div className="mt-4 pb-safe flex justify-center">
|
||
<Button
|
||
onClick={async () => {
|
||
if (onPasteQRCode) {
|
||
await onPasteQRCode();
|
||
}
|
||
}}
|
||
className="rounded-full px-8"
|
||
size="lg"
|
||
>
|
||
Paste
|
||
</Button>
|
||
</div>
|
||
</DialogContent>
|
||
</Dialog>
|
||
</>
|
||
);
|
||
}
|