added expiry timer

This commit is contained in:
lemonknowsall committed 2025-10-12 13:28:11 -07:00
1 parent 45918e86e2
commit 7ba3d7f8c5
2 files changed
+70 -2

No files matched your search

+18 -1
View File
@@ -104,6 +104,7 @@ export function CashuWallet() {
amount: number; amount: number;
invoice: string; invoice: string;
qrCodeUrl: string; qrCodeUrl: string;
expiry: number;
} | null>(null); } | null>(null);
const [showRemoveMintModal, setShowRemoveMintModal] = useState(false); const [showRemoveMintModal, setShowRemoveMintModal] = useState(false);
@@ -212,7 +213,8 @@ export function CashuWallet() {
mintUrl: activeMintUrl, mintUrl: activeMintUrl,
amount, amount,
invoice: quote.request, invoice: quote.request,
qrCodeUrl qrCodeUrl,
expiry: quote.expiry
}); });
setShowInvoiceModal(true); setShowInvoiceModal(true);
@@ -694,11 +696,26 @@ export function CashuWallet() {
} }
setShowInvoiceModal(false); 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" title="Lightning Invoice"
description={`Pay this invoice to receive ${currentInvoice.amount} sats`} description={`Pay this invoice to receive ${currentInvoice.amount} sats`}
qrCodeUrl={currentInvoice.qrCodeUrl} qrCodeUrl={currentInvoice.qrCodeUrl}
content={currentInvoice.invoice} content={currentInvoice.invoice}
icon="zap" icon="zap"
expiryTimestamp={currentInvoice.expiry}
/> />
)} )}
+52 -1
View File
@@ -1,7 +1,8 @@
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; 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 { useCopyToClipboard } from '@/hooks/useCopyToClipboard';
import { useEffect, useState } from 'react';
interface QRModalProps { interface QRModalProps {
isOpen: boolean; isOpen: boolean;
@@ -11,6 +12,8 @@ interface QRModalProps {
qrCodeUrl: string; qrCodeUrl: string;
content: string; content: string;
icon?: 'qr' | 'zap'; icon?: 'qr' | 'zap';
expiryTimestamp?: number;
onExpiry?: () => void;
} }
export function QRModal({ export function QRModal({
@@ -21,12 +24,52 @@ export function QRModal({
qrCodeUrl, qrCodeUrl,
content, content,
icon = 'qr', icon = 'qr',
expiryTimestamp,
onExpiry,
}: QRModalProps) { }: QRModalProps) {
const { copy, copied } = useCopyToClipboard(); const { copy, copied } = useCopyToClipboard();
const [timeRemaining, setTimeRemaining] = useState<number | null>(null);
const handleCopy = () => { const handleCopy = () => {
copy(content); 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; if (!isOpen) return null;
const IconComponent = icon === 'zap' ? Zap : QrCode; const IconComponent = icon === 'zap' ? Zap : QrCode;
@@ -58,6 +101,14 @@ export function QRModal({
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent className="space-y-4"> <CardContent className="space-y-4">
{expiryTimestamp && timeRemaining !== null && timeRemaining <= 3600 && (
<div className="flex items-center justify-center gap-2 text-sm">
<Clock className="h-4 w-4" />
<span className={timeRemaining < 60 ? "text-destructive font-semibold" : "text-muted-foreground"}>
Expires in {formatTime(timeRemaining)}
</span>
</div>
)}
<div className="flex justify-center"> <div className="flex justify-center">
<div className="bg-white dark:bg-gray-100 p-4 rounded-lg border shadow-sm"> <div className="bg-white dark:bg-gray-100 p-4 rounded-lg border shadow-sm">
<img <img