import { NDKEvent, NDKKind, NDKUser } from '@nostr-dev-kit/ndk'; import * as Dialog from '@radix-ui/react-dialog'; import { memo, useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { toast } from 'sonner'; import { useNDK } from '@libs/ndk/provider'; import { useStorage } from '@libs/storage/provider'; import { NIP05 } from '@shared/nip05'; import { TextNote } from '@shared/notes'; import { User } from '@shared/user'; import { useProfile } from '@utils/hooks/useProfile'; import { displayNpub } from '@utils/shortenKey'; import { UserLatestPosts } from './userLatestPosts'; export const UserWithDrawer = memo(function UserWithDrawer({ pubkey, }: { pubkey: string; }) { const { db } = useStorage(); const { ndk } = useNDK(); const { status, user } = useProfile(pubkey); 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(pubkey)) { setFollowed(true); } }, []); return (
{status === 'pending' ? (

Loading...

) : ( <>
{pubkey}
{user?.name || user?.display_name || user?.displayName}
{user?.nip05 ? ( ) : ( {displayNpub(pubkey, 16)} )}
{user?.about ? : null}
{followed ? ( ) : ( )} Message
)}
); });