I'm tired

This commit is contained in:
Ren Amamiya
2023-03-23 13:03:34 +07:00
parent b3baacb3e0
commit 705430a11e
6 changed files with 80 additions and 75 deletions

View File

@@ -1,7 +1,3 @@
import DatabaseProvider from '@components/contexts/database';
import RelayProvider from '@components/contexts/relay';
import { useLocalStorage } from '@rehooks/local-storage';
import { Provider } from 'jotai';
import type { NextPage } from 'next';
import type { AppProps } from 'next/app';
@@ -21,14 +17,5 @@ type AppPropsWithLayout = AppProps & {
export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout ?? ((page) => page);
// Get relays from localstorage
const [relays] = useLocalStorage('relays');
return (
<Provider>
<DatabaseProvider>
<RelayProvider relays={relays}>{getLayout(<Component {...pageProps} />)}</RelayProvider>
</DatabaseProvider>
</Provider>
);
return <Provider>{getLayout(<Component {...pageProps} />)}</Provider>;
}

View File

@@ -1,35 +1,26 @@
import BaseLayout from '@layouts/base';
import { getActiveAccount } from '@utils/storage';
import LumeSymbol from '@assets/icons/Lume';
import { useLocalStorage } from '@rehooks/local-storage';
import { useRouter } from 'next/router';
import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useEffect, useRef, useState } from 'react';
import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useEffect } from 'react';
export default function Page() {
const router = useRouter();
const [currentUser]: object[] = useLocalStorage('current-user');
const [loading, setLoading] = useState(true);
const timer = useRef(null);
useEffect(() => {
if (currentUser) {
timer.current = setTimeout(() => {
setLoading(false);
router.push('/newsfeed/following');
}, 1000);
} else {
timer.current = setTimeout(() => {
setLoading(false);
router.push('/onboarding');
}, 1000);
}
// clean up
return () => {
clearTimeout(timer.current);
};
}, [currentUser, router]);
getActiveAccount()
.then((res: any[]) => {
if (res.length > 0) {
router.push('/newsfeed/following');
} else {
router.push('/onboarding');
}
})
.catch(console.error);
}, [router]);
return (
<div className="relative h-full overflow-hidden">
@@ -47,21 +38,19 @@ export default function Page() {
</div>
</div>
<div className="absolute bottom-16 left-1/2 -translate-x-1/2 transform">
{loading && (
<svg
className="h-5 w-5 animate-spin text-black dark:text-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
></path>
</svg>
)}
<svg
className="h-5 w-5 animate-spin text-black dark:text-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
>
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
></path>
</svg>
</div>
</div>
</div>

3
src/stores/user.tsx Normal file
View File

@@ -0,0 +1,3 @@
import { atom } from 'jotai';
export const currentUser = atom({});

31
src/utils/storage.tsx Normal file
View File

@@ -0,0 +1,31 @@
import Database from 'tauri-plugin-sql-api';
let db: null | Database = null;
// connect database (sqlite)
// path: tauri::api::path::BaseDirectory::App
export async function connect(): Promise<Database> {
if (db) {
return db;
}
db = await Database.load('sqlite:lume.db');
return db;
}
// get all relays
export async function getAllRelays() {
const db = await connect();
return await db.select('SELECT relay_url FROM relays WHERE relay_status = "1"');
}
// get active account
export async function getActiveAccount() {
const db = await connect();
return await db.select(`SELECT * FROM accounts LIMIT 1`);
}
// get all follows by account id
export async function getAllFollowsByID(id: string) {
const db = await connect();
return await db.select(`SELECT pubkey FROM follows WHERE account = "${id}"`);
}