infinite loop initialization, wallet endpoint structure, zapdialog debug

This commit is contained in:
lemonknowsall committed 2025-10-06 17:27:30 -07:00
1 parent 4672413b04
commit e14d4e887f
6 files changed
+284 -23

No files matched your search

+65 -2
View File
@@ -9,6 +9,7 @@ import { Alert, AlertDescription } from '@/components/ui/alert';
import { Badge } from '@/components/ui/badge';
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger} from '@/components/ui/dropdown-menu';
import { Loader2, Wallet, Plus, Send, Download, Bitcoin, RefreshCw, Copy, Check, ChevronDown, History, ArrowUpRight, ArrowDownLeft, Zap, X, Coins, ChevronUp } from 'lucide-react';
import QRCode from 'qrcode';
export function CashuWallet() {
// ============================================================================
@@ -71,6 +72,7 @@ export function CashuWallet() {
amount: number;
invoice: string;
timestamp: number;
qrCodeUrl?: string;
}>>('cashu-pending-quotes', []);
// ============================================================================
@@ -80,6 +82,7 @@ export function CashuWallet() {
const [copiedToken, setCopiedToken] = useState(false);
const [copiedInvoice, setCopiedInvoice] = useState<string | null>(null);
// ============================================================================
// STATE - History
// ============================================================================
@@ -127,6 +130,27 @@ export function CashuWallet() {
}
};
// ============================================================================
// HELPER FUNCTIONS - QR Code
// ============================================================================
const generateQRCodeForQuote = async (invoice: string): Promise<string> => {
try {
return await QRCode.toDataURL(invoice, {
width: 256,
margin: 2,
color: {
dark: '#000000',
light: '#FFFFFF',
},
});
} catch (err) {
console.error('Failed to generate QR code:', err);
return '';
}
};
// ============================================================================
// HELPER FUNCTIONS - History
// ============================================================================
@@ -218,13 +242,17 @@ export function CashuWallet() {
}
const quote = await createMintQuote(activeMintUrl, amount);
// Save the pending quote
// Generate QR code for the Lightning invoice
const qrCodeUrl = await generateQRCodeForQuote(quote.request);
// Save the pending quote with QR code
setPendingQuotes(prev => [...prev, {
quoteId: quote.quote,
mintUrl: activeMintUrl,
amount,
invoice: quote.request,
timestamp: Date.now()
timestamp: Date.now(),
qrCodeUrl
}]);
const message = `Mint quote created! Request: ${quote.request}`;
@@ -479,6 +507,28 @@ export function CashuWallet() {
}
}, [isInitialized, historyEntries.length, loadHistory]);
// Generate QR codes for existing pending quotes that don't have them
useEffect(() => {
const generateMissingQRCodes = async () => {
const quotesNeedingQR = pendingQuotes.filter(quote => !quote.qrCodeUrl);
if (quotesNeedingQR.length === 0) return;
const updatedQuotes = await Promise.all(
pendingQuotes.map(async (quote) => {
if (!quote.qrCodeUrl) {
const qrCodeUrl = await generateQRCodeForQuote(quote.invoice);
return { ...quote, qrCodeUrl };
}
return quote;
})
);
setPendingQuotes(updatedQuotes);
};
generateMissingQRCodes();
}, []); // Only run once on mount
// ============================================================================
// RENDER - Loading State
// ============================================================================
@@ -944,6 +994,19 @@ export function CashuWallet() {
Check Payment
</Button>
</div>
{/* QR Code */}
{quote.qrCodeUrl && (
<div className="flex justify-center mb-3">
<div className="bg-white dark:bg-gray-100 p-3 rounded-lg border shadow-sm">
<img
src={quote.qrCodeUrl}
alt="Lightning Invoice QR Code"
className="w-28 h-28 sm:w-32 sm:h-32 object-contain"
/>
</div>
</div>
)}
<div className="relative">
<div className="bg-muted p-4 rounded-lg border font-mono text-sm">
{quote.invoice.length > 20 ? `${quote.invoice.substring(0, 20)}...` : quote.invoice}
+11
View File
@@ -389,6 +389,17 @@ export function ZapDialog({ target, children, className }: ZapDialogProps) {
const handleZap = () => {
const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount;
// Validate the amount
if (isNaN(finalAmount) || finalAmount <= 0) {
toast({
title: 'Invalid amount',
description: 'Please enter a valid amount greater than 0.',
variant: 'destructive',
});
return;
}
if (canPayWithCashu && finalAmount > cashuBalance) {
toast({
title: 'Insufficient Cashu balance',