import { useEffect, useState } from 'react' import QRCode from 'react-qr-code' import type { AlbyInvoice } from '@/types/alby' import { getAlbyService, isWebLNAvailable } from '@/lib/alby' import { AlbyInstaller } from './AlbyInstaller' interface PaymentModalProps { invoice: AlbyInvoice onClose: () => void onPaymentComplete: () => void } export function PaymentModal({ invoice, onClose, onPaymentComplete }: PaymentModalProps) { const [copied, setCopied] = useState(false) const [timeRemaining, setTimeRemaining] = useState(null) const paymentUrl = `lightning:${invoice.invoice}` // Calculate time remaining until invoice expiry useEffect(() => { if (invoice.expiresAt) { const updateTimeRemaining = () => { const now = Math.floor(Date.now() / 1000) const remaining = invoice.expiresAt - now setTimeRemaining(remaining > 0 ? remaining : 0) } updateTimeRemaining() const interval = setInterval(updateTimeRemaining, 1000) return () => clearInterval(interval) } }, [invoice.expiresAt]) const formatTimeRemaining = (seconds: number): string => { if (seconds <= 0) return 'Expired' const minutes = Math.floor(seconds / 60) const secs = seconds % 60 return `${minutes}:${secs.toString().padStart(2, '0')}` } const handleCopy = async () => { try { await navigator.clipboard.writeText(invoice.invoice) setCopied(true) setTimeout(() => setCopied(false), 2000) } catch (e) { console.error('Failed to copy:', e) } } const handleOpenWallet = async () => { try { const alby = getAlbyService() if (!isWebLNAvailable()) { throw new Error('WebLN is not available. Please install Alby or another Lightning wallet extension.') } await alby.enable() await alby.sendPayment(invoice.invoice) onPaymentComplete() } catch (e) { const error = e instanceof Error ? e : new Error(String(e)) console.error('Payment failed:', error) if (error.message.includes('user rejected') || error.message.includes('cancelled')) { return } alert(`Payment failed: ${error.message}`) } } return (

Pay {invoice.amount} sats

{timeRemaining !== null && (

Time remaining: {formatTimeRemaining(timeRemaining)}

)}

Lightning Invoice:

{invoice.invoice}
{/* QR Code */}

Scan with your Lightning wallet to pay

{timeRemaining !== null && timeRemaining <= 0 && (

This invoice has expired

Please close this modal and try again to generate a new invoice.

)}

Payment will be automatically verified once completed

) }