From 094ccd92da3315fa1ca5fcf30b1f6fcdc9c211a3 Mon Sep 17 00:00:00 2001 From: codytseng Date: Sat, 8 Mar 2025 11:35:23 +0800 Subject: [PATCH] feat: handle user cancellation in zap process --- src/components/NoteStats/ZapButton.tsx | 8 ++++++-- src/components/ZapDialog/index.tsx | 8 ++++++-- src/services/lightning.service.ts | 3 ++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/components/NoteStats/ZapButton.tsx b/src/components/NoteStats/ZapButton.tsx index 253629d..206a6b9 100644 --- a/src/components/NoteStats/ZapButton.tsx +++ b/src/components/NoteStats/ZapButton.tsx @@ -48,14 +48,18 @@ export default function ZapButton({ event }: { event: Event }) { throw new Error('You need to be logged in to zap') } setZapping(true) - const { invoice } = await lightning.zap( + const zapResult = await lightning.zap( pubkey, event.pubkey, defaultZapSats, defaultZapComment, event.id ) - addZap(event.id, invoice, defaultZapSats, defaultZapComment) + // user canceled + if (!zapResult) { + return + } + addZap(event.id, zapResult.invoice, defaultZapSats, defaultZapComment) } catch (error) { toast({ title: t('Zap failed'), diff --git a/src/components/ZapDialog/index.tsx b/src/components/ZapDialog/index.tsx index 7ba8beb..ded731a 100644 --- a/src/components/ZapDialog/index.tsx +++ b/src/components/ZapDialog/index.tsx @@ -77,11 +77,15 @@ function ZapDialogContent({ throw new Error('You need to be logged in to zap') } setZapping(true) - const { invoice } = await lightning.zap(pubkey, recipient, sats, comment, eventId, () => + const zapResult = await lightning.zap(pubkey, recipient, sats, comment, eventId, () => setOpen(false) ) + // user canceled + if (!zapResult) { + return + } if (eventId) { - addZap(eventId, invoice, sats, comment) + addZap(eventId, zapResult.invoice, sats, comment) } } catch (error) { toast({ diff --git a/src/services/lightning.service.ts b/src/services/lightning.service.ts index 33669cb..2012b18 100644 --- a/src/services/lightning.service.ts +++ b/src/services/lightning.service.ts @@ -48,7 +48,7 @@ class LightningService { comment: string, eventId?: string, closeOuterModel?: () => void - ): Promise<{ preimage: string; invoice: string }> { + ): Promise<{ preimage: string; invoice: string } | null> { if (!client.signer) { throw new Error('You need to be logged in to zap') } @@ -112,6 +112,7 @@ class LightningService { onCancelled: () => { clearInterval(checkPaymentInterval) subCloser?.close() + resolve(null) } })