From 5dda246820e753adead257bea232dba61537aa27 Mon Sep 17 00:00:00 2001 From: "benjamin.747" Date: Tue, 20 Aug 2024 16:51:02 +0800 Subject: [PATCH] add api feedback in request --- gateway/src/api/ztm_router.rs | 35 +++++++++++++++++++--- jupiter/src/storage/ztm_storage.rs | 6 +++- lunar/.env.local | 2 +- lunar/src-tauri/Cargo.toml | 11 ++++--- lunar/src/app/api/fetcher.ts | 4 ++- lunar/src/app/settings/page.tsx | 39 +++++++++++++++++------- lunar/src/components/CodeTable.tsx | 40 ++++++++++++++++++------- lunar/src/components/catalyst/hello.tsx | 17 ----------- 8 files changed, 104 insertions(+), 50 deletions(-) delete mode 100644 lunar/src/components/catalyst/hello.tsx diff --git a/gateway/src/api/ztm_router.rs b/gateway/src/api/ztm_router.rs index 49f8d9b64..fe021861d 100644 --- a/gateway/src/api/ztm_router.rs +++ b/gateway/src/api/ztm_router.rs @@ -20,6 +20,7 @@ pub fn routers() -> Router { .route("/ztm/repo_provide", post(repo_provide)) .route("/ztm/repo_fork", get(repo_folk)) .route("/ztm/peer_id", get(peer_id)) + .route("/ztm/alias_to_path", get(alias_to_path)) } async fn repo_provide( @@ -38,10 +39,12 @@ async fn repo_provide( let RepoProvideQuery { path, alias } = json.clone(); let context = state.inner.context.clone(); let model: ztm_path_mapping::Model = json.into(); - match context.services.ztm_storage.save_alias_mapping(model.clone()).await { - Ok(_) => (), - Err(err) => return Err((StatusCode::BAD_REQUEST, err.to_string())), - } + context + .services + .ztm_storage + .save_alias_mapping(model.clone()) + .await + .map_err(|_| (StatusCode::BAD_REQUEST, String::from("Invalid Params")))?; let res = match gemini::http::handler::repo_provide( bootstrap_node, state.inner.context.clone(), @@ -108,3 +111,27 @@ async fn peer_id( let (peer_id, _) = vault::init(); Ok(Json(CommonResult::success(Some(peer_id)))) } + +async fn alias_to_path( + Query(query): Query>, + state: State, +) -> Result>, (StatusCode, String)> { + let context = state.inner.context.clone(); + let alias = match query.get("alias") { + Some(str) => str, + None => { + return Err((StatusCode::BAD_REQUEST, String::from("Alias not provide\n"))); + } + }; + let res = context + .services + .ztm_storage + .get_path_from_alias(alias) + .await + .map_err(|_| (StatusCode::BAD_REQUEST, String::from("Invalid Params")))?; + if let Some(res) = res { + Ok(Json(CommonResult::success(Some(res.repo_path)))) + } else { + Err((StatusCode::BAD_REQUEST, String::from("Alias not found\n"))) + } +} diff --git a/jupiter/src/storage/ztm_storage.rs b/jupiter/src/storage/ztm_storage.rs index b0360845f..ed73eb1e6 100644 --- a/jupiter/src/storage/ztm_storage.rs +++ b/jupiter/src/storage/ztm_storage.rs @@ -239,7 +239,11 @@ impl ZTMStorage { ) -> Result<(), MegaError> { ztm_path_mapping::Entity::insert(model.into_active_model()) .exec(self.get_connection()) - .await?; + .await + .map_err(|err| { + tracing::error!("Error saving alias mapping: {}", err); + err + })?; Ok(()) } diff --git a/lunar/.env.local b/lunar/.env.local index 8a5e7944f..06f23c0a6 100644 --- a/lunar/.env.local +++ b/lunar/.env.local @@ -1,2 +1,2 @@ -NEXT_PUBLIC_RELAY_API_URL=http://34.84.172.121 +NEXT_PUBLIC_RELAY_API_URL=http://gitmono.org NEXT_PUBLIC_API_URL=http://localhost:8000 diff --git a/lunar/src-tauri/Cargo.toml b/lunar/src-tauri/Cargo.toml index 984fc0daf..3f1cd1fc6 100644 --- a/lunar/src-tauri/Cargo.toml +++ b/lunar/src-tauri/Cargo.toml @@ -14,12 +14,15 @@ rust-version = "1.60" tauri-build = { version = "1.5.3", features = [] } [dependencies] -serde_json = "1.0" -serde = { version = "1.0", features = ["derive"] } -tauri = { version = "1.7.0", features = [ "shell-sidecar", "process-command-api"] } +serde_json = { workspace = true } +serde = { workspace = true, features = ["derive"] } +tauri = { version = "1.7.1", features = [ + "shell-sidecar", + "process-command-api", +] } tokio = { workspace = true } -[dev-dependencies] +[dev-dependencies] tokio = { workspace = true, features = ["macros"] } [features] diff --git a/lunar/src/app/api/fetcher.ts b/lunar/src/app/api/fetcher.ts index 95a1d126d..14ad39bbf 100644 --- a/lunar/src/app/api/fetcher.ts +++ b/lunar/src/app/api/fetcher.ts @@ -139,7 +139,9 @@ export async function requestPublishRepo(data) { }); if (!response.ok) { - throw new Error('Failed to publish repo'); + const errorResponse = await response.text(); + const errorMessage = errorResponse || 'Failed to publish repo'; + throw new Error(errorMessage); } return response.json(); } \ No newline at end of file diff --git a/lunar/src/app/settings/page.tsx b/lunar/src/app/settings/page.tsx index 4f23d07b2..40d74a087 100644 --- a/lunar/src/app/settings/page.tsx +++ b/lunar/src/app/settings/page.tsx @@ -6,18 +6,26 @@ import { Input } from '@/components/catalyst/input' import { Text } from '@/components/catalyst/text' import { invoke } from '@tauri-apps/api/tauri' import { useState } from 'react' -import { Button, Skeleton } from "antd"; +import { Button, Skeleton, message } from "antd"; import { usePeerId } from '@/app/api/fetcher' - interface MegaStartParams { bootstrap_node: string, } export default function Settings() { + const [messageApi, contextHolder] = message.useMessage(); + + const success = () => { + messageApi.open({ + type: 'success', + content: 'Save setting successful', + }); + }; + const [loadings, setLoadings] = useState([]); const [params, setParams] = useState({ - bootstrap_node: "http://34.84.172.121/relay", + bootstrap_node: "http://gitmono.org/relay", }); const { peerId, isLoading, isError } = usePeerId(); if (isLoading) return ; @@ -28,13 +36,14 @@ export default function Settings() { newLoadings[index] = true; return newLoadings; }); - setTimeout(() => { - setLoadings((prevLoadings) => { - const newLoadings = [...prevLoadings]; - newLoadings[index] = false; - return newLoadings; - }); - }, 6000); + } + + const exitLoading = (index: number) => { + setLoadings((prevLoadings) => { + const newLoadings = [...prevLoadings]; + newLoadings[index] = false; + return newLoadings; + }); } const stopMega = async () => { @@ -46,8 +55,15 @@ export default function Settings() { const restartMega = async () => { enterLoading(1); invoke('restart_mega_service', { params: params }) - .then((message) => console.log("result:", message)) + .then((message) => { + console.log("result:", message); + success() + }) .catch((err) => console.error("err:", err)); + + setTimeout(() => { + exitLoading(1); + }, 1000); }; const handleInputChange = (e: React.ChangeEvent) => { @@ -60,6 +76,7 @@ export default function Settings() { return (
+ {contextHolder} Settings diff --git a/lunar/src/components/CodeTable.tsx b/lunar/src/components/CodeTable.tsx index 953e58cde..3cc1bb8e4 100644 --- a/lunar/src/components/CodeTable.tsx +++ b/lunar/src/components/CodeTable.tsx @@ -5,7 +5,7 @@ import { useRouter, useSearchParams } from 'next/navigation' import Markdown from 'react-markdown' import { formatDistance, fromUnixTime } from 'date-fns' import styles from './CodeTable.module.css' -import { Input, Modal, Space, Table, TableProps } from 'antd/lib' +import { Input, Modal, Space, Table, TableProps, message } from 'antd/lib' import { useState } from 'react' import { FolderIcon, @@ -23,6 +23,21 @@ export interface DataType { } const CodeTable = ({ directory, readmeContent, with_ztm }) => { + const [messageApi, contextHolder] = message.useMessage(); + + const msg_error = (content: String) => { + messageApi.open({ + type: 'error', + content: content, + }); + }; + const msg_success = (content: String) => { + messageApi.open({ + type: 'success', + content: content, + }); + }; + const router = useRouter(); const fileCodeContainerStyle = { width: '100%', @@ -79,7 +94,7 @@ const CodeTable = ({ directory, readmeContent, with_ztm }) => { render: (_, record) => ( - + {/* */} ), }, @@ -134,17 +149,19 @@ const CodeTable = ({ directory, readmeContent, with_ztm }) => { newPath = `${path}/${filename}`; } setConfirmLoading(true); - await requestPublishRepo({ - "path": newPath, - "alias": filename, - }) + + try { + const result = await requestPublishRepo({ + "path": newPath, + "alias": filename, + }); + msg_success("Publish Success!"); + console.log('Repo published successfully:', result); + } catch (error) { + msg_error("Publish failed:" + error); + } setOpen(false); setConfirmLoading(false); - // setTimeout(() => { - // console.log("publish path", newPath); - // setOpen(false); - // setConfirmLoading(false); - // }, 2000); }; const handleCancel = () => { @@ -153,6 +170,7 @@ const CodeTable = ({ directory, readmeContent, with_ztm }) => { return (
+ {contextHolder} ('数据加载中...') - useEffect(() => { - // sleep 1s, to see the loading effect - setTimeout(() => { - invoke('hello_string', { name: 'lunar' }).then((response: string) => { - setContent(response) - }) - }, 1000) - }, []) - return
{content}
-}