Fix QR rerender on WebLN payment (#1181)

This commit is contained in:
ekzyis 2024-05-17 20:27:34 -05:00 committed by GitHub
parent cf995d0a9f
commit 7119c2ef0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 13 deletions

View File

@ -1,7 +1,7 @@
import QRCode from 'qrcode.react' import QRCode from 'qrcode.react'
import { CopyInput, InputSkeleton } from './form' import { CopyInput, InputSkeleton } from './form'
import InvoiceStatus from './invoice-status' import InvoiceStatus from './invoice-status'
import { useEffect, useRef } from 'react' import { useEffect } from 'react'
import { useWebLN } from './webln' import { useWebLN } from './webln'
import SimpleCountdown from './countdown' import SimpleCountdown from './countdown'
import Bolt11Info from './bolt11-info' import Bolt11Info from './bolt11-info'
@ -9,13 +9,10 @@ import Bolt11Info from './bolt11-info'
export default function Qr ({ asIs, value, webLn, statusVariant, description, status }) { export default function Qr ({ asIs, value, webLn, statusVariant, description, status }) {
const qrValue = asIs ? value : 'lightning:' + value.toUpperCase() const qrValue = asIs ? value : 'lightning:' + value.toUpperCase()
const provider = useWebLN() const provider = useWebLN()
// XXX antipattern ... we shouldn't be getting multiple renders
const sendPayment = useRef(false)
useEffect(() => { useEffect(() => {
async function effect () { async function effect () {
if (webLn && provider && !sendPayment.current) { if (webLn && provider) {
sendPayment.current = true
try { try {
await provider.sendPayment({ bolt11: value }) await provider.sendPayment({ bolt11: value })
} catch (e) { } catch (e) {

View File

@ -1,4 +1,4 @@
import { createContext, useCallback, useContext, useEffect, useState } from 'react' import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { useWalletLogger } from '../logger' import { useWalletLogger } from '../logger'
import { Status, migrateLocalStorage } from '.' import { Status, migrateLocalStorage } from '.'
import { bolt11Tags } from '@/lib/bolt11' import { bolt11Tags } from '@/lib/bolt11'
@ -72,7 +72,6 @@ export function LNbitsProvider ({ children }) {
const [status, setStatus] = useState() const [status, setStatus] = useState()
const { logger } = useWalletLogger(Wallet.LNbits) const { logger } = useWalletLogger(Wallet.LNbits)
const name = 'LNbits'
let storageKey = 'webln:provider:lnbits' let storageKey = 'webln:provider:lnbits'
if (me) { if (me) {
storageKey = `${storageKey}:${me.id}` storageKey = `${storageKey}:${me.id}`
@ -196,7 +195,9 @@ export function LNbitsProvider ({ children }) {
loadConfig().catch(console.error) loadConfig().catch(console.error)
}, []) }, [])
const value = { name, url, adminKey, status, saveConfig, clearConfig, getInfo, sendPayment } const value = useMemo(
() => ({ name: 'LNbits', url, adminKey, status, saveConfig, clearConfig, getInfo, sendPayment }),
[url, adminKey, status, saveConfig, clearConfig, getInfo, sendPayment])
return ( return (
<LNbitsContext.Provider value={value}> <LNbitsContext.Provider value={value}>
{children} {children}

View File

@ -1,4 +1,4 @@
import { createContext, useCallback, useContext, useEffect, useState } from 'react' import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { useWalletLogger } from '../logger' import { useWalletLogger } from '../logger'
import LNC from '@lightninglabs/lnc-web' import LNC from '@lightninglabs/lnc-web'
import { Status, migrateLocalStorage } from '.' import { Status, migrateLocalStorage } from '.'
@ -191,8 +191,11 @@ export function LNCProvider ({ children }) {
})() })()
}, [me, setStatus, setConfig, logger]) }, [me, setStatus, setConfig, logger])
const value = useMemo(
() => ({ name: 'lnc', status, unlock, getInfo, sendPayment, config, saveConfig, clearConfig }),
[status, unlock, getInfo, sendPayment, config, saveConfig, clearConfig])
return ( return (
<LNCContext.Provider value={{ name: 'lnc', status, unlock, getInfo, sendPayment, config, saveConfig, clearConfig }}> <LNCContext.Provider value={value}>
{children} {children}
{modal} {modal}
</LNCContext.Provider> </LNCContext.Provider>

View File

@ -1,6 +1,6 @@
// https://github.com/getAlby/js-sdk/blob/master/src/webln/NostrWeblnProvider.ts // https://github.com/getAlby/js-sdk/blob/master/src/webln/NostrWeblnProvider.ts
import { createContext, useCallback, useContext, useEffect, useState } from 'react' import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
import { Relay, finalizeEvent, nip04 } from 'nostr-tools' import { Relay, finalizeEvent, nip04 } from 'nostr-tools'
import { parseNwcUrl } from '@/lib/url' import { parseNwcUrl } from '@/lib/url'
import { useWalletLogger } from '../logger' import { useWalletLogger } from '../logger'
@ -20,7 +20,6 @@ export function NWCProvider ({ children }) {
const [status, setStatus] = useState() const [status, setStatus] = useState()
const { logger } = useWalletLogger(Wallet.NWC) const { logger } = useWalletLogger(Wallet.NWC)
const name = 'NWC'
let storageKey = 'webln:provider:nwc' let storageKey = 'webln:provider:nwc'
if (me) { if (me) {
storageKey = `${storageKey}:${me.id}` storageKey = `${storageKey}:${me.id}`
@ -273,7 +272,9 @@ export function NWCProvider ({ children }) {
loadConfig().catch(err => logger.error(err.message || err.toString?.())) loadConfig().catch(err => logger.error(err.message || err.toString?.()))
}, []) }, [])
const value = { name, nwcUrl, relayUrl, walletPubkey, secret, status, saveConfig, clearConfig, getInfo, sendPayment } const value = useMemo(
() => ({ name: 'NWC', nwcUrl, relayUrl, walletPubkey, secret, status, saveConfig, clearConfig, getInfo, sendPayment }),
[nwcUrl, relayUrl, walletPubkey, secret, status, saveConfig, clearConfig, getInfo, sendPayment])
return ( return (
<NWCContext.Provider value={value}> <NWCContext.Provider value={value}>
{children} {children}