import { NDKEvent, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk"; import { Image } from "@shared/image"; import { RelayContext } from "@shared/relayProvider"; import { useActiveAccount } from "@stores/accounts"; import { DEFAULT_AVATAR } from "@stores/constants"; import { useQuery } from "@tanstack/react-query"; import { dateToUnix } from "@utils/date"; import { usePageContext } from "@utils/hooks/usePageContext"; import { useProfile } from "@utils/hooks/useProfile"; import { compactNumber } from "@utils/number"; import { shortenKey } from "@utils/shortenKey"; import { useContext } from "react"; import { Link } from "react-router-dom"; export function UserScreen() { const ndk = useContext(RelayContext); const pageContext = usePageContext(); const searchParams: any = pageContext.urlParsed.search; const pubkey = searchParams.pubkey || ""; const { user } = useProfile(pubkey); const { data: userStats, error } = useQuery(["user", pubkey], async () => { const res = await fetch( `https://api.nostr.band/v0/stats/profile/${pubkey}`, ); if (res.ok) { return await res.json(); } }); const account = useActiveAccount((state: any) => state.account); const follows = account ? JSON.parse(account.follows) : []; const follow = (pubkey: string) => { try { const followsAsSet = new Set(follows); followsAsSet.add(pubkey); const signer = new NDKPrivateKeySigner(account.privkey); ndk.signer = signer; const tags = []; followsAsSet.forEach((item) => { tags.push(["p", item]); }); const event = new NDKEvent(ndk); event.content = ""; event.created_at = dateToUnix(); event.pubkey = pubkey; event.kind = 3; event.tags = tags; // publish event event.publish(); } catch (error) { console.log(error); } }; const unfollow = (pubkey: string) => { try { const followsAsSet = new Set(follows); followsAsSet.delete(pubkey); const signer = new NDKPrivateKeySigner(account.privkey); ndk.signer = signer; const tags = []; followsAsSet.forEach((item) => { tags.push(["p", item]); }); const event = new NDKEvent(ndk); event.content = ""; event.created_at = dateToUnix(); event.pubkey = pubkey; event.kind = 3; event.tags = tags; // publish event event.publish(); } catch (error) { console.log(error); } }; return (
{"banner"}
{pubkey}
{user?.displayName || user?.name || "No name"}
{user?.nip05 || shortenKey(pubkey)}

{user?.about}

{error &&

Failed to fetch user stats

} {!userStats ? (

Loading...

) : (
{userStats.stats[pubkey].followers_pubkey_count ?? 0} Followers
{userStats.stats[pubkey].pub_following_pubkey_count ?? 0} Following
{userStats.stats[pubkey].zaps_received ? compactNumber.format( userStats.stats[pubkey].zaps_received.msats / 1000, ) : 0} Zaps received
{userStats.stats[pubkey].zaps_sent ? compactNumber.format( userStats.stats[pubkey].zaps_sent.msats / 1000, ) : 0} Zaps sent
)}
{follows.includes(pubkey) ? ( ) : ( )} Message
); }