diff --git a/src/libs/ndk/instance.ts b/src/libs/ndk/instance.ts index c4b6b479..79efa0c6 100644 --- a/src/libs/ndk/instance.ts +++ b/src/libs/ndk/instance.ts @@ -64,14 +64,17 @@ export const NDKInstance = () => { const bunker = !!parseInt(bunkerSetting); const outbox = !!parseInt(outboxSetting); + // #TODO: user can config outbox relays + const outboxRelayUrls = normalizeRelayUrlSet(['wss://purplepag.es/']); + const tauriAdapter = new NDKCacheAdapterTauri(db); const instance = new NDK({ explicitRelayUrls, - cacheAdapter: tauriAdapter, - outboxRelayUrls: ['wss://purplepag.es'], + outboxRelayUrls, enableOutboxModel: outbox, autoConnectUserRelays: true, autoFetchUserMutelist: true, + cacheAdapter: tauriAdapter, // clientName: 'Lume', // clientNip89: '', }); diff --git a/src/utils/hooks/useProfile.ts b/src/utils/hooks/useProfile.ts index 0920c2dc..ef7a17b6 100644 --- a/src/utils/hooks/useProfile.ts +++ b/src/utils/hooks/useProfile.ts @@ -1,11 +1,10 @@ import { NDKUserProfile } from '@nostr-dev-kit/ndk'; -import { useQuery, useQueryClient } from '@tanstack/react-query'; +import { useQuery } from '@tanstack/react-query'; import { nip19 } from 'nostr-tools'; import { useNDK } from '@libs/ndk/provider'; export function useProfile(pubkey: string, embed?: string) { - const queryClient = useQueryClient(); const { ndk } = useNDK(); const { isFetching, @@ -14,31 +13,38 @@ export function useProfile(pubkey: string, embed?: string) { } = useQuery({ queryKey: ['user', pubkey], queryFn: async () => { - // parse data from nostr.band api - if (embed) { - const profile: NDKUserProfile = JSON.parse(embed); + try { + // parse data from nostr.band api + if (embed) { + const profile: NDKUserProfile = JSON.parse(embed); + return profile; + } + + // get clean pubkey without any special characters + let hexstring = pubkey.replace(/[^a-zA-Z0-9]/g, ''); + + if (hexstring.startsWith('npub1') || hexstring.startsWith('nprofile1')) { + const decoded = nip19.decode(hexstring); + if (decoded.type === 'nprofile') hexstring = decoded.data.pubkey; + if (decoded.type === 'npub') hexstring = decoded.data; + } + + const user = ndk.getUser({ pubkey: hexstring }); + const profile = await user.fetchProfile(); + + if (!profile) + throw new Error( + `Cannot get metadata for ${pubkey}, will be retry after 10 seconds` + ); + return profile; - } - - // get clean pubkey without any special characters - let hexstring = pubkey.replace(/[^a-zA-Z0-9]/g, ''); - - if (hexstring.startsWith('npub1') || hexstring.startsWith('nprofile1')) { - const decoded = nip19.decode(hexstring); - if (decoded.type === 'nprofile') hexstring = decoded.data.pubkey; - if (decoded.type === 'npub') hexstring = decoded.data; - } - - const user = ndk.getUser({ pubkey: hexstring }); - const profile = await user.fetchProfile(); - - if (!profile) + } catch (e) { + console.error(e); throw new Error( `Cannot get metadata for ${pubkey}, will be retry after 10 seconds` ); - return profile; + } }, - initialData: () => queryClient.getQueryData(['user', pubkey]) as NDKUserProfile, refetchOnWindowFocus: false, refetchOnReconnect: false, retry: 2,