import { NDKEvent, NDKKind, NDKUser } from '@nostr-dev-kit/ndk'; import { useQuery } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; import { toast } from 'sonner'; import { useNDK } from '@libs/ndk/provider'; import { useStorage } from '@libs/storage/provider'; import { FollowIcon, UnfollowIcon } from '@shared/icons'; import { Image } from '@shared/image'; import { compactNumber } from '@utils/number'; import { shortenKey } from '@utils/shortenKey'; export interface Profile { pubkey: string; profile: { content: string }; } export function NostrBandUserProfile({ data }: { data: Profile }) { const { db } = useStorage(); const { ndk } = useNDK(); const { status, data: userStats } = useQuery({ queryKey: ['user-stats', data.pubkey], queryFn: 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 follow = async (pubkey: string) => { try { const user = ndk.getUser({ pubkey: db.account.pubkey }); const contacts = await user.follows(); const add = await user.follow(new NDKUser({ pubkey: pubkey }), contacts); if (add) { setFollowed(true); } else { toast('You already follow this user'); } } catch (error) { console.log(error); } }; const unfollow = async (pubkey: string) => { try { const user = ndk.getUser({ pubkey: db.account.pubkey }); const contacts = await user.follows(); contacts.delete(new NDKUser({ pubkey: pubkey })); let list: string[][]; contacts.forEach((el) => list.push(['p', el.pubkey, el.relayUrls?.[0] || '', ''])); const event = new NDKEvent(ndk); event.content = ''; event.kind = NDKKind.Contacts; event.tags = list; const publishedRelays = await event.publish(); if (publishedRelays) { setFollowed(false); } } catch (error) { console.log(error); } }; useEffect(() => { if (db.account.follows.includes(data.pubkey)) { setFollowed(true); } }, []); if (!profile) { return (
Can't fetch profile
{profile.nip05 || shortenKey(data.pubkey)}
{profile.about || profile.bio}
Loading...
) : (