This commit is contained in:
Ren Amamiya
2023-09-26 09:05:39 +07:00
parent a66770989b
commit 236131087a
21 changed files with 605 additions and 523 deletions

View File

@@ -1,4 +1,4 @@
import { NDKKind, NDKUserProfile } from '@nostr-dev-kit/ndk';
import { NDKUserProfile } from '@nostr-dev-kit/ndk';
import { useQuery } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
@@ -14,30 +14,20 @@ export function useProfile(pubkey: string, embed?: string) {
} = useQuery(
['user', pubkey],
async () => {
if (!embed) {
const cleanPubkey = pubkey.replace('-', '');
const dbEvent = await db.getMetadataByPubkey(cleanPubkey);
if (dbEvent) {
return JSON.parse(dbEvent.content) as NDKUserProfile;
}
const events = await ndk.fetchEvents(
{
kinds: [NDKKind.Metadata],
authors: [cleanPubkey],
limit: 100,
},
{ closeOnEose: true }
);
if (!events) throw new Error(`User not found: ${pubkey}`);
const latestEvent = [...events].sort((a, b) => b.created_at - a.created_at)[0];
await db.createMetadata(latestEvent);
return JSON.parse(latestEvent.content) as NDKUserProfile;
} else {
if (embed) {
const profile: NDKUserProfile = JSON.parse(embed);
return profile;
}
const cleanPubkey = pubkey.replace('-', '');
const user = ndk.getUser({ hexpubkey: cleanPubkey });
const profile = await user.fetchProfile({ closeOnEose: true });
if (!user.profile) return Promise.reject(new Error('profile not found'));
await db.createProfile(cleanPubkey, profile);
return user.profile;
},
{
enabled: !!ndk,