improve notification and performance

This commit is contained in:
2023-11-30 16:02:28 +07:00
parent 6f68c2762b
commit 80f675cb54
11 changed files with 356 additions and 291 deletions

View File

@@ -1,10 +1,11 @@
import { NDKUserProfile } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { useQuery, useQueryClient } 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 {
status,
@@ -13,24 +14,34 @@ 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);
return profile;
}
// get clean pubkey without any special characters
let hexstring = pubkey.replace(/[^a-zA-Z0-9]/g, '');
if (hexstring.startsWith('npub1'))
hexstring = nip19.decode(hexstring).data as string;
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 });
if (!user) return Promise.reject(new Error("user's profile not found"));
const profile = await user.fetchProfile();
return await user.fetchProfile();
if (!profile)
throw new Error(
`Cannot get metadata for ${pubkey}, will be retry after 10 seconds`
);
return profile;
},
staleTime: Infinity,
refetchOnMount: false,
initialData: () => queryClient.getQueryData(['user', pubkey]),
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: 2,
});
return { status, user, error };