snort/src/element/Invoice.js

73 lines
2.5 KiB
JavaScript
Raw Normal View History

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-02 13:40:42 +00:00
import { decode as invoiceDecode } from "light-bolt11-decoder";
import { useMemo } from "react";
2023-01-09 16:18:34 +00:00
import NoteTime from "./NoteTime";
2023-01-09 22:26:41 +00:00
import QrCode from "./QrCode";
2023-01-02 13:40:42 +00:00
export default function Invoice(props) {
const invoice = props.invoice;
2023-01-09 22:26:41 +00:00
const [showLnQr, setShowLnQr] = 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-04 20:09:35 +00:00
let amount = parseInt(parsed.sections.find(a => a.name === "amount")?.value);
2023-01-06 19:57:26 +00:00
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);
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-01-06 19:57:26 +00:00
function header() {
if (info?.description?.length > 0) {
return (
<>
<h4> Invoice for {info?.amount?.toLocaleString()} sats</h4>
<p>{info?.description}</p>
2023-01-09 22:26:41 +00:00
{ showLnQr ? <p><QrCode data={invoice} link={"lightning:${invoice}"} /></p> : null }
2023-01-06 19:57:26 +00:00
</>
)
} else {
return (
2023-01-09 22:26:41 +00:00
<>
2023-01-06 19:57:26 +00:00
<h4> Invoice for {info?.amount?.toLocaleString()} sats</h4>
2023-01-09 22:26:41 +00:00
{ showLnQr ? <p><QrCode data={invoice} link={"lightning:${invoice}"} /></p> : null }
</>
2023-01-06 19:57:26 +00:00
)
}
}
2023-01-09 22:26:41 +00:00
function pay(){
return (
<>
{ showLnQr ? <div className="btn" onClick={() => window.open(`lightning:${invoice}`)}>Pay</div> :
<div className="btn" onClick={(e) => setShowLnQr(true)}>Pay</div> }
</>
)
}
2023-01-06 19:57:26 +00:00
2023-01-02 13:40:42 +00:00
return (
2023-01-08 20:36:36 +00:00
<div className="note-invoice flex">
2023-01-06 19:57:26 +00:00
<div className="f-grow flex f-col">
{header()}
2023-01-09 16:18:34 +00:00
{info?.expire ? <small>{info?.expired ? "Expired" : "Expires"} <NoteTime from={info.expire * 1000} /></small> : null}
2023-01-02 13:40:42 +00:00
</div>
2023-01-06 19:57:26 +00:00
2023-01-09 22:26:41 +00:00
{info?.expired ? <div className="btn">Expired</div> : pay() }
2023-01-02 13:40:42 +00:00
</div>
)
}