import BaseLayout from '@layouts/base'; import { DatabaseContext } from '@components/contexts/database'; import { RelayContext } from '@components/contexts/relay'; import { useLocalStorage } from '@rehooks/local-storage'; import Image from 'next/image'; import { useRouter } from 'next/router'; import { getPublicKey, nip19 } from 'nostr-tools'; import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useCallback, useContext, useEffect, useMemo, useState, } from 'react'; export default function Page() { const { db }: any = useContext(DatabaseContext); const relayPool: any = useContext(RelayContext); const router = useRouter(); const { privkey }: any = router.query; const [relays] = useLocalStorage('relays'); const [profile, setProfile] = useState({ picture: '', display_name: '', username: '' }); const pubkey = useMemo(() => (privkey ? getPublicKey(privkey) : null), [privkey]); // save account to database const insertAccount = useCallback( async (metadata) => { const npub = privkey ? nip19.npubEncode(pubkey) : null; const nsec = privkey ? nip19.nsecEncode(privkey) : null; // insert to database await db.execute( `INSERT OR IGNORE INTO accounts (id, privkey, npub, nsec, metadata) VALUES ("${pubkey}", "${privkey}", "${npub}", "${nsec}", '${metadata}')` ); // update state setProfile(JSON.parse(metadata)); }, [db, privkey, pubkey] ); // save follows to database const insertFollows = useCallback( async (follows) => { follows.forEach(async (item) => { if (item) { // insert to database await db.execute( `INSERT OR IGNORE INTO follows (pubkey, account, kind) VALUES ("${item[1]}", "${pubkey}", "0")` ); } }); }, [db, pubkey] ); const submit = () => { router.push('/'); }; useEffect(() => { relayPool.subscribe( [ { authors: [pubkey], kinds: [0, 3], since: 0, }, ], relays, (event: any) => { if (event.kind === 0) { insertAccount(event.content); } else { if (event.tags.length > 0) { insertFollows(event.tags); } } }, undefined, (events: any, relayURL: any) => { console.log(events, relayURL); } ); }, [insertAccount, insertFollows, pubkey, relayPool, relays]); return (

Bringing back your profile...

{profile.display_name}

·

@{profile.username}

); } Page.getLayout = function getLayout( page: | string | number | boolean | ReactElement> | ReactFragment | ReactPortal ) { return {page}; };