import { useQuery } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; import { FollowIcon, LoaderIcon, UnfollowIcon } from '@shared/icons'; import { Image } from '@shared/image'; import { DEFAULT_AVATAR } from '@stores/constants'; import { useSocial } from '@utils/hooks/useSocial'; import { compactNumber } from '@utils/number'; import { shortenKey } from '@utils/shortenKey'; export function Profile({ data }: { data: any }) { const { status: socialStatus, userFollows, follow, unfollow } = useSocial(); const { status, data: userStats } = useQuery( ['user-stats', data.pubkey], async () => { const res = await fetch(`https://api.nostr.band/v0/stats/profile/${data.pubkey}`); return res.json(); }, { refetchOnMount: false, refetchOnReconnect: false, refetchOnWindowFocus: false, staleTime: Infinity, } ); const embedProfile = data.profile ? JSON.parse(data.profile.content) : null; const profile = embedProfile; const [followed, setFollowed] = useState(false); const followUser = (pubkey: string) => { try { follow(pubkey); // update state setFollowed(true); } catch (error) { console.log(error); } }; const unfollowUser = (pubkey: string) => { try { unfollow(pubkey); // update state setFollowed(false); } catch (error) { console.log(error); } }; useEffect(() => { if (status === 'success' && userFollows) { if (userFollows.includes(data.pubkey)) { setFollowed(true); } } }, [status]); if (!profile) { return (

Can't fetch profile

); } return (

{profile.display_name || profile.name}

{profile.nip05 || shortenKey(data.pubkey)}

{socialStatus === 'loading' ? ( ) : followed ? ( ) : ( )}

{profile.about || profile.bio}

{status === 'loading' ? (

Loading...

) : (
{userStats.stats[data.pubkey].followers_pubkey_count ?? 0} Followers
{userStats.stats[data.pubkey].pub_following_pubkey_count ?? 0} Following
{userStats.stats[data.pubkey].zaps_received ? compactNumber.format( userStats.stats[data.pubkey].zaps_received.msats / 1000 ) : 0} Zaps received
)}
); }