From 670b981b05ec39c5a8fa531d5f895f64fabd3a92 Mon Sep 17 00:00:00 2001 From: en Date: Mon, 27 Jan 2025 14:24:48 +0100 Subject: [PATCH] fix(zap): add timeout and hide loading when done --- src/components/Zap.tsx | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/components/Zap.tsx b/src/components/Zap.tsx index efc0d8c..5a1fe9f 100644 --- a/src/components/Zap.tsx +++ b/src/components/Zap.tsx @@ -21,6 +21,7 @@ import { getZapAmount, log, LogType, + timeout, unformatNumber } from '../utils' import { LoadingSpinner } from './LoadingSpinner' @@ -303,12 +304,15 @@ export const ZapPopUp = ({ if (!receiverMetadata?.pubkey) { setIsLoading(false) - toast.error('pubkey is missing in receiver metadata!') + toast.error('Pubkey is missing in receiver metadata!') return null } // Find the receiver's read relays. - const receiverRelays = await getRelayListForUser(receiver, ndk) + const receiverRelays = await Promise.race([ + getRelayListForUser(receiver, ndk), + timeout(2000) + ]) .then((ndkRelayList) => { if (ndkRelayList) return ndkRelayList.readRelayUrls return [] // Return an empty array if ndkRelayList is undefined @@ -554,7 +558,7 @@ export const ZapSplit = ({ const generatePaymentInvoices = async () => { if (!amount) return null - let userHexKey: string + let userHexKey: string | undefined setIsLoading(true) setLoadingSpinnerDesc('Getting user pubkey') @@ -562,7 +566,11 @@ export const ZapSplit = ({ if (userState.auth && userState.user?.pubkey) { userHexKey = userState.user.pubkey as string } else { - userHexKey = (await window.nostr?.getPublicKey()) as string + try { + userHexKey = (await window.nostr?.getPublicKey()) as string + } catch (error) { + log(true, LogType.Error, `Could not get pubkey`, error) + } } if (!userHexKey) { @@ -620,7 +628,11 @@ export const ZapSplit = ({ if (adminShare > 0 && admin?.pubkey && admin?.lud16) { // Find the receiver's read relays. - const adminRelays = await getRelayListForUser(admin.pubkey as string, ndk) + // TODO: NDK should have native timeout in a future release + const adminRelays = await Promise.race([ + getRelayListForUser(admin.pubkey as string, ndk), + timeout(2000) + ]) .then((ndkRelayList) => { if (ndkRelayList) return ndkRelayList.readRelayUrls return [] // Return an empty array if ndkRelayList is undefined @@ -721,6 +733,8 @@ export const ZapSplit = ({ toast.warn('Webln is not present. Use QR code to send zap.') setInvoices(paymentInvoices) } + + setIsLoading(false) } const removeInvoice = (key: string) => {