From cb6006f596e480ac2e180508dbb1a9b47cb1dbb3 Mon Sep 17 00:00:00 2001 From: reya Date: Fri, 11 Oct 2024 14:09:15 +0700 Subject: [PATCH] feat: handle error when publish note --- src-tauri/src/commands/account.rs | 9 +++++++- src-tauri/src/commands/event.rs | 2 +- src-tauri/src/common.rs | 9 +++++++- src/routes/editor/index.tsx | 35 +++++++++++++++++++++++-------- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src-tauri/src/commands/account.rs b/src-tauri/src/commands/account.rs index 1de21ad5..26eea44a 100644 --- a/src-tauri/src/commands/account.rs +++ b/src-tauri/src/commands/account.rs @@ -353,7 +353,14 @@ pub async fn login( { let keys: Vec<&str> = events .iter() - .flat_map(|event| event.get_tags_content(TagKind::p())) + .flat_map(|event| { + event + .tags + .iter() + .filter(|t| t.kind() == TagKind::p()) + .filter_map(|t| t.content()) + .collect::>() + }) .collect(); let trusted_list: HashSet = keys diff --git a/src-tauri/src/commands/event.rs b/src-tauri/src/commands/event.rs index 9ca08b57..0988e134 100644 --- a/src-tauri/src/commands/event.rs +++ b/src-tauri/src/commands/event.rs @@ -372,7 +372,7 @@ pub async fn publish( // Publish match client.send_event_builder(builder).await { - Ok(event_id) => Ok(event_id.to_bech32().unwrap()), + Ok(event_id) => Ok(event_id.to_hex()), Err(err) => Err(err.to_string()), } } diff --git a/src-tauri/src/common.rs b/src-tauri/src/common.rs index b564815f..31b26db4 100644 --- a/src-tauri/src/common.rs +++ b/src-tauri/src/common.rs @@ -164,7 +164,14 @@ pub async fn process_event(client: &Client, events: Vec) -> Vec = requests .iter() - .flat_map(|ev| ev.get_tags_content(TagKind::e())) + .flat_map(|event| { + event + .tags + .iter() + .filter(|t| t.kind() == TagKind::e()) + .filter_map(|t| t.content()) + .collect::>() + }) .collect(); // Remove event if event is deleted by author diff --git a/src/routes/editor/index.tsx b/src/routes/editor/index.tsx index 52d08e19..b46d30e6 100644 --- a/src/routes/editor/index.tsx +++ b/src/routes/editor/index.tsx @@ -7,6 +7,7 @@ import { User } from "@/components/user"; import { LumeEvent, useEvent } from "@/system"; import { Feather } from "@phosphor-icons/react"; import { createFileRoute } from "@tanstack/react-router"; +import { getCurrentWindow } from "@tauri-apps/api/window"; import { nip19 } from "nostr-tools"; import { useEffect, useMemo, useRef, useState, useTransition } from "react"; import { createPortal } from "react-dom"; @@ -84,8 +85,9 @@ function Screen() { const { reply_to } = Route.useSearch(); const { users, initialValue } = Route.useRouteContext(); - const [isPending, startTransition] = useTransition(); const [text, setText] = useState(""); + const [error, setError] = useState(""); + const [isPending, startTransition] = useTransition(); const [attaches, setAttaches] = useState(null); const [warning, setWarning] = useState({ enable: false, reason: "" }); const [difficulty, setDifficulty] = useState({ enable: false, num: 21 }); @@ -127,16 +129,26 @@ function Screen() { const publish = async () => { startTransition(async () => { try { - const content = text.trim(); + // Temporary hide window + await getCurrentWindow().hide(); - await LumeEvent.publish( - content, - warning.enable && warning.reason.length ? warning.reason : null, - difficulty.num, - reply_to, - ); + let res: Result; - setText(""); + if (reply_to) { + res = await commands.reply(content, reply_to, root_to); + } else { + res = await commands.publish(content, warning, difficulty); + } + + if (res.status === "ok") { + setText(""); + // Close window + await getCurrentWindow().close(); + } else { + setError(res.error); + // Focus window + await getCurrentWindow().setFocus(); + } } catch { return; } @@ -158,6 +170,10 @@ function Screen() { Reply to: + ) : error?.length ? ( +
+

{error}

+
) : null}