import { type NostrEvent, type NostrMetadata, NSchema as n } from '@nostrify/nostrify'; import { useNostr } from '@nostrify/react'; import { useQuery } from '@tanstack/react-query'; export function useAuthor(pubkey: string | undefined) { const { nostr } = useNostr(); return useQuery<{ event?: NostrEvent; metadata?: NostrMetadata }>({ queryKey: ['author', pubkey ?? ''], queryFn: async ({ signal }) => { if (!pubkey) { return {}; } const [event] = await nostr.query( [{ kinds: [0], authors: [pubkey!], limit: 1 }], { signal: AbortSignal.any([signal, AbortSignal.timeout(1500)]) }, ); if (!event) { throw new Error('No event found'); } try { const metadata = n.json().pipe(n.metadata()).parse(event.content); return { metadata, event }; } catch { return { event }; } }, retry: 3, }); }