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(() => { if (authorPubkeys.length === 0) { setProfiles(new Map()) setLoading(false) return } const loadProfiles = async () => { setLoading(true) const profilesMap = new Map() const profilePromises = authorPubkeys.map(async (pubkey) => { try { const profile = await nostrService.getProfile(pubkey) return { pubkey, profile: profile ?? { pubkey }, } } catch (error) { console.error(`Error loading profile for ${pubkey}:`, error) return { pubkey, profile: { pubkey }, } } }) const results = await Promise.all(profilePromises) results.forEach(({ pubkey, profile }) => { profilesMap.set(pubkey, profile) }) setProfiles(profilesMap) setLoading(false) } void loadProfiles() }, [pubkeysKey]) return { profiles, loading } }