/* eslint-disable @typescript-eslint/no-explicit-any */ import BaseLayout from '@layouts/baseLayout'; import FullLayout from '@layouts/fullLayout'; import { currentUser } from '@stores/currentUser'; import { follows } from '@stores/follows'; import LumeSymbol from '@assets/icons/Lume'; 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'; import Database from 'tauri-plugin-sql-api'; const db = typeof window !== 'undefined' ? await Database.load('sqlite:lume.db') : null; export default function Page() { const router = useRouter(); const [loading, setLoading] = useState(true); const requestNotification = 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'; } if (permissionGranted) { sendNotification({ title: 'Lume', body: 'Nostr is awesome' }); } return permissionGranted; }, []); const getAccount = useCallback(async () => { const result = await db.select(`SELECT * FROM accounts ASC LIMIT 1`); return result; }, []); const getFollows = useCallback(async (account) => { const arr = []; const result: any = await db.select(`SELECT pubkey FROM follows WHERE account = "${account.id}"`); result.forEach((item: { pubkey: string }) => { arr.push(item.pubkey); }); return arr; }, []); // Explain: // Step 1: request allow notification from system // Step 2: get first account. #TODO: get last used account instead (part of multi account feature) // Step 3: get follows by account useEffect(() => { requestNotification().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); }); }, [getAccount, getFollows, requestNotification, router]); return (
{/* spacer */}
A censorship-resistant social network built on nostr
{loading ? ( ) : ( <> )}
{/* background */}
{/* end background */}
); } Page.getLayout = function getLayout( page: string | number | boolean | ReactElement> | ReactFragment | ReactPortal ) { return ( {page} ); };