From 156faa2fe1880a815235c01957b2e15c1311fb98 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Sat, 25 Feb 2023 16:38:47 +0700 Subject: [PATCH 1/2] refactor initial process --- src/components/checkAccount.tsx | 88 -------------------- src/pages/index.tsx | 141 +++++++++++++++++++++----------- 2 files changed, 93 insertions(+), 136 deletions(-) delete mode 100644 src/components/checkAccount.tsx diff --git a/src/components/checkAccount.tsx b/src/components/checkAccount.tsx deleted file mode 100644 index c4d13705..00000000 --- a/src/components/checkAccount.tsx +++ /dev/null @@ -1,88 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { currentUser } from '@stores/currentUser'; -import { follows } from '@stores/follows'; - -import { useRouter } from 'next/router'; -import { useEffect, useState } from 'react'; -import Database from 'tauri-plugin-sql-api'; - -export default function CheckAccount() { - const router = useRouter(); - const [loading, setLoading] = useState(true); - - useEffect(() => { - const accounts = async () => { - const db = await Database.load('sqlite:lume.db'); - const result = await db.select( - `SELECT * FROM accounts WHERE current = "1" ORDER BY id ASC LIMIT 1` - ); - - return result; - }; - - const getFollows = async (account) => { - const db = await Database.load('sqlite:lume.db'); - const result: any = await db.select( - `SELECT pubkey FROM follows WHERE account = "${account.pubkey}"` - ); - - const arr = []; - - result.forEach((item: { pubkey: any }) => { - arr.push(item.pubkey); - }); - - return arr; - }; - - accounts() - .then((res: any) => { - if (res.length === 0) { - setTimeout(() => { - setLoading(false); - router.push('/onboarding'); - }, 1500); - } else { - currentUser.set(res[0]); - - getFollows(res[0]) - .then(async (res) => { - follows.set(res); - - setTimeout(() => { - setLoading(false); - router.push('/feed/following'); - }, 1500); - }) - .catch(console.error); - } - }) - .catch(console.error); - }, [router]); - - return ( - <> - {loading ? ( - - - - - ) : ( - <> - )} - - ); -} diff --git a/src/pages/index.tsx b/src/pages/index.tsx index a04a5410..3936f6b2 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,59 +1,106 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ import BaseLayout from '@layouts/baseLayout'; import FullLayout from '@layouts/fullLayout'; -import CheckAccount from '@components/checkAccount'; +import { currentUser } from '@stores/currentUser'; +import { follows } from '@stores/follows'; import LumeSymbol from '@assets/icons/Lume'; +import { isPermissionGranted, requestPermission } from '@tauri-apps/api/notification'; import { motion } from 'framer-motion'; -import { - JSXElementConstructor, - ReactElement, - ReactFragment, - ReactPortal, - useCallback, - useEffect, - useState, -} from 'react'; +import { useRouter } from 'next/router'; +import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useCallback, useEffect, useState } from 'react'; import Database from 'tauri-plugin-sql-api'; const db = typeof window !== 'undefined' ? await Database.load('sqlite:lume.db') : null; export default function Page() { - const [done, setDone] = useState(false); + const router = useRouter(); + const [loading, setLoading] = useState(true); const initDB = useCallback(async () => { if (db) { await db.execute( 'CREATE TABLE IF NOT EXISTS accounts (id INTEGER PRIMARY KEY, privkey TEXT NOT NULL, pubkey TEXT NOT NULL, npub TEXT, nsec TEXT, current INTEGER DEFAULT "0" NOT NULL, metadata JSON, UNIQUE(privkey));' ); - await db.execute( - 'CREATE TABLE IF NOT EXISTS follows (id INTEGER PRIMARY KEY, pubkey TEXT NOT NULL, account TEXT, UNIQUE(pubkey));' - ); + await db.execute('CREATE TABLE IF NOT EXISTS follows (id INTEGER PRIMARY KEY, pubkey TEXT NOT NULL, account TEXT, UNIQUE(pubkey));'); await db.execute( 'CREATE TABLE IF NOT EXISTS note_reactions (id INTEGER PRIMARY KEY, reaction_id TEXT NOT NULL, e TEXT, p TEXT, UNIQUE(reaction_id));' ); - await db.execute( - 'CREATE TABLE IF NOT EXISTS note_replies (id INTEGER PRIMARY KEY, reply_id TEXT NOT NULL, e TEXT, p TEXT, UNIQUE(reply_id));' - ); - await db.execute( - 'CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, event_id TEXT, event JSON, UNIQUE(event_id));' - ); - await db.execute( - 'CREATE TABLE IF NOT EXISTS cache_profiles (id INTEGER PRIMARY KEY, pubkey TEXT, metadata JSON, UNIQUE(pubkey));' - ); - await db.execute( - 'CREATE TABLE IF NOT EXISTS block_pubkeys (id INTEGER PRIMARY KEY, pubkey TEXT, UNIQUE(pubkey));' - ); + await db.execute('CREATE TABLE IF NOT EXISTS note_replies (id INTEGER PRIMARY KEY, reply_id TEXT NOT NULL, e TEXT, p TEXT, UNIQUE(reply_id));'); + await db.execute('CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY, event_id TEXT, event JSON, UNIQUE(event_id));'); + await db.execute('CREATE TABLE IF NOT EXISTS cache_profiles (id INTEGER PRIMARY KEY, pubkey TEXT, metadata JSON, UNIQUE(pubkey));'); + await db.execute('CREATE TABLE IF NOT EXISTS block_pubkeys (id INTEGER PRIMARY KEY, pubkey TEXT, UNIQUE(pubkey));'); await db.close(); } - - setDone(true); }, []); + const notification = useCallback(async () => { + // NOTE: notification don't work in dev mode (only affect MacOS) + // ref: https://github.com/tauri-apps/tauri/issues/4965 + let permissionGranted = await isPermissionGranted(); + if (!permissionGranted) { + const permission = await requestPermission(); + permissionGranted = permission === 'granted'; + } + }, []); + + const getAccount = useCallback(async () => { + const db = await Database.load('sqlite:lume.db'); + const result = await db.select(`SELECT * FROM accounts WHERE current = "1" ORDER BY id ASC LIMIT 1`); + + return result; + }, []); + + const getFollows = useCallback(async (account) => { + const arr = []; + const db = await Database.load('sqlite:lume.db'); + const result: any = await db.select(`SELECT pubkey FROM follows WHERE account = "${account.pubkey}"`); + + result.forEach((item: { pubkey: string }) => { + arr.push(item.pubkey); + }); + + return arr; + }, []); + + // Explain: + // Step 1: check DB tables, if not exist then create new table. #TODO: move this function to Rust code + // Step 2: request allow notification from system + // Step 3: get first account. #TODO: get last used account instead (part of multi account feature) + // Step 4: get follows by account useEffect(() => { - initDB().catch(console.error); - }, [initDB]); + initDB() + .then(() => notification()) + .then(() => { + getAccount() + .then((res: any) => { + if (res.length === 0) { + setTimeout(() => { + setLoading(false); + router.push('/onboarding'); + }, 1500); + } else { + // store current user in localstorage + currentUser.set(res[0]); + getFollows(res[0]) + .then(async (res) => { + // store follows in localstorage + follows.set(res); + // redirect to newsfeed + setTimeout(() => { + setLoading(false); + router.push('/feed/following'); + }, 1500); + }) + .catch(console.error); + } + }) + .catch(console.error); + }) + .catch(console.error); + }, [getAccount, getFollows, initDB, notification, router]); return (
@@ -63,9 +110,7 @@ export default function Page() {
- + A censorship-resistant social network
-
{done ? : <>}
+
+ {loading ? ( + + + + + ) : ( + <> + )} +
{/* background */}
@@ -84,13 +141,7 @@ export default function Page() { aria-hidden="true" className="dark:fill-white/2.5 absolute inset-x-0 inset-y-[-50%] h-[200%] w-full skew-y-[-18deg] fill-black/40 stroke-black/50 mix-blend-overlay dark:stroke-white/5"> - + @@ -109,13 +160,7 @@ export default function Page() { } Page.getLayout = function getLayout( - page: - | string - | number - | boolean - | ReactElement> - | ReactFragment - | ReactPortal + page: string | number | boolean | ReactElement> | ReactFragment | ReactPortal ) { return ( From 1bd890391d3eb755845dd9e5dfbcdfb7d6cb484f Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Sat, 25 Feb 2023 16:43:17 +0700 Subject: [PATCH 2/2] added notify on launch --- src/pages/index.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 3936f6b2..49db3ffb 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -7,7 +7,7 @@ import { follows } from '@stores/follows'; import LumeSymbol from '@assets/icons/Lume'; -import { isPermissionGranted, requestPermission } from '@tauri-apps/api/notification'; +import { isPermissionGranted, requestPermission, sendNotification } from '@tauri-apps/api/notification'; import { motion } from 'framer-motion'; import { useRouter } from 'next/router'; import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useCallback, useEffect, useState } from 'react'; @@ -44,6 +44,9 @@ export default function Page() { const permission = await requestPermission(); permissionGranted = permission === 'granted'; } + if (permissionGranted) { + sendNotification({ title: 'Lume', body: 'Nostr is awesome' }); + } }, []); const getAccount = useCallback(async () => {