diff --git a/src/components/ProfileSection.tsx b/src/components/ProfileSection.tsx index b8f0092..227a945 100644 --- a/src/components/ProfileSection.tsx +++ b/src/components/ProfileSection.tsx @@ -383,7 +383,12 @@ const FollowButton = ({ pubkey }: FollowButtonProps) => { if (userState.auth && userState.user?.pubkey) { return userState.user.pubkey as string } else { - return (await window.nostr?.getPublicKey()) as string + try { + return (await window.nostr?.getPublicKey()) as string + } catch (error) { + log(true, LogType.Error, `Could not get pubkey`, error) + return null + } } } diff --git a/src/components/Zap.tsx b/src/components/Zap.tsx index c393947..5a1fe9f 100644 --- a/src/components/Zap.tsx +++ b/src/components/Zap.tsx @@ -19,6 +19,9 @@ import { formatNumber, getTagValue, getZapAmount, + log, + LogType, + timeout, unformatNumber } from '../utils' import { LoadingSpinner } from './LoadingSpinner' @@ -268,7 +271,7 @@ export const ZapPopUp = ({ const generatePaymentRequest = useCallback(async (): Promise => { - let userHexKey: string + let userHexKey: string | undefined setIsLoading(true) setLoadingSpinnerDesc('Getting user pubkey') @@ -276,7 +279,11 @@ export const ZapPopUp = ({ 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) { @@ -285,7 +292,7 @@ export const ZapPopUp = ({ return null } - setLoadingSpinnerDesc('finding receiver metadata') + setLoadingSpinnerDesc('Finding receiver metadata') const receiverMetadata = await findMetadata(receiver) @@ -297,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 @@ -548,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') @@ -556,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) { @@ -614,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 @@ -715,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) => { diff --git a/src/components/comment/index.tsx b/src/components/comment/index.tsx index b9a87b3..acb7c0f 100644 --- a/src/components/comment/index.tsx +++ b/src/components/comment/index.tsx @@ -78,12 +78,16 @@ export const Comments = ({ addressable, setCommentCount }: Props) => { const handleSubmit = async (content: string): Promise => { if (content === '') return false - let pubkey: string + let pubkey: string | undefined if (userState.auth && userState.user?.pubkey) { pubkey = userState.user.pubkey as string } else { - pubkey = (await window.nostr?.getPublicKey()) as string + try { + pubkey = (await window.nostr?.getPublicKey()) as string + } catch (error) { + log(true, LogType.Error, `Could not get pubkey`, error) + } } if (!pubkey) { diff --git a/src/controllers/image/nostrcheck-server.ts b/src/controllers/image/nostrcheck-server.ts index fec22f3..288474f 100644 --- a/src/controllers/image/nostrcheck-server.ts +++ b/src/controllers/image/nostrcheck-server.ts @@ -2,7 +2,7 @@ import axios, { isAxiosError } from 'axios' import { NostrEvent, NDKKind } from '@nostr-dev-kit/ndk' import { type MediaOperations } from '.' import { store } from 'store' -import { now } from 'utils' +import { log, LogType, now } from 'utils' import { BaseError, handleError } from 'types' // https://github.com/quentintaranpino/nostrcheck-server/blob/main/DOCS.md#media-post @@ -124,12 +124,16 @@ export class NostrCheckServer implements MediaOperations { try { const url = `${this.#url}${this.#media}` - let hexPubkey: string + let hexPubkey: string | undefined const userState = store.getState().user if (userState.auth && userState.user?.pubkey) { hexPubkey = userState.user.pubkey as string } else { - hexPubkey = (await window.nostr?.getPublicKey()) as string + try { + hexPubkey = (await window.nostr?.getPublicKey()) as string + } catch (error) { + log(true, LogType.Error, `Could not get pubkey`, error) + } } if (!hexPubkey) { diff --git a/src/hooks/useReactions.ts b/src/hooks/useReactions.ts index 0f029bb..0735949 100644 --- a/src/hooks/useReactions.ts +++ b/src/hooks/useReactions.ts @@ -70,12 +70,16 @@ export const useReactions = (params: UseReactionsParams) => { }, [reactionEvents, userState]) const getPubkey = async () => { - let hexPubkey: string + let hexPubkey: string | undefined if (userState.auth && userState.user?.pubkey) { hexPubkey = userState.user.pubkey as string } else { - hexPubkey = (await window.nostr?.getPublicKey()) as string + try { + hexPubkey = (await window.nostr?.getPublicKey()) as string + } catch (error) { + log(true, LogType.Error, `Could not get pubkey`, error) + } } if (!hexPubkey) { diff --git a/src/pages/blog/action.ts b/src/pages/blog/action.ts index 696fcdd..9c7045f 100644 --- a/src/pages/blog/action.ts +++ b/src/pages/blog/action.ts @@ -33,11 +33,15 @@ export const blogRouteAction = } const userState = store.getState().user - let hexPubkey: string + let hexPubkey: string | undefined if (userState.auth && userState.user?.pubkey) { hexPubkey = userState.user.pubkey as string } else { - hexPubkey = (await window.nostr?.getPublicKey()) as string + try { + hexPubkey = (await window.nostr?.getPublicKey()) as string + } catch (error) { + log(true, LogType.Error, `Could not get pubkey`, error) + } } if (!hexPubkey) { diff --git a/src/pages/mod/action.ts b/src/pages/mod/action.ts index 5f7f477..aac8cdd 100644 --- a/src/pages/mod/action.ts +++ b/src/pages/mod/action.ts @@ -43,11 +43,15 @@ export const modRouteAction = } const userState = store.getState().user - let hexPubkey: string + let hexPubkey: string | undefined if (userState.auth && userState.user?.pubkey) { hexPubkey = userState.user.pubkey as string } else { - hexPubkey = (await window.nostr?.getPublicKey()) as string + try { + hexPubkey = (await window.nostr?.getPublicKey()) as string + } catch (error) { + log(true, LogType.Error, `Could not get pubkey`, error) + } } if (!hexPubkey) { diff --git a/src/pages/profile/index.tsx b/src/pages/profile/index.tsx index 5aacbbc..478257c 100644 --- a/src/pages/profile/index.tsx +++ b/src/pages/profile/index.tsx @@ -30,6 +30,8 @@ import { copyTextToClipboard, DEFAULT_FILTER_OPTIONS, extractBlogCardDetails, + log, + LogType, now, npubToHex, scrollIntoView, @@ -72,7 +74,7 @@ export const ProfilePage = () => { return } - let userHexKey: string + let userHexKey: string | undefined setIsLoading(true) setLoadingSpinnerDesc('Getting user pubkey') @@ -80,7 +82,11 @@ export const ProfilePage = () => { 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) { @@ -512,11 +518,15 @@ const ReportUserPopup = ({ setIsLoading(true) setLoadingSpinnerDesc('Getting user pubkey') - let userHexKey: string + let userHexKey: string | undefined 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) { diff --git a/src/pages/settings/preference.tsx b/src/pages/settings/preference.tsx index 7f6c8e0..0fee4da 100644 --- a/src/pages/settings/preference.tsx +++ b/src/pages/settings/preference.tsx @@ -55,12 +55,16 @@ export const PreferencesSetting = () => { const handleSave = async () => { setIsSaving(true) - let hexPubkey: string + let hexPubkey: string | undefined if (user?.pubkey) { hexPubkey = user.pubkey as string } else { - hexPubkey = (await window.nostr?.getPublicKey()) as string + try { + hexPubkey = (await window.nostr?.getPublicKey()) as string + } catch (error) { + log(true, LogType.Error, `Could not get pubkey`, error) + } } if (!hexPubkey) {