import { Button } from '@/components/ui/button' import { formatAmount, getAmountFromInvoice } from '@/lib/lightning' import { cn } from '@/lib/utils' import { useNostr } from '@/providers/NostrProvider' import lightning from '@/services/lightning.service' import { Loader, Zap } from 'lucide-react' import { useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { toast } from 'sonner' export function EmbeddedLNInvoice({ invoice, className }: { invoice: string; className?: string }) { const { t } = useTranslation() const { checkLogin, pubkey } = useNostr() const [paying, setPaying] = useState(false) const amount = useMemo(() => { return getAmountFromInvoice(invoice) }, [invoice]) const handlePay = async () => { try { if (!pubkey) { throw new Error('You need to be logged in to zap') } setPaying(true) const invoiceResult = await lightning.payInvoice(invoice) // user canceled if (!invoiceResult) { return } } catch (error) { toast.error(t('Lightning payment failed') + ': ' + (error as Error).message) } finally { setPaying(false) } } const handlePayClick = (e: React.MouseEvent) => { e.stopPropagation() checkLogin(() => handlePay()) } return (
e.stopPropagation()} >
{t('Lightning Invoice')}
{formatAmount(amount)} {t('sats')}
) }