From 247f28ae750ce86fca3e99dd00ee7e47622296b4 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Wed, 22 Feb 2023 21:12:37 +0700 Subject: [PATCH] updated onboarding flow --- src/pages/onboarding/fetch-follows.tsx | 125 +++++++++++++++++++++++++ src/pages/onboarding/fetch-profile.tsx | 77 ++++++++++----- src/pages/onboarding/import.tsx | 7 +- src/pages/onboarding/index.tsx | 2 +- 4 files changed, 182 insertions(+), 29 deletions(-) create mode 100644 src/pages/onboarding/fetch-follows.tsx diff --git a/src/pages/onboarding/fetch-follows.tsx b/src/pages/onboarding/fetch-follows.tsx new file mode 100644 index 00000000..9ff02b60 --- /dev/null +++ b/src/pages/onboarding/fetch-follows.tsx @@ -0,0 +1,125 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import BaseLayout from '@layouts/baseLayout'; +import OnboardingLayout from '@layouts/onboardingLayout'; + +import { motion } from 'framer-motion'; +import { useRouter } from 'next/router'; +import { useNostrEvents } from 'nostr-react'; +import { + JSXElementConstructor, + ReactElement, + ReactFragment, + ReactPortal, + useEffect, + useState, +} from 'react'; +import Database from 'tauri-plugin-sql-api'; + +export default function Page() { + const [follows, setFollows] = useState([null]); + const [loading, setLoading] = useState(false); + + const router = useRouter(); + const { pubkey }: any = router.query; + + const { onEvent } = useNostrEvents({ + filter: { + authors: [pubkey], + kinds: [3], + }, + }); + + onEvent((rawMetadata) => { + try { + setFollows(rawMetadata.tags); + } catch (err) { + console.error(err, rawMetadata); + } + }); + + useEffect(() => { + setLoading(true); + + const insertDB = async () => { + const db = await Database.load('sqlite:lume.db'); + follows.forEach(async (item) => { + if (item) { + await db.execute( + `INSERT INTO followings (pubkey, account) VALUES ("${item[1]}", "${pubkey}")` + ); + } + }); + }; + + if (follows !== null && follows.length > 0) { + insertDB() + .then(() => { + setTimeout(() => { + setLoading(false); + router.push('/feed/following'); + }, 1500); + }) + .catch(console.error); + } + }, [follows, pubkey, router]); + + return ( +
+
{/* spacer */}
+ +
+ + Fetching your follows... + + + Not only profile, every nostr client can sync your follows list when you move to a new + client, so please keep your key safely (again) + +
+
+ +
+ {loading === true ? ( + + + + + ) : ( + <> + )} +
+
+
+ ); +} + +Page.getLayout = function getLayout( + page: + | string + | number + | boolean + | ReactElement> + | ReactFragment + | ReactPortal +) { + return ( + + {page} + + ); +}; diff --git a/src/pages/onboarding/fetch-profile.tsx b/src/pages/onboarding/fetch-profile.tsx index d3ba1788..befbeecc 100644 --- a/src/pages/onboarding/fetch-profile.tsx +++ b/src/pages/onboarding/fetch-profile.tsx @@ -11,13 +11,14 @@ import { ReactElement, ReactFragment, ReactPortal, - useCallback, + useEffect, useState, } from 'react'; import Database from 'tauri-plugin-sql-api'; export default function Page() { const [account, setAccount] = useState(null); + const [loading, setLoading] = useState(false); const router = useRouter(); const { privkey }: any = router.query; @@ -42,19 +43,32 @@ export default function Page() { } }); - const insertAccount = useCallback(async () => { - // save account to database - const db = await Database.load('sqlite:lume.db'); - await db.execute( - `INSERT INTO accounts (privkey, pubkey, npub, nsec, metadata) VALUES ("${privkey}", "${pubkey}", "${npub}", "${nsec}", '${JSON.stringify( - account - )}')` - ); - await db.close(); + useEffect(() => { + setLoading(true); + const insertDB = async () => { + // save account to database + const db = await Database.load('sqlite:lume.db'); + await db.execute( + `INSERT INTO accounts (privkey, pubkey, npub, nsec, metadata) VALUES ("${privkey}", "${pubkey}", "${npub}", "${nsec}", '${JSON.stringify( + account + )}')` + ); + await db.close(); + }; - setTimeout(() => { - router.push('/feed/following'); - }, 500); + if (account !== null) { + insertDB() + .then(() => { + setTimeout(() => { + setLoading(false); + router.push({ + pathname: '/onboarding/fetch-follows', + query: { pubkey: pubkey }, + }); + }, 1500); + }) + .catch(console.error); + } }, [account, npub, nsec, privkey, pubkey, router]); return ( @@ -65,26 +79,37 @@ export default function Page() { - Getting your old profile + Fetching your profile... - As long as you have private key, you alway can recover your profile as well as follows - list when you move to new nostr client + As long as you have private key, you alway can sync your profile on every nostr client, + so please keep your key safely -
-

#TODO: show profile

-
-
- -
+ {loading === true ? ( + + + + + ) : ( + <> + )}
diff --git a/src/pages/onboarding/import.tsx b/src/pages/onboarding/import.tsx index 9a2b4a26..cf5e3a10 100644 --- a/src/pages/onboarding/import.tsx +++ b/src/pages/onboarding/import.tsx @@ -37,11 +37,14 @@ export default function Page() { const onSubmit = async (data: any) => { let privKey = data['key']; + if (privKey.substring(0, 4) === 'nsec') { privKey = nip19.decode(privKey).data; } + try { const pubKey = getPublicKey(privKey); + if (pubKey) { router.push({ pathname: '/onboarding/fetch-profile', @@ -75,8 +78,8 @@ export default function Page() {
{errors.key &&

{errors.key.message}

}
diff --git a/src/pages/onboarding/index.tsx b/src/pages/onboarding/index.tsx index e491ae52..0075558d 100644 --- a/src/pages/onboarding/index.tsx +++ b/src/pages/onboarding/index.tsx @@ -34,7 +34,7 @@ export default function Page() { - Login + Login with private key