From 4fe1d416de98124d7f32395d5d8ff64bd23b0218 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 10 Aug 2023 04:13:09 +0200 Subject: [PATCH] Fix onConfirmation called at every render --- components/invoice.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/components/invoice.js b/components/invoice.js index 5f0526fb..d2f4bca5 100644 --- a/components/invoice.js +++ b/components/invoice.js @@ -20,7 +20,6 @@ export function Invoice ({ invoice, onConfirmation, successVerb }) { if (invoice.confirmedAt) { variant = 'confirmed' status = `${numWithUnits(invoice.satsReceived, { abbreviate: false })} ${successVerb || 'deposited'}` - onConfirmation?.(invoice) webLn = false } else if (invoice.cancelled) { variant = 'failed' @@ -32,6 +31,12 @@ export function Invoice ({ invoice, onConfirmation, successVerb }) { webLn = false } + useEffect(() => { + if (invoice.confirmedAt) { + onConfirmation?.(invoice) + } + }, [invoice.confirmedAt]) + const { nostr } = invoice return ( @@ -150,14 +155,10 @@ export const useInvoiceable = (fn, options = defaultOptions) => { const [fnArgs, setFnArgs] = useState() // fix for bug where `showModal` runs the code for two modals and thus executes `onConfirmation` twice - let called = false let errorCount = 0 const onConfirmation = useCallback( onClose => { - called = false return async ({ id, satsReceived, hash }) => { - if (called) return - called = true await sleep(2000) const repeat = () => fn(satsReceived, ...fnArgs, hash)