'use client'; import { RelayContext } from '@components/relaysProvider'; import { DEFAULT_AVATAR } from '@stores/constants'; import { fetchProfileMetadata } from '@utils/hooks/useProfileMetadata'; import { shortenKey } from '@utils/shortenKey'; import { createAccount, createPleb, updateAccount } from '@utils/storage'; import { nip02ToArray } from '@utils/transform'; import Image from 'next/image'; import { useRouter } from 'next/navigation'; import { getPublicKey } from 'nostr-tools'; import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; export default function Page({ params }: { params: { privkey: string } }) { const router = useRouter(); const [pool, relays]: any = useContext(RelayContext); const pubkey = useMemo(() => (params.privkey ? getPublicKey(params.privkey) : null), [params.privkey]); const eose = useRef(0); const [profile, setProfile] = useState({ metadata: null }); const [done, setDone] = useState(false); const createPlebs = useCallback(async (tags: string[]) => { for (const tag of tags) { fetchProfileMetadata(tag[1]) .then((res: any) => createPleb(tag[1], res.content)) .catch(console.error); } }, []); useEffect(() => { const unsubscribe = pool.subscribe( [ { kinds: [0, 3], authors: [pubkey], }, ], relays, (event: any) => { if (event.kind === 0) { // create account createAccount(pubkey, params.privkey, event.content); // update state setProfile({ metadata: JSON.parse(event.content), }); } else { if (event.tags.length > 0) { createPlebs(event.tags); const arr = nip02ToArray(event.tags); // update account's folllows with NIP-02 tag list updateAccount('follows', arr, pubkey); } } }, undefined, () => { if (eose.current > 5) { setDone(true); } else { eose.current += 1; } }, { unsubscribeOnEose: true, logAllEvents: false, } ); return () => { unsubscribe; }; }, [pool, relays, pubkey, params.privkey, createPlebs]); // submit then redirect to home const submit = () => { router.replace('/'); }; return (

Bringing back your profile...

{profile.metadata?.display_name || profile.metadata?.name}

·

@{profile.metadata?.username || (pubkey && shortenKey(pubkey))}

{done === false ? ( ) : ( )}
); }