feat: update fetch opg function
This commit is contained in:
24
packages/ark/src/hooks/usePreview.ts
Normal file
24
packages/ark/src/hooks/usePreview.ts
Normal file
@@ -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 };
|
||||||
|
}
|
||||||
@@ -1,54 +1,16 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use webpage::{Webpage, WebpageOptions};
|
use webpage::{Opengraph, Webpage, WebpageOptions};
|
||||||
|
|
||||||
#[derive(serde::Serialize)]
|
|
||||||
pub struct OpenGraphResponse {
|
|
||||||
title: String,
|
|
||||||
description: String,
|
|
||||||
url: String,
|
|
||||||
image: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn fetch_opg(url: String) -> Result<OpenGraphResponse, ()> {
|
pub fn fetch_opg(url: String) -> Result<Opengraph, String> {
|
||||||
let mut options = WebpageOptions::default();
|
let mut options = WebpageOptions::default();
|
||||||
options.allow_insecure = true;
|
options.allow_insecure = true;
|
||||||
options.max_redirections = 3;
|
options.max_redirections = 2;
|
||||||
options.timeout = Duration::from_secs(15);
|
options.timeout = Duration::from_secs(10);
|
||||||
|
|
||||||
let info = Webpage::from_url(&url, options);
|
if let Ok(data) = Webpage::from_url(&url, options) {
|
||||||
|
Ok(data.html.opengraph.into())
|
||||||
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())
|
|
||||||
} else {
|
} else {
|
||||||
Err(())
|
Err("Get open graph failed".into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,8 @@ pub mod commands;
|
|||||||
pub mod nostr;
|
pub mod nostr;
|
||||||
pub mod tray;
|
pub mod tray;
|
||||||
|
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
|
use std::fs;
|
||||||
use tauri::Manager;
|
use tauri::Manager;
|
||||||
use tauri_plugin_autostart::MacosLauncher;
|
use tauri_plugin_autostart::MacosLauncher;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user