2023-01-02 13:40:42 +00:00
|
|
|
import "./Invoice.css";
|
2023-01-09 22:26:41 +00:00
|
|
|
import { useState } from "react";
|
2023-01-16 17:48:25 +00:00
|
|
|
// @ts-expect-error
|
2023-01-02 13:40:42 +00:00
|
|
|
import { decode as invoiceDecode } from "light-bolt11-decoder";
|
|
|
|
import { useMemo } from "react";
|
2023-01-20 11:11:50 +00:00
|
|
|
import NoteTime from "Element/NoteTime";
|
2023-02-07 13:32:32 +00:00
|
|
|
import SendSats from "Element/SendSats";
|
2023-02-01 22:42:46 +00:00
|
|
|
import ZapCircle from "Icons/ZapCircle";
|
2023-01-26 15:57:36 +00:00
|
|
|
import useWebln from "Hooks/useWebln";
|
2023-01-02 13:40:42 +00:00
|
|
|
|
2023-01-16 17:48:25 +00:00
|
|
|
export interface InvoiceProps {
|
|
|
|
invoice: string
|
|
|
|
}
|
|
|
|
export default function Invoice(props: InvoiceProps) {
|
2023-01-02 13:40:42 +00:00
|
|
|
const invoice = props.invoice;
|
2023-01-26 15:57:36 +00:00
|
|
|
const webln = useWebln();
|
2023-01-12 06:07:48 +00:00
|
|
|
const [showInvoice, setShowInvoice] = useState(false);
|
2023-01-02 13:40:42 +00:00
|
|
|
|
|
|
|
const info = useMemo(() => {
|
2023-01-04 20:09:35 +00:00
|
|
|
try {
|
|
|
|
let parsed = invoiceDecode(invoice);
|
2023-01-09 22:26:41 +00:00
|
|
|
|
2023-01-16 17:48:25 +00:00
|
|
|
let amount = parseInt(parsed.sections.find((a: any) => a.name === "amount")?.value);
|
|
|
|
let timestamp = parseInt(parsed.sections.find((a: any) => a.name === "timestamp")?.value);
|
|
|
|
let expire = parseInt(parsed.sections.find((a: any) => a.name === "expiry")?.value);
|
|
|
|
let description = parsed.sections.find((a: any) => a.name === "description")?.value;
|
2023-01-06 19:57:26 +00:00
|
|
|
let ret = {
|
|
|
|
amount: !isNaN(amount) ? (amount / 1000) : 0,
|
|
|
|
expire: !isNaN(timestamp) && !isNaN(expire) ? timestamp + expire : null,
|
2023-01-12 12:00:44 +00:00
|
|
|
description,
|
|
|
|
expired: false
|
2023-01-06 19:57:26 +00:00
|
|
|
};
|
|
|
|
if (ret.expire) {
|
|
|
|
ret.expired = ret.expire < (new Date().getTime() / 1000);
|
2023-01-04 20:09:35 +00:00
|
|
|
}
|
2023-01-06 19:57:26 +00:00
|
|
|
return ret;
|
2023-01-04 20:09:35 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2023-01-02 13:40:42 +00:00
|
|
|
}
|
|
|
|
}, [invoice]);
|
|
|
|
|
2023-02-01 22:42:46 +00:00
|
|
|
const [isPaid, setIsPaid] = useState(false);
|
|
|
|
const isExpired = info?.expired
|
|
|
|
const amount = info?.amount ?? 0
|
|
|
|
const description = info?.description
|
|
|
|
|
2023-01-06 19:57:26 +00:00
|
|
|
function header() {
|
2023-02-01 22:42:46 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h4>Lightning Invoice</h4>
|
|
|
|
<ZapCircle className="zap-circle" />
|
2023-02-07 13:32:32 +00:00
|
|
|
<SendSats title="Pay Invoice" invoice={invoice} show={showInvoice} onClose={() => setShowInvoice(false)} />
|
2023-02-01 22:42:46 +00:00
|
|
|
</>
|
|
|
|
)
|
2023-01-06 19:57:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 22:42:46 +00:00
|
|
|
async function payInvoice(e: any) {
|
2023-01-26 15:57:36 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
if (webln?.enabled) {
|
|
|
|
try {
|
2023-02-01 22:42:46 +00:00
|
|
|
await webln.sendPayment(invoice);
|
|
|
|
setIsPaid(true)
|
2023-01-26 15:57:36 +00:00
|
|
|
} catch (error) {
|
|
|
|
setShowInvoice(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setShowInvoice(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 13:40:42 +00:00
|
|
|
return (
|
2023-01-12 06:07:48 +00:00
|
|
|
<>
|
2023-02-01 22:42:46 +00:00
|
|
|
<div className={`note-invoice flex ${isExpired ? 'expired' : ''} ${isPaid ? 'paid' : ''}`}>
|
|
|
|
<div className="invoice-header">
|
|
|
|
{header()}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<p className="invoice-amount">
|
|
|
|
{amount > 0 && (
|
|
|
|
<>
|
|
|
|
{amount.toLocaleString()} <span className="sats">sat{amount === 1 ? '' : 's'}</span>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<div className="invoice-body">
|
|
|
|
{description && <p>{description}</p>}
|
|
|
|
{isPaid ? (
|
|
|
|
<div className="paid">
|
|
|
|
Paid
|
2023-01-12 09:57:35 +00:00
|
|
|
</div>
|
2023-02-01 22:42:46 +00:00
|
|
|
) : (
|
|
|
|
<button disabled={isExpired} type="button" onClick={payInvoice}>
|
|
|
|
{isExpired ? "Expired" : "Pay"}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
2023-01-06 19:57:26 +00:00
|
|
|
|
2023-01-12 09:57:35 +00:00
|
|
|
</div>
|
2023-01-12 06:07:48 +00:00
|
|
|
|
|
|
|
</>
|
2023-01-02 13:40:42 +00:00
|
|
|
)
|
2023-02-01 22:42:46 +00:00
|
|
|
}
|