From 3005d274036a26a866a4a435d5afe235074f9010 Mon Sep 17 00:00:00 2001 From: reya Date: Wed, 13 Mar 2024 10:24:45 +0700 Subject: [PATCH] feat: update fetch opg function --- packages/ark/src/hooks/usePreview.ts | 24 +++++++++++++ src-tauri/src/commands/opg.rs | 52 ++++------------------------ src-tauri/src/main.rs | 3 +- 3 files changed, 32 insertions(+), 47 deletions(-) create mode 100644 packages/ark/src/hooks/usePreview.ts diff --git a/packages/ark/src/hooks/usePreview.ts b/packages/ark/src/hooks/usePreview.ts new file mode 100644 index 00000000..01ca11cc --- /dev/null +++ b/packages/ark/src/hooks/usePreview.ts @@ -0,0 +1,24 @@ +import { useQuery } from "@tanstack/react-query"; +import { invoke } from "@tauri-apps/api/core"; + +export function usePreview(url: string) { + const { isLoading, isError, data } = useQuery({ + queryKey: ["url", url], + queryFn: async () => { + try { + const cmd = await invoke("fetch_opg", { url }); + console.log(cmd); + return cmd; + } catch (e) { + throw new Error(e); + } + }, + refetchOnWindowFocus: false, + refetchOnMount: false, + refetchOnReconnect: false, + staleTime: Infinity, + retry: 2, + }); + + return { isLoading, isError, data }; +} diff --git a/src-tauri/src/commands/opg.rs b/src-tauri/src/commands/opg.rs index 94a083ee..8eaa7371 100644 --- a/src-tauri/src/commands/opg.rs +++ b/src-tauri/src/commands/opg.rs @@ -1,54 +1,16 @@ use std::time::Duration; -use webpage::{Webpage, WebpageOptions}; - -#[derive(serde::Serialize)] -pub struct OpenGraphResponse { - title: String, - description: String, - url: String, - image: String, -} +use webpage::{Opengraph, Webpage, WebpageOptions}; #[tauri::command] -pub fn fetch_opg(url: String) -> Result { +pub fn fetch_opg(url: String) -> Result { let mut options = WebpageOptions::default(); options.allow_insecure = true; - options.max_redirections = 3; - options.timeout = Duration::from_secs(15); + options.max_redirections = 2; + options.timeout = Duration::from_secs(10); - let info = Webpage::from_url(&url, options); - - if let Ok(data) = info { - let html = data.html; - let result = OpenGraphResponse { - title: html - .opengraph - .properties - .get("title") - .cloned() - .unwrap_or_default(), - description: html - .opengraph - .properties - .get("description") - .cloned() - .unwrap_or_default(), - url: html - .opengraph - .properties - .get("url") - .cloned() - .unwrap_or_default(), - image: html - .opengraph - .images - .get(0) - .and_then(|i| Some(i.url.clone())) - .unwrap_or_default(), - }; - - Ok(result.into()) + if let Ok(data) = Webpage::from_url(&url, options) { + Ok(data.html.opengraph.into()) } else { - Err(()) + Err("Get open graph failed".into()) } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 53bd5515..a777881d 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -7,9 +7,8 @@ pub mod commands; pub mod nostr; pub mod tray; -use std::fs; - use nostr_sdk::prelude::*; +use std::fs; use tauri::Manager; use tauri_plugin_autostart::MacosLauncher;