From 45918e86e2a48986ec74041c859761cc450b47fb Mon Sep 17 00:00:00 2001 From: lemonknowsall Date: Sun, 12 Oct 2025 13:01:18 -0700 Subject: [PATCH] detect lightning payment and toast --- src/components/CashuWallet.tsx | 61 ++++++++++++++++++++++++++++- src/components/ui/qr-modal.tsx | 2 +- src/hooks/cashu/useCashuBalances.ts | 6 --- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/components/CashuWallet.tsx b/src/components/CashuWallet.tsx index 94ee9d3..c987865 100644 --- a/src/components/CashuWallet.tsx +++ b/src/components/CashuWallet.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useCallback } from 'react'; +import { useState, useEffect, useCallback, useRef } from 'react'; import { useCashu, type HistoryEntry } from '@/hooks/useCashu'; import { useCashuMints } from '@/hooks/cashu/useCashuMints'; import { useLightningAddress } from '@/hooks/useLightningAddress'; @@ -35,6 +35,7 @@ export function CashuWallet() { payMeltQuote, redeemMintQuote, addMintQuote, + on, refreshBalances, refreshMints, clearError, @@ -110,6 +111,9 @@ export function CashuWallet() { url: string; name?: string; } | null>(null); + + // Ref to store the mint quote redeemed event unsubscribe function + const mintQuoteUnsubscribeRef = useRef<(() => void) | null>(null); // ============================================================================ // COMPUTED VALUES @@ -220,6 +224,40 @@ export function CashuWallet() { description: "Wallet will automatically claim tokens when payment is detected.", }); setQuoteAmount(''); + + // Clean up any previous listener + if (mintQuoteUnsubscribeRef.current) { + mintQuoteUnsubscribeRef.current(); + mintQuoteUnsubscribeRef.current = null; + } + + // Listen for payment notification via mint-quote:redeemed event (note: hyphenated!) + console.log('๐Ÿ”” Setting up payment listener for quote:', quote.quote); + const quoteId = quote.quote; + const invoiceAmount = amount; + + const unsubscribe = on('mint-quote:redeemed', (payload: unknown) => { + const redeemedQuote = payload as { quoteId?: string }; + console.log('๐Ÿ”” Mint quote redeemed event received:', redeemedQuote); + + // Check if this is the quote we're waiting for + if (redeemedQuote.quoteId === quoteId) { + console.log('โœ… Our quote was redeemed! Payment detected:', redeemedQuote); + toast({ + title: "Payment Received", + description: `Successfully received ${invoiceAmount} sats! Tokens have been added to your wallet.`, + }); + setShowInvoiceModal(false); + setCurrentInvoice(null); + + // Clean up the event listener + mintQuoteUnsubscribeRef.current = null; + unsubscribe(); + } + }); + + // Store unsubscribe function for cleanup + mintQuoteUnsubscribeRef.current = unsubscribe; } catch (err) { const errorMessage = `Failed to create mint quote: ${err instanceof Error ? err.message : 'Unknown error'}`; console.error('โŒ', errorMessage, err); @@ -520,6 +558,17 @@ export function CashuWallet() { } }, [isInitialized, historyEntries.length, loadHistory]); + // Cleanup mint quote listener on component unmount + useEffect(() => { + return () => { + if (mintQuoteUnsubscribeRef.current) { + console.log('๐Ÿงน Cleaning up mint quote listener (component unmount)'); + mintQuoteUnsubscribeRef.current(); + mintQuoteUnsubscribeRef.current = null; + } + }; + }, []); + // ============================================================================ // RENDER - Main UI @@ -636,7 +685,15 @@ export function CashuWallet() { {currentInvoice && ( setShowInvoiceModal(false)} + onClose={() => { + // Clean up event listener when modal is closed manually + if (mintQuoteUnsubscribeRef.current) { + console.log('๐Ÿงน Cleaning up mint quote listener (modal closed)'); + mintQuoteUnsubscribeRef.current(); + mintQuoteUnsubscribeRef.current = null; + } + setShowInvoiceModal(false); + }} title="Lightning Invoice" description={`Pay this invoice to receive ${currentInvoice.amount} sats`} qrCodeUrl={currentInvoice.qrCodeUrl} diff --git a/src/components/ui/qr-modal.tsx b/src/components/ui/qr-modal.tsx index 1406069..56f9b2a 100644 --- a/src/components/ui/qr-modal.tsx +++ b/src/components/ui/qr-modal.tsx @@ -33,7 +33,7 @@ export function QRModal({ return (
void | Promise) => () => void }).on('mintQuote:redeemed', () => { - console.log('๐Ÿ’ฐ Mint quote redeemed, refreshing balances...'); - refreshBalances(); - }); - const unsubscribeSendCreated = (manager as { on: (event: string, handler: (payload: unknown) => void | Promise) => () => void }).on('send:created', () => { console.log('๐Ÿ“ค Send created, refreshing balances...'); // Bypass throttling for send operations to ensure immediate balance update @@ -144,7 +139,6 @@ export function useCashuBalances(manager: unknown | null): UseCashuBalancesRetur return () => { unsubscribeProofsSaved(); unsubscribeProofsStateChanged(); - unsubscribeMintQuoteRedeemed(); unsubscribeSendCreated(); unsubscribeReceiveCreated(); };