update useProfile hook

This commit is contained in:
Ren Amamiya
2023-07-10 14:20:36 +07:00
parent 4e36b2d902
commit 2a4c3dd7c8
8 changed files with 19 additions and 12 deletions

View File

@@ -1,3 +1,4 @@
import { NDKFilter } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
@@ -17,10 +18,16 @@ export function useProfile(pubkey: string, fallback?: string) {
if (cache && parseInt(cache.created_at) + 86400 >= current) {
return cache;
} else {
const user = ndk.getUser({ hexpubkey: pubkey });
await user.fetchProfile();
await createMetadata(pubkey, pubkey, JSON.stringify(user.profile));
return user.profile;
const filter: NDKFilter = { kinds: [0], authors: [pubkey] };
const events = await ndk.fetchEvents(filter);
const latest = [...events].slice(-1)[0];
if (latest) {
await createMetadata(pubkey, pubkey, latest.content);
return JSON.parse(latest.content);
} else {
await createMetadata(pubkey, pubkey, [...events][0].content);
return JSON.parse([...events][0].content);
}
}
} else {
const profile = JSON.parse(fallback);