diff --git a/src/components/user/base.tsx b/src/components/user/base.tsx index b19d8d4b..1ab3ca59 100644 --- a/src/components/user/base.tsx +++ b/src/components/user/base.tsx @@ -1,13 +1,13 @@ -import { DatabaseContext } from '@components/contexts/database'; import { ImageWithFallback } from '@components/imageWithFallback'; +import { createCacheProfile } from '@utils/storage'; import { truncate } from '@utils/truncate'; import { fetch } from '@tauri-apps/api/http'; -import { memo, useCallback, useContext, useEffect, useState } from 'react'; +import destr from 'destr'; +import { memo, useCallback, useEffect, useState } from 'react'; export const UserBase = memo(function UserBase({ pubkey }: { pubkey: string }) { - const { db }: any = useContext(DatabaseContext); const [profile, setProfile] = useState(null); const fetchProfile = useCallback(async (id: string) => { @@ -15,24 +15,17 @@ export const UserBase = memo(function UserBase({ pubkey }: { pubkey: string }) { method: 'GET', timeout: 30, }); - return res; + return res.data; }, []); - const cacheProfile = useCallback( - async (event) => { - // insert to database - await db.execute('INSERT OR IGNORE INTO cache_profiles (id, metadata) VALUES (?, ?);', [pubkey, event.content]); - // update state - setProfile(JSON.parse(event.content)); - }, - [db, pubkey] - ); - useEffect(() => { fetchProfile(pubkey) - .then((res) => cacheProfile(res)) + .then((res: any) => { + setProfile(destr(res.content)); + createCacheProfile(res.pubkey, res.content); + }) .catch(console.error); - }, [fetchProfile, cacheProfile, pubkey]); + }, [fetchProfile, pubkey]); return (
@@ -41,7 +34,7 @@ export const UserBase = memo(function UserBase({ pubkey }: { pubkey: string }) { )}
-
+
{profile?.display_name || profile?.name} {truncate(pubkey, 16, ' .... ')}
diff --git a/src/pages/onboarding/create/index.tsx b/src/pages/onboarding/create/index.tsx index 852f7f70..e31f934b 100644 --- a/src/pages/onboarding/create/index.tsx +++ b/src/pages/onboarding/create/index.tsx @@ -1,14 +1,13 @@ import BaseLayout from '@layouts/base'; -import { DatabaseContext } from '@components/contexts/database'; -import { RelayContext } from '@components/contexts/relay'; +import { pool } from '@utils/pool'; +import { createAccount, getAllRelays } from '@utils/storage'; import { EyeClosedIcon, EyeOpenIcon } from '@radix-ui/react-icons'; -import { useLocalStorage, writeStorage } from '@rehooks/local-storage'; import Image from 'next/image'; import { useRouter } from 'next/router'; import { generatePrivateKey, getEventHash, getPublicKey, nip19, signEvent } from 'nostr-tools'; -import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useContext, useMemo, useState } from 'react'; +import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useMemo, useState } from 'react'; import { Config, names, uniqueNamesGenerator } from 'unique-names-generator'; const config: Config = { @@ -18,11 +17,6 @@ const config: Config = { export default function Page() { const router = useRouter(); - const { db }: any = useContext(DatabaseContext); - const relayPool: any = useContext(RelayContext); - - const [relays] = useLocalStorage('relays'); - const [type, setType] = useState('password'); const [loading, setLoading] = useState(false); @@ -33,16 +27,8 @@ export default function Page() { const npub = nip19.npubEncode(pubKey); const nsec = nip19.nsecEncode(privKey); - // toggle privatek key - const showPrivateKey = () => { - if (type === 'password') { - setType('text'); - } else { - setType('password'); - } - }; - // auto-generated profile - const data = useMemo( + // auto-generated profile metadata + const metadata = useMemo( () => ({ display_name: name, name: name, @@ -52,23 +38,35 @@ export default function Page() { }), [name] ); - // insert to database - const insertDB = async () => { - await db.execute('INSERT OR IGNORE INTO accounts (id, privkey, npub, nsec, metadata) VALUES (?, ?, ?, ?, ?)', [ - pubKey, - privKey, - npub, - nsec, - data, - ]); + + // build profile + const data = useMemo( + () => ({ + pubkey: pubKey, + privkey: privKey, + npub: npub, + nsec: nsec, + metadata: metadata, + }), + [metadata, npub, nsec, privKey, pubKey] + ); + + // toggle privatek key + const showPrivateKey = () => { + if (type === 'password') { + setType('text'); + } else { + setType('password'); + } }; - // build event and broadcast to all relays - const createAccount = () => { + + // create account and broadcast to all relays + const submit = () => { setLoading(true); // build event const event: any = { - content: JSON.stringify(data), + content: JSON.stringify(metadata), created_at: Math.floor(Date.now() / 1000), kind: 0, pubkey: pubKey, @@ -76,23 +74,20 @@ export default function Page() { }; event.id = getEventHash(event); event.sig = signEvent(event, privKey); + // insert to database then broadcast - insertDB() + createAccount(data) .then(() => { - // publish to relays - relayPool.publish(event, relays); - // set currentUser in global state - writeStorage('current-user', { - metadata: JSON.stringify(data), - npub: npub, - privkey: privKey, - id: pubKey, - }); - // redirect to pre-follow - setTimeout(() => { - setLoading(false); - router.push('/onboarding/create/step-2'); - }, 1500); + getAllRelays() + .then((res) => { + // publish to relays + pool(res).publish(event, res); + router.push({ + pathname: '/onboarding/create/step-2', + query: { id: pubKey, privkey: privKey }, + }); + }) + .catch(console.error); }) .catch(console.error); }; @@ -146,12 +141,12 @@ export default function Page() {
- +
-

{data.display_name}

-

@{data.username}

+

{metadata.display_name}

+

@{metadata.username}

@@ -183,7 +178,7 @@ export default function Page() { ) : (