From 24c2ed4eb203c0e421e29b3b9a339d0aa7a5e70e Mon Sep 17 00:00:00 2001 From: reya Date: Wed, 6 Dec 2023 08:08:11 +0700 Subject: [PATCH] update --- src/app.tsx | 2 +- src/utils/hooks/useEvent.ts | 18 ++++++++++++------ src/utils/hooks/useProfile.ts | 18 ++++++++++++------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/app.tsx b/src/app.tsx index 54fef176..3f75e465 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -308,7 +308,7 @@ export default function App() { router={router} fallbackElement={
- +
} future={{ v7_startTransition: true }} diff --git a/src/utils/hooks/useEvent.ts b/src/utils/hooks/useEvent.ts index 783eef55..7617aba3 100644 --- a/src/utils/hooks/useEvent.ts +++ b/src/utils/hooks/useEvent.ts @@ -1,4 +1,4 @@ -import { NDKEvent } from '@nostr-dev-kit/ndk'; +import { NDKEvent, NDKSubscriptionCacheUsage, NostrEvent } from '@nostr-dev-kit/ndk'; import { useQuery } from '@tanstack/react-query'; import { nip19 } from 'nostr-tools'; import { AddressPointer } from 'nostr-tools/lib/types/nip19'; @@ -30,20 +30,26 @@ export function useEvent(id: undefined | string, embed?: undefined | string) { // return embed event (nostr.band api) if (embed) { - const event: NDKEvent = JSON.parse(embed); - return event; + const embedEvent: NostrEvent = JSON.parse(embed); + const ndkEvent = new NDKEvent(ndk, embedEvent); + + return ndkEvent; } // get event from relay - const event = await ndk.fetchEvent(id); + const event = await ndk.fetchEvent(id, { + cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST, + }); + + if (!event) + throw new Error(`Cannot get event with ${id}, will be retry after 10 seconds`); - if (!event) throw new Error('event not found'); return event; }, - staleTime: Infinity, refetchOnWindowFocus: false, refetchOnMount: false, refetchOnReconnect: false, + retry: 2, }); return { status, isFetching, isError, data }; diff --git a/src/utils/hooks/useProfile.ts b/src/utils/hooks/useProfile.ts index d7ebf9ad..c9b10ac9 100644 --- a/src/utils/hooks/useProfile.ts +++ b/src/utils/hooks/useProfile.ts @@ -1,4 +1,4 @@ -import { NDKUserProfile } from '@nostr-dev-kit/ndk'; +import { NDKSubscriptionCacheUsage, NDKUserProfile } from '@nostr-dev-kit/ndk'; import { useQuery } from '@tanstack/react-query'; import { nip19 } from 'nostr-tools'; @@ -30,15 +30,21 @@ export function useProfile(pubkey: string, embed?: string) { } const user = ndk.getUser({ pubkey: hexstring }); - const profile = await user.fetchProfile(); + const profile = await user.fetchProfile({ + cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST, + }); + + if (!profile) + throw new Error( + `Cannot get metadata for ${pubkey}, will be retry after 10 seconds` + ); + return profile; } catch (e) { - console.error(e); - throw new Error( - `Cannot get metadata for ${pubkey}, will be retry after 10 seconds` - ); + throw new Error(e); } }, + refetchOnMount: false, refetchOnWindowFocus: false, refetchOnReconnect: false, retry: 2,