From ef7e15973af3b4bf4926fa294c202a0d5be98f73 Mon Sep 17 00:00:00 2001 From: Alex Lewin Date: Tue, 22 Oct 2024 11:53:27 +0100 Subject: [PATCH] feat: add lightning status to hooksDemo --- packages/react/lib/hooks/index.ts | 4 +- ...tningInvoice.ts => useReceiveLightning.ts} | 11 +++--- packages/react/src/components/HooksDemo.tsx | 38 +++++++++++-------- 3 files changed, 29 insertions(+), 24 deletions(-) rename packages/react/lib/hooks/{useLightningInvoice.ts => useReceiveLightning.ts} (83%) diff --git a/packages/react/lib/hooks/index.ts b/packages/react/lib/hooks/index.ts index 16fdb7d..daf1ad4 100644 --- a/packages/react/lib/hooks/index.ts +++ b/packages/react/lib/hooks/index.ts @@ -1,6 +1,6 @@ import { useBalance } from './useBalance' import { useOpenWallet } from './useOpenWallet' import { useFedimintWallet } from './useFedimintWallet' -import { useLightningInvoice } from './useLightningInvoice' +import { useReceiveLightning } from './useReceiveLightning' -export { useBalance, useOpenWallet, useFedimintWallet, useLightningInvoice } +export { useBalance, useOpenWallet, useFedimintWallet, useReceiveLightning } diff --git a/packages/react/lib/hooks/useLightningInvoice.ts b/packages/react/lib/hooks/useReceiveLightning.ts similarity index 83% rename from packages/react/lib/hooks/useLightningInvoice.ts rename to packages/react/lib/hooks/useReceiveLightning.ts index 9d796a5..be1a037 100644 --- a/packages/react/lib/hooks/useLightningInvoice.ts +++ b/packages/react/lib/hooks/useReceiveLightning.ts @@ -2,12 +2,12 @@ import { useCallback, useEffect, useState } from 'react' import { useFedimintWallet, useOpenWallet } from '.' import { LnReceiveState, type CreateBolt11Response } from '@fedimint/core-web' -export const useLightningInvoice = () => { +export const useReceiveLightning = () => { const wallet = useFedimintWallet() const { walletStatus } = useOpenWallet() const [invoice, setInvoice] = useState() - const [isPaid, setIsPaid] = useState() - const [invoiceStatus, setInvoiceStatus] = useState() + const [invoiceReceiveState, setInvoiceReceiveState] = + useState() const [error, setError] = useState() const generateInvoice = useCallback( @@ -25,7 +25,7 @@ export const useLightningInvoice = () => { const unsubscribe = wallet.lightning.subscribeLnReceive( invoice.operation_id, (state) => { - setInvoiceStatus(state) + setInvoiceReceiveState(state) }, (error) => { setError(error) @@ -40,8 +40,7 @@ export const useLightningInvoice = () => { return { generateInvoice, bolt11: invoice?.invoice, - invoiceStatus, - isPaid, + invoiceStatus: invoiceReceiveState, error, } } diff --git a/packages/react/src/components/HooksDemo.tsx b/packages/react/src/components/HooksDemo.tsx index 6935183..4326a36 100644 --- a/packages/react/src/components/HooksDemo.tsx +++ b/packages/react/src/components/HooksDemo.tsx @@ -1,18 +1,15 @@ -import React, { useEffect } from 'react' -import { - useBalance, - useLightningInvoice, - useOpenWallet, - useFedimintWallet, -} from '../../lib' +import React from 'react' +import { useBalance, useReceiveLightning, useOpenWallet } from '../../lib' + +const TEST_FEDERATION_INVITE = + 'fed11qgqzc2nhwden5te0vejkg6tdd9h8gepwvejkg6tdd9h8garhduhx6at5d9h8jmn9wshxxmmd9uqqzgxg6s3evnr6m9zdxr6hxkdkukexpcs3mn7mj3g5pc5dfh63l4tj6g9zk4er' function HooksDemo() { const balance = useBalance() const { walletStatus, openWallet, joinFederation } = useOpenWallet() - const wallet = useFedimintWallet() const isOpen = walletStatus === 'open' - const { generateInvoice, bolt11, invoiceStatus, isPaid, error } = - useLightningInvoice() + const { generateInvoice, bolt11, invoiceStatus, error } = + useReceiveLightning() return ( <> @@ -34,11 +31,7 @@ function HooksDemo() { joinFederation(invite) @@ -68,7 +61,20 @@ function HooksDemo() { bolt11:

{bolt11 ? bolt11 : 'no invoice generated'}

-
+
+ invoiceStatus: +

+ {typeof invoiceStatus === 'string' + ? invoiceStatus + : typeof invoiceStatus === 'object' + ? Object.keys(invoiceStatus)[0] + : 'no invoice status'} +

+
+
+ error: +

{error}

+
)