import { useState, useEffect } from 'react' import { numWithUnits } from '@/lib/format' import AccordianItem from './accordian-item' import Qr from './qr' import Countdown from './countdown' import PayerData from './payer-data' import Bolt11Info from './bolt11-info' import { useQuery } from '@apollo/client' import { INVOICE } from '@/fragments/wallet' import { FAST_POLL_INTERVAL, SSR } from '@/lib/constants' import { WebLnNotEnabledError } from './payment' export default function Invoice ({ invoice, modal, onPayment, info, successVerb, webLn, webLnError, poll }) { const [expired, setExpired] = useState(new Date(invoice.expiredAt) <= new Date()) const { data, error } = useQuery(INVOICE, SSR ? {} : { pollInterval: FAST_POLL_INTERVAL, variables: { id: invoice.id }, nextFetchPolicy: 'cache-and-network', skip: !poll }) if (data) { invoice = data.invoice } if (error) { return
{error.toString()}
} // if webLn was not passed, use true by default if (webLn === undefined) webLn = true let variant = 'default' let status = 'waiting for you' if (invoice.cancelled) { variant = 'failed' status = 'cancelled' webLn = false } else if (invoice.confirmedAt || (invoice.isHeld && invoice.satsReceived && !expired)) { variant = 'confirmed' status = `${numWithUnits(invoice.satsReceived, { abbreviate: false })} ${successVerb || 'deposited'}` webLn = false } else if (expired) { variant = 'failed' status = 'expired' webLn = false } useEffect(() => { if (invoice.confirmedAt || (invoice.isHeld && invoice.satsReceived)) { onPayment?.(invoice) } }, [invoice.confirmedAt, invoice.isHeld, invoice.satsReceived]) const { nostr, comment, lud18Data, bolt11, confirmedPreimage } = invoice return ( <> {webLnError && !(webLnError instanceof WebLnNotEnabledError) &&
Payment from attached wallet failed:
{webLnError.toString()}
} {invoice.confirmedAt ? (
) : (
{ setExpired(true) }} />
)} {!modal && <> {info &&
{info}
}
{nostr ? {JSON.stringify(nostr, null, 2)} } /> : null}
{lud18Data &&
} />
} {comment &&
{comment}} />
} } ) }