polish
This commit is contained in:
@@ -36,10 +36,17 @@ export function useNostr() {
|
||||
[]
|
||||
);
|
||||
|
||||
const sub = async (filter: NDKFilter, callback: (event: NDKEvent) => void) => {
|
||||
const sub = async (
|
||||
filter: NDKFilter,
|
||||
callback: (event: NDKEvent) => void,
|
||||
groupable?: boolean
|
||||
) => {
|
||||
if (!ndk) throw new Error('NDK instance not found');
|
||||
|
||||
const subEvent = ndk.subscribe(filter, { closeOnEose: false });
|
||||
const subEvent = ndk.subscribe(filter, {
|
||||
closeOnEose: false,
|
||||
groupable: groupable ?? true,
|
||||
});
|
||||
subManager.set(JSON.stringify(filter), subEvent);
|
||||
|
||||
subEvent.addListener('event', (event: NDKEvent) => {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user