import { useEffect, useState, useMemo } from 'react' import type { NostrProfile } from '@/types/nostr' import { nostrService } from '@/lib/nostr' interface AuthorProfile extends NostrProfile { pubkey: string } export function useAuthorsProfiles(authorPubkeys: string[]): { profiles: Map loading: boolean } { const [profiles, setProfiles] = useState>(new Map()) const [loading, setLoading] = useState(true) const pubkeysKey = useMemo(() => [...authorPubkeys].sort().join(','), [authorPubkeys]) useEffect(() => { void loadAndSetProfiles({ authorPubkeys, setProfiles, setLoading }) }, [pubkeysKey, authorPubkeys]) return { profiles, loading } } async function loadAndSetProfiles(params: { authorPubkeys: string[] setProfiles: (value: Map) => void setLoading: (value: boolean) => void }): Promise { if (params.authorPubkeys.length === 0) { params.setProfiles(new Map()) params.setLoading(false) return } params.setLoading(true) const profilesMap = await loadProfilesMap(params.authorPubkeys) params.setProfiles(profilesMap) params.setLoading(false) } async function loadProfilesMap(authorPubkeys: string[]): Promise> { const results = await Promise.all(authorPubkeys.map(loadSingleProfile)) const map = new Map() results.forEach(({ pubkey, profile }) => { map.set(pubkey, profile) }) return map } async function loadSingleProfile(pubkey: string): Promise<{ pubkey: string; profile: AuthorProfile }> { try { const profile = await nostrService.getProfile(pubkey) return { pubkey, profile: ensureAuthorProfile(pubkey, profile) } } catch (loadError) { console.error(`Error loading profile for ${pubkey}:`, loadError) return { pubkey, profile: { pubkey } } } } function ensureAuthorProfile(pubkey: string, profile: NostrProfile | null): AuthorProfile { if (!profile) { return { pubkey } } return { ...profile, pubkey } }