snort/src/element/Invoice.js

58 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-01-02 13:40:42 +00:00
import "./Invoice.css";
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-02 13:40:42 +00:00
export default function Invoice(props) {
const invoice = props.invoice;
const info = useMemo(() => {
2023-01-04 20:09:35 +00:00
try {
let parsed = invoiceDecode(invoice);
2023-01-06 20:11:52 +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>
</>
)
} else {
return (
<h4> Invoice for {info?.amount?.toLocaleString()} sats</h4>
)
}
}
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
{info?.expired ? <div className="btn">Expired</div> :
<div className="btn" onClick={() => window.open(`lightning:${invoice}`)}>Pay</div>}
2023-01-02 13:40:42 +00:00
</div>
)
}