diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index d1839a2d..9c75451b 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -16,7 +16,7 @@ tauri-build = { version = "1.2", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
-tauri = { version = "1.2", features = ["app-all", "clipboard-all", "http-all", "notification-all", "os-all", "shell-open", "system-tray", "window-start-dragging"] }
+tauri = { version = "1.2", features = ["app-all", "clipboard-all", "http-all", "notification-all", "os-all", "shell-open", "system-tray", "window-close", "window-start-dragging"] }
[dependencies.tauri-plugin-sql]
git = "https://github.com/tauri-apps/plugins-workspace"
diff --git a/src-tauri/migrations/20230226004139_create_tables.sql b/src-tauri/migrations/20230226004139_create_tables.sql
index 24603813..f42ee440 100644
--- a/src-tauri/migrations/20230226004139_create_tables.sql
+++ b/src-tauri/migrations/20230226004139_create_tables.sql
@@ -87,4 +87,18 @@ CREATE TABLE
content TEXT NOT NULL,
parent_id TEXT,
parent_comment_id TEXT
- );
\ No newline at end of file
+ );
+
+-- create settings
+CREATE TABLE
+ settings (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ setting_key TEXT NOT NULL,
+ setting_value TEXT NOT NULL
+ );
+
+-- add default setting
+INSERT INTO
+ settings (setting_key, setting_value)
+VALUES
+ ("last_login", "0");
\ No newline at end of file
diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json
index 1f2dbd20..e95465c7 100644
--- a/src-tauri/tauri.conf.json
+++ b/src-tauri/tauri.conf.json
@@ -39,7 +39,8 @@
"all": true
},
"window": {
- "startDragging": true
+ "startDragging": true,
+ "close": true
}
},
"bundle": {
diff --git a/src/components/note/connector.tsx b/src/components/note/connector.tsx
index 1edcb83f..64a56207 100644
--- a/src/components/note/connector.tsx
+++ b/src/components/note/connector.tsx
@@ -4,12 +4,14 @@ import { activeAccountAtom } from '@stores/account';
import { hasNewerNoteAtom } from '@stores/note';
import { relaysAtom } from '@stores/relays';
-import { dateToUnix, hoursAgo } from '@utils/getDate';
-import { createCacheNote, getAllFollowsByID } from '@utils/storage';
+import { dateToUnix } from '@utils/getDate';
+import { createCacheNote, getAllFollowsByID, updateLastLoginTime } from '@utils/storage';
import { pubkeyArray } from '@utils/transform';
+import { window } from '@tauri-apps/api';
+import { TauriEvent } from '@tauri-apps/api/event';
import { useAtom, useAtomValue, useSetAtom } from 'jotai';
-import { memo, useContext, useEffect, useRef, useState } from 'react';
+import { memo, useCallback, useContext, useEffect, useRef, useState } from 'react';
export const NoteConnector = memo(function NoteConnector() {
const pool: any = useContext(RelayContext);
@@ -21,29 +23,34 @@ export const NoteConnector = memo(function NoteConnector() {
const [isOnline] = useState(true);
const now = useRef(new Date());
- useEffect(() => {
+ const subscribe = useCallback(() => {
getAllFollowsByID(activeAccount.id).then((follows) => {
pool.subscribe(
[
{
kinds: [1],
authors: pubkeyArray(follows),
- since: dateToUnix(hoursAgo(12, now.current)),
+ since: dateToUnix(now.current),
},
],
relays,
(event: any) => {
// insert event to local database
createCacheNote(event);
- // ask user load newer note
- if (event.created_at > dateToUnix(now.current)) {
- setHasNewerNote(true);
- }
+ setHasNewerNote(true);
}
);
});
}, [activeAccount.id, pool, relays, setHasNewerNote]);
+ useEffect(() => {
+ subscribe();
+ window.getCurrent().listen(TauriEvent.WINDOW_CLOSE_REQUESTED, () => {
+ updateLastLoginTime(now.current);
+ window.appWindow.close();
+ });
+ }, [activeAccount.id, pool, relays, setHasNewerNote, subscribe]);
+
return (
<>
diff --git a/src/pages/index.tsx b/src/pages/index.tsx
index 6149c8df..dc300974 100644
--- a/src/pages/index.tsx
+++ b/src/pages/index.tsx
@@ -14,7 +14,7 @@ export default function Page() {
getAccounts()
.then((res: any) => {
if (res.length > 0) {
- router.push('/newsfeed/following');
+ router.push('/init');
} else {
router.push('/onboarding');
}
diff --git a/src/pages/init.tsx b/src/pages/init.tsx
new file mode 100644
index 00000000..a3ee40ab
--- /dev/null
+++ b/src/pages/init.tsx
@@ -0,0 +1,133 @@
+import BaseLayout from '@layouts/base';
+
+import { RelayContext } from '@components/relaysProvider';
+
+import { activeAccountAtom } from '@stores/account';
+import { relaysAtom } from '@stores/relays';
+
+import { dateToUnix, hoursAgo } from '@utils/getDate';
+import { countTotalNotes, createCacheNote, getAllFollowsByID, getLastLoginTime } from '@utils/storage';
+import { pubkeyArray } from '@utils/transform';
+
+import LumeSymbol from '@assets/icons/Lume';
+
+import { useAtom, useAtomValue } from 'jotai';
+import { useRouter } from 'next/router';
+import { JSXElementConstructor, ReactElement, ReactFragment, ReactPortal, useContext, useEffect, useRef } from 'react';
+
+export default function Page() {
+ const router = useRouter();
+ const pool: any = useContext(RelayContext);
+
+ const relays = useAtomValue(relaysAtom);
+ const [activeAccount] = useAtom(activeAccountAtom);
+
+ const now = useRef(new Date());
+ const timer = useRef(null);
+ const unsubscribe = useRef(null);
+
+ useEffect(() => {
+ countTotalNotes().then((count) => {
+ if (count.total === 0) {
+ getAllFollowsByID(activeAccount.id).then((follows) => {
+ unsubscribe.current = pool.subscribe(
+ [
+ {
+ kinds: [1],
+ authors: pubkeyArray(follows),
+ since: dateToUnix(hoursAgo(24, now.current)),
+ until: dateToUnix(now.current),
+ },
+ ],
+ relays,
+ (event) => {
+ // insert event to local database
+ createCacheNote(event);
+ },
+ undefined,
+ () => {
+ timer.current = setTimeout(() => router.push('/newsfeed/following'), 3000);
+ }
+ );
+ });
+ } else {
+ getLastLoginTime().then((time) => {
+ const parseDate = new Date(time);
+
+ getAllFollowsByID(activeAccount.id).then((follows) => {
+ unsubscribe.current = pool.subscribe(
+ [
+ {
+ kinds: [1],
+ authors: pubkeyArray(follows),
+ since: dateToUnix(parseDate),
+ until: dateToUnix(now.current),
+ },
+ ],
+ relays,
+ (event) => {
+ // insert event to local database
+ createCacheNote(event);
+ },
+ undefined,
+ () => {
+ timer.current = setTimeout(() => router.push('/newsfeed/following'), 3000);
+ }
+ );
+ });
+ });
+ }
+ });
+
+ return () => {
+ unsubscribe.current();
+ clearTimeout(timer.current);
+ };
+ }, [activeAccount.id, pool, relays, router]);
+
+ return (
+
+ {/* dragging area */}
+
+ {/* end dragging area */}
+
+
+
+
+
Loading...
+
+ Keep calm and waiting, Lume is fetching event...
+
+
+
+
+
+
+ );
+}
+
+Page.getLayout = function getLayout(
+ page:
+ | string
+ | number
+ | boolean
+ | ReactElement
>
+ | ReactFragment
+ | ReactPortal
+) {
+ return {page};
+};
diff --git a/src/utils/storage.tsx b/src/utils/storage.tsx
index 4fdd5892..bd7a663a 100644
--- a/src/utils/storage.tsx
+++ b/src/utils/storage.tsx
@@ -143,3 +143,23 @@ export async function createCacheCommentNote(data, eid) {
]
);
}
+
+// create cache comment note
+export async function countTotalNotes() {
+ const db = await connect();
+ const result = await db.select('SELECT COUNT(*) AS "total" FROM cache_notes;');
+ return result[0];
+}
+
+// get last login time
+export async function getLastLoginTime() {
+ const db = await connect();
+ const result = await db.select('SELECT setting_value FROM settings WHERE setting_key = "last_login"');
+ return result[0];
+}
+
+// update last login time
+export async function updateLastLoginTime(time) {
+ const db = await connect();
+ return await db.execute(`UPDATE settings SET setting_value = "${time}" WHERE setting_key = "last_login"`);
+}