Skip to content

Commit

Permalink
handle invoices inside of message
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul authored and benthecarman committed Apr 13, 2024
1 parent 1bbd679 commit 626b614
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/routes/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,20 @@ function SingleMessage(props: {

const parsed = createAsync(
async () => {
const result = toParsedParams(props.dm.message, network);
let result = undefined;

// Look for a long word that might be an invoice
const split_message_by_whitespace = props.dm.message.split(/\s+/g);
for (const word of split_message_by_whitespace) {
if (word.length > 15) {
result = toParsedParams(word, network);
if (result.ok) {
break;
}
}
}

if (!result.ok) {
if (!result || !result.ok) {
return undefined;
}

Expand All @@ -92,6 +103,10 @@ function SingleMessage(props: {
return {
type: "invoice",
status: "paid",
message_without_invoice: props.dm.message.replace(
result.value.original,
""
),
value: result.value.invoice,
amount: result.value.amount_sats
};
Expand All @@ -103,6 +118,10 @@ function SingleMessage(props: {
return {
type: "invoice",
status: "unpaid",
message_without_invoice: props.dm.message.replace(
result.value.original,
""
),
from: props.dm.from,
value: result.value.invoice,
amount: result.value.amount_sats
Expand Down Expand Up @@ -130,9 +149,9 @@ function SingleMessage(props: {
navWithContactId();
}

function handlePay() {
function handlePay(invoice: string) {
actions.handleIncomingString(
props.dm.message,
invoice,
(error) => {
showToast(error);
},
Expand All @@ -153,6 +172,11 @@ function SingleMessage(props: {
<Switch>
<Match when={parsed()?.type === "invoice"}>
<div class="flex flex-col gap-2">
<Show when={parsed()?.message_without_invoice}>
<p class="!mb-0 break-words">
{parsed()?.message_without_invoice}
</p>
</Show>
<div class="flex items-center gap-2">
<Zap class="h-4 w-4" />
<span>Lightning Invoice</span>
Expand All @@ -167,7 +191,7 @@ function SingleMessage(props: {
<Button
intent="blue"
layout="xs"
onClick={handlePay}
onClick={() => handlePay(parsed()?.value || "")}
>
Pay
</Button>
Expand Down

0 comments on commit 626b614

Please sign in to comment.