From 1a9b3a2603c2b538c11a8980d595371b3f6908df Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:28:57 +0700 Subject: [PATCH] added init data page --- src-tauri/Cargo.toml | 2 +- .../20230226004139_create_tables.sql | 16 ++- src-tauri/tauri.conf.json | 3 +- src/components/note/connector.tsx | 25 ++-- src/pages/index.tsx | 2 +- src/pages/init.tsx | 133 ++++++++++++++++++ src/utils/storage.tsx | 20 +++ 7 files changed, 188 insertions(+), 13 deletions(-) create mode 100644 src/pages/init.tsx 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 ( <>
+ Keep calm and waiting, Lume is fetching event... +
+