import { getProfileFromProfileEvent } from '@/lib/event' import { userIdToPubkey } from '@/lib/pubkey' import { useNostr } from '@/providers/NostrProvider' import client from '@/services/client.service' import indexedDb from '@/services/indexed-db.service' import { TProfile } from '@/types' import { kinds } from 'nostr-tools' import { useEffect, useState } from 'react' export function useFetchProfile(id?: string) { const { profile: currentAccountProfile } = useNostr() const [isFetching, setIsFetching] = useState(true) const [error, setError] = useState(null) const [profile, setProfile] = useState(null) const [pubkey, setPubkey] = useState(null) useEffect(() => { setProfile(null) setPubkey(null) const fetchProfile = async () => { setIsFetching(true) try { if (!id) { setIsFetching(false) setError(new Error('No id provided')) return } const pubkey = userIdToPubkey(id) setPubkey(pubkey) const storedProfileEvent = await indexedDb.getReplaceableEvent(pubkey, kinds.Metadata) if (storedProfileEvent) { const profile = getProfileFromProfileEvent(storedProfileEvent) setProfile(profile) setIsFetching(false) return } const profile = await client.fetchProfile(id) if (profile) { setProfile(profile) } } catch (err) { setError(err as Error) } finally { setIsFetching(false) } } fetchProfile() }, [id]) useEffect(() => { if (currentAccountProfile && pubkey === currentAccountProfile.pubkey) { setProfile(currentAccountProfile) } }, [currentAccountProfile]) return { isFetching, error, profile } }