Skip to content

Commit

Permalink
feat: add lightning status to hooksDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlwn123 committed Oct 22, 2024
1 parent 3cd12e1 commit ef7e159
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/react/lib/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateBolt11Response>()
const [isPaid, setIsPaid] = useState<boolean>()
const [invoiceStatus, setInvoiceStatus] = useState<LnReceiveState>()
const [invoiceReceiveState, setInvoiceReceiveState] =
useState<LnReceiveState>()
const [error, setError] = useState<string>()

const generateInvoice = useCallback(
Expand All @@ -25,7 +25,7 @@ export const useLightningInvoice = () => {
const unsubscribe = wallet.lightning.subscribeLnReceive(
invoice.operation_id,
(state) => {
setInvoiceStatus(state)
setInvoiceReceiveState(state)
},
(error) => {
setError(error)
Expand All @@ -40,8 +40,7 @@ export const useLightningInvoice = () => {
return {
generateInvoice,
bolt11: invoice?.invoice,
invoiceStatus,
isPaid,
invoiceStatus: invoiceReceiveState,
error,
}
}
38 changes: 22 additions & 16 deletions packages/react/src/components/HooksDemo.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
Expand All @@ -34,11 +31,7 @@ function HooksDemo() {
<b>joinFederation(invite)</b>
<button
disabled={isOpen}
onClick={() =>
joinFederation(
'fed11qgqzc2nhwden5te0vejkg6tdd9h8gepwvejkg6tdd9h8garhduhx6at5d9h8jmn9wshxxmmd9uqqzgxg6s3evnr6m9zdxr6hxkdkukexpcs3mn7mj3g5pc5dfh63l4tj6g9zk4er',
)
}
onClick={() => joinFederation(TEST_FEDERATION_INVITE)}
>
Join Federation
</button>
Expand Down Expand Up @@ -68,7 +61,20 @@ function HooksDemo() {
<b>bolt11:</b>
<p className="truncate">{bolt11 ? bolt11 : 'no invoice generated'}</p>
</div>
<div className="row"></div>
<div className="row">
<b>invoiceStatus:</b>
<p>
{typeof invoiceStatus === 'string'
? invoiceStatus
: typeof invoiceStatus === 'object'
? Object.keys(invoiceStatus)[0]
: 'no invoice status'}
</p>
</div>
<div className="row">
<b>error:</b>
<p>{error}</p>
</div>
</div>
</>
)
Expand Down

0 comments on commit ef7e159

Please sign in to comment.