From 6e1fa1040235abeb8c5951587e2ac63ecbd5ed54 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Sat, 3 Jun 2023 08:45:47 +0700 Subject: [PATCH] refactor --- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 6 +++ src/app/auth/pages/create/index.page.tsx | 11 +++-- .../auth/pages/create/step-2/index.page.tsx | 9 +--- .../auth/pages/create/step-3/index.page.tsx | 10 ++--- src/app/auth/pages/import/index.page.tsx | 10 ++--- .../auth/pages/import/step-2/index.page.tsx | 34 ++++++--------- src/app/index/pages/index.page.tsx | 26 +++++++----- src/app/note/components/replies/form.tsx | 10 ----- src/app/prefetch/pages/index.page.tsx | 41 ++++++++++++++++++- src/stores/accounts.tsx | 19 ++++++++- src/utils/storage.tsx | 2 +- 12 files changed, 104 insertions(+), 76 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 63d2d20f..4950aa95 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 = ["clipboard-read-text", "clipboard-write-text", "dialog-open", "fs-read-dir", "fs-read-file", "http-all", "http-multipart", "notification-all", "os-all", "shell-open", "system-tray", "updater", "window-close", "window-start-dragging"] } +tauri = { version = "1.2", features = ["clipboard-read-text", "clipboard-write-text", "dialog-open", "fs-read-dir", "fs-read-file", "http-all", "http-multipart", "notification-all", "os-all", "process-relaunch", "shell-open", "system-tray", "updater", "window-close", "window-start-dragging"] } tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } sqlx-cli = {version = "0.6.3", default-features = false, features = ["sqlite"] } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index f94d9a06..6491c50c 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -60,6 +60,12 @@ "window": { "startDragging": true, "close": true + }, + "process": { + "all": false, + "exit": false, + "relaunch": true, + "relaunchDangerousAllowSymlinkMacos": false } }, "bundle": { diff --git a/src/app/auth/pages/create/index.page.tsx b/src/app/auth/pages/create/index.page.tsx index 42de366c..d2638ff2 100644 --- a/src/app/auth/pages/create/index.page.tsx +++ b/src/app/auth/pages/create/index.page.tsx @@ -1,10 +1,12 @@ import { EyeOffIcon, EyeOnIcon } from "@shared/icons"; -import { createAccount } from "@utils/storage"; +import { useActiveAccount } from "@stores/accounts"; import { generatePrivateKey, getPublicKey, nip19 } from "nostr-tools"; import { useMemo, useState } from "react"; import { navigate } from "vite-plugin-ssr/client/router"; export function Page() { + const createAccount = useActiveAccount((state: any) => state.create); + const [type, setType] = useState("password"); const privkey = useMemo(() => generatePrivateKey(), []); @@ -22,11 +24,8 @@ export function Page() { }; const submit = async () => { - const account = await createAccount(npub, pubkey, privkey, null, 1); - - if (account) { - navigate("/app/auth/create/step-2"); - } + createAccount(npub, pubkey, privkey, null, 1); + navigate("/app/auth/create/step-2"); }; return ( diff --git a/src/app/auth/pages/create/step-2/index.page.tsx b/src/app/auth/pages/create/step-2/index.page.tsx index 69386640..a24a0fc3 100644 --- a/src/app/auth/pages/create/step-2/index.page.tsx +++ b/src/app/auth/pages/create/step-2/index.page.tsx @@ -10,11 +10,8 @@ import { navigate } from "vite-plugin-ssr/client/router"; export function Page() { const pool: any = useContext(RelayContext); + const account = useActiveAccount((state: any) => state.account); - const [account, fetchAccount] = useActiveAccount((state: any) => [ - state.account, - state.fetch, - ]); const [image, setImage] = useState(DEFAULT_AVATAR); const [loading, setLoading] = useState(false); @@ -52,10 +49,6 @@ export function Page() { ); }; - useEffect(() => { - fetchAccount(); - }, [fetchAccount]); - useEffect(() => { setValue("picture", image); }, [setValue, image]); diff --git a/src/app/auth/pages/create/step-3/index.page.tsx b/src/app/auth/pages/create/step-3/index.page.tsx index 5b8dba4b..8d16a8ff 100644 --- a/src/app/auth/pages/create/step-3/index.page.tsx +++ b/src/app/auth/pages/create/step-3/index.page.tsx @@ -3,7 +3,6 @@ import { CheckCircleIcon } from "@shared/icons"; import { RelayContext } from "@shared/relayProvider"; import { useActiveAccount } from "@stores/accounts"; import { WRITEONLY_RELAYS } from "@stores/constants"; -import { updateAccount } from "@utils/storage"; import { arrayToNIP02 } from "@utils/transform"; import { getEventHash, getSignature } from "nostr-tools"; import { useContext, useState } from "react"; @@ -130,11 +129,8 @@ export function Page() { const submit = async () => { setLoading(true); - // update account follows in database - updateAccount("follows", follows, account.pubkey); - - // update account follows in state - updateFollows(JSON.stringify(follows)); + // update account follows + updateFollows(follows); const tags = arrayToNIP02(follows); @@ -155,7 +151,7 @@ export function Page() { // redirect to step 3 setTimeout( () => - navigate("/app/prefetch", { + navigate("/", { overwriteLastHistoryEntry: true, }), 2000, diff --git a/src/app/auth/pages/import/index.page.tsx b/src/app/auth/pages/import/index.page.tsx index 3b5a703a..1fc6148f 100644 --- a/src/app/auth/pages/import/index.page.tsx +++ b/src/app/auth/pages/import/index.page.tsx @@ -1,4 +1,4 @@ -import { createAccount } from "@utils/storage"; +import { useActiveAccount } from "@stores/accounts"; import { getPublicKey, nip19 } from "nostr-tools"; import { Resolver, useForm } from "react-hook-form"; import { navigate } from "vite-plugin-ssr/client/router"; @@ -22,6 +22,7 @@ const resolver: Resolver = async (values) => { }; export function Page() { + const createAccount = useActiveAccount((state: any) => state.create); const { register, setError, @@ -41,11 +42,8 @@ export function Page() { const pubkey = getPublicKey(privkey); const npub = nip19.npubEncode(pubkey); - const account = await createAccount(npub, pubkey, privkey, null, 1); - - if (account) { - navigate("/app/auth/import/step-2"); - } + createAccount(npub, pubkey, privkey, null, 1); + navigate("/app/auth/import/step-2"); } } catch (error) { setError("key", { diff --git a/src/app/auth/pages/import/step-2/index.page.tsx b/src/app/auth/pages/import/step-2/index.page.tsx index ac856d76..a92b42a7 100644 --- a/src/app/auth/pages/import/step-2/index.page.tsx +++ b/src/app/auth/pages/import/step-2/index.page.tsx @@ -1,31 +1,31 @@ import { User } from "@app/auth/components/user"; import { RelayContext } from "@shared/relayProvider"; import { useActiveAccount } from "@stores/accounts"; -import { READONLY_RELAYS } from "@stores/constants"; -import { updateAccount } from "@utils/storage"; +import { METADATA_RELAY } from "@stores/constants"; import { nip02ToArray } from "@utils/transform"; -import { useContext, useEffect, useState } from "react"; +import { useContext, useState } from "react"; import useSWRSubscription from "swr/subscription"; import { navigate } from "vite-plugin-ssr/client/router"; export function Page() { const pool: any = useContext(RelayContext); - const [account, fetchAccount, updateFollows] = useActiveAccount( - (state: any) => [state.account, state.fetch, state.updateFollows], - ); + const [account, updateFollows] = useActiveAccount((state: any) => [ + state.account, + state.updateFollows, + ]); const [loading, setLoading] = useState(false); - const [follows, setFollows] = useState([]); + const [follows, setFollows] = useState(null); - useSWRSubscription(account ? account.pubkey : null, (key: string) => { + useSWRSubscription(account ? ["follows", account.pubkey] : null, () => { const unsubscribe = pool.subscribe( [ { kinds: [3], - authors: [key], + authors: [account.pubkey], }, ], - READONLY_RELAYS, + METADATA_RELAY, (event: any) => { setFollows(event.tags); }, @@ -43,23 +43,13 @@ export function Page() { // follows as list const followsList = nip02ToArray(follows); - // update account follows in database - updateAccount("follows", followsList, account.pubkey); - // update account follows in store - updateFollows(JSON.stringify(followsList)); + updateFollows(followsList); // redirect to home - setTimeout( - () => navigate("/app/prefetch", { overwriteLastHistoryEntry: true }), - 2000, - ); + setTimeout(() => navigate("/", { overwriteLastHistoryEntry: true }), 2000); }; - useEffect(() => { - fetchAccount(); - }, [fetchAccount]); - return (
diff --git a/src/app/index/pages/index.page.tsx b/src/app/index/pages/index.page.tsx index 57d5393d..64a8fb03 100644 --- a/src/app/index/pages/index.page.tsx +++ b/src/app/index/pages/index.page.tsx @@ -6,19 +6,23 @@ export function Page() { const fetchLastLogin = useActiveAccount((state: any) => state.fetchLastLogin); const fetchAccount = useActiveAccount((state: any) => state.fetch); const account = useActiveAccount((state: any) => state.account); - - if (!account && typeof window !== "undefined") { - navigate("/app/auth", { overwriteLastHistoryEntry: true }); - } - - if (account && typeof window !== "undefined") { - navigate("/app/prefetch", { overwriteLastHistoryEntry: true }); - } + const lastLogin = useActiveAccount((state: any) => state.lastLogin); useEffect(() => { - fetchAccount(); - fetchLastLogin(); - }, [fetchAccount, fetchLastLogin]); + if (!account) { + navigate("/app/auth", { overwriteLastHistoryEntry: true }); + } + + if (account) { + navigate("/app/prefetch", { overwriteLastHistoryEntry: true }); + } + if (account === null) { + fetchAccount(); + } + if (lastLogin === null) { + fetchLastLogin(); + } + }, [fetchAccount, fetchLastLogin, account, lastLogin]); return (
diff --git a/src/app/note/components/replies/form.tsx b/src/app/note/components/replies/form.tsx index 2940efcb..2a5947ab 100644 --- a/src/app/note/components/replies/form.tsx +++ b/src/app/note/components/replies/form.tsx @@ -11,7 +11,6 @@ export function NoteReplyForm({ id }: { id: string }) { const pool: any = useContext(RelayContext); const account = useActiveAccount((state: any) => state.account); - const { user } = useProfile(account.pubkey); const [value, setValue] = useState(""); const submitEvent = () => { @@ -35,15 +34,6 @@ export function NoteReplyForm({ id }: { id: string }) { return (
-
-
- {id} -
-