From 7ba3d7f8c5972315e4276a115117b50c7e484013 Mon Sep 17 00:00:00 2001 From: lemonknowsall Date: Sun, 12 Oct 2025 13:28:11 -0700 Subject: [PATCH] added expiry timer --- src/components/CashuWallet.tsx | 19 +++++++++++- src/components/ui/qr-modal.tsx | 53 +++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/components/CashuWallet.tsx b/src/components/CashuWallet.tsx index c987865..2751be3 100644 --- a/src/components/CashuWallet.tsx +++ b/src/components/CashuWallet.tsx @@ -104,6 +104,7 @@ export function CashuWallet() { amount: number; invoice: string; qrCodeUrl: string; + expiry: number; } | null>(null); const [showRemoveMintModal, setShowRemoveMintModal] = useState(false); @@ -212,7 +213,8 @@ export function CashuWallet() { mintUrl: activeMintUrl, amount, invoice: quote.request, - qrCodeUrl + qrCodeUrl, + expiry: quote.expiry }); setShowInvoiceModal(true); @@ -694,11 +696,26 @@ export function CashuWallet() { } setShowInvoiceModal(false); }} + onExpiry={() => { + // Clean up event listener when invoice expires + if (mintQuoteUnsubscribeRef.current) { + console.log('🧹 Cleaning up mint quote listener (invoice expired)'); + mintQuoteUnsubscribeRef.current(); + mintQuoteUnsubscribeRef.current = null; + } + // Show destructive toast + 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} /> )} diff --git a/src/components/ui/qr-modal.tsx b/src/components/ui/qr-modal.tsx index 56f9b2a..a6c1600 100644 --- a/src/components/ui/qr-modal.tsx +++ b/src/components/ui/qr-modal.tsx @@ -1,7 +1,8 @@ import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; -import { X, Copy, Check, QrCode, Zap } from 'lucide-react'; +import { X, Copy, Check, QrCode, Zap, Clock } from 'lucide-react'; import { useCopyToClipboard } from '@/hooks/useCopyToClipboard'; +import { useEffect, useState } from 'react'; interface QRModalProps { isOpen: boolean; @@ -11,6 +12,8 @@ interface QRModalProps { qrCodeUrl: string; content: string; icon?: 'qr' | 'zap'; + expiryTimestamp?: number; + onExpiry?: () => void; } export function QRModal({ @@ -21,12 +24,52 @@ export function QRModal({ qrCodeUrl, content, icon = 'qr', + expiryTimestamp, + onExpiry, }: QRModalProps) { const { copy, copied } = useCopyToClipboard(); + const [timeRemaining, setTimeRemaining] = useState(null); const handleCopy = () => { copy(content); }; + + // Countdown timer effect + useEffect(() => { + if (!isOpen || !expiryTimestamp) return; + + const updateTimer = () => { + const now = Math.floor(Date.now() / 1000); // Current time in seconds + const remaining = expiryTimestamp - now; + + if (remaining <= 0) { + setTimeRemaining(0); + // Call expiry callback + if (onExpiry) { + onExpiry(); + } + onClose(); + } else { + setTimeRemaining(remaining); + } + }; + + // Update immediately + updateTimer(); + + // Then update every second + const interval = setInterval(updateTimer, 1000); + + return () => clearInterval(interval); + }, [isOpen, expiryTimestamp, onExpiry, onClose]); + + // Format time remaining as MM:SS + const formatTime = (seconds: number): string => { + const mins = Math.floor(seconds / 60); + const secs = seconds % 60; + return `${mins}:${secs.toString().padStart(2, '0')}`; + }; + if (!isOpen) return null; const IconComponent = icon === 'zap' ? Zap : QrCode; @@ -58,6 +101,14 @@ export function QRModal({ + {expiryTimestamp && timeRemaining !== null && timeRemaining <= 3600 && ( +
+ + + Expires in {formatTime(timeRemaining)} + +
+ )}