import "./Invoice.css"; import { useState } from "react"; import { decode as invoiceDecode } from "light-bolt11-decoder"; import { useMemo } from "react"; import NoteTime from "./NoteTime"; import QrCode from "./QrCode"; export default function Invoice(props) { const invoice = props.invoice; const [showLnQr, setShowLnQr] = useState(false); const info = useMemo(() => { try { let parsed = invoiceDecode(invoice); let amount = parseInt(parsed.sections.find(a => a.name === "amount")?.value); let timestamp = parseInt(parsed.sections.find(a => a.name === "timestamp")?.value); let expire = parseInt(parsed.sections.find(a => a.name === "expiry")?.value); let description = parsed.sections.find(a => a.name === "description")?.value; let ret = { amount: !isNaN(amount) ? (amount / 1000) : 0, expire: !isNaN(timestamp) && !isNaN(expire) ? timestamp + expire : null, description }; if (ret.expire) { ret.expired = ret.expire < (new Date().getTime() / 1000); } return ret; } catch (e) { console.error(e); } }, [invoice]); function header() { if (info?.description?.length > 0) { return ( <>

⚡️ Invoice for {info?.amount?.toLocaleString()} sats

{info?.description}

{ showLnQr ?

: null } ) } else { return ( <>

⚡️ Invoice for {info?.amount?.toLocaleString()} sats

{ showLnQr ?

: null } ) } } function pay(){ return ( <> { showLnQr ?
window.open(`lightning:${invoice}`)}>Pay
:
setShowLnQr(true)}>Pay
} ) } return (
{header()} {info?.expire ? {info?.expired ? "Expired" : "Expires"} : null}
{info?.expired ?
Expired
: pay() }
) }