From 0ed95979ca1ac64cb0013a1837cb4fb049ffd83a Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Mon, 15 Jan 2024 12:26:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat(src/App):=20=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/server/mod.rs | 23 +++++++++++++++-------- src/App.tsx | 17 +++++++++++------ types/index.d.ts | 3 ++- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src-tauri/src/server/mod.rs b/src-tauri/src/server/mod.rs index dcc3998..7c90cb9 100644 --- a/src-tauri/src/server/mod.rs +++ b/src-tauri/src/server/mod.rs @@ -66,23 +66,22 @@ impl Writer for ServerError { #[derive(Debug, Serialize, Clone)] struct Task { name: String, - progress: f64, + percent: f64, + speed: f64, // MB/s } impl Into for Task { fn into(self) -> String { - format!( - r#"{{"name":"{}", "progress":{}}}"#, - self.name, self.progress - ) + format!(r#"{{"name":"{}", "progress":{}}}"#, self.name, self.percent) } } impl Task { - fn new>(name: S, progress: f64) -> Self { + fn new>(name: S, progress: f64, speed: f64) -> Self { Self { name: name.into(), - progress, + percent: progress, + speed, } } } @@ -183,10 +182,18 @@ async fn upload(req: &mut Request) -> Result<()> { let name = name.clone(); move |_, progress| { let name = name.clone(); + + let progress_end = Instant::now(); + + // cost 不能是秒, 秒转为整数可能是 0 + let cost = progress_end.duration_since(start).as_millis() as f64; + + let speed = (progress / 1024 / 1024) as f64 / (cost / 1000.0); + if let Some(w) = MAIN_WINDOW.get() { let _ = w.emit( UPLOAD_EVENT, - Task::new(name, (progress * 10000 / size) as f64 / 100.0), + Task::new(name, (progress * 10000 / size) as f64 / 100.0, speed), ); } } diff --git a/src/App.tsx b/src/App.tsx index ced955e..6dcc882 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -16,9 +16,10 @@ import { open as pick } from "@tauri-apps/api/dialog"; interface FileListItemProps { name: string; percent: number; + speed?: number; } -const FileListItem = ({ name, percent }: FileListItemProps) => ( +const FileListItem = ({ name, percent, speed }: FileListItemProps) => (
( }} > {name} + {speed ? `(${speed.toFixed(1)} MB/s)` : ""}
@@ -41,7 +43,7 @@ const App = () => { const [progressList, setProgressList] = useState>( new OrderedSet("name"), ); - const [fileList, setFileList] = useState([]); + const [fileList, setFileList] = useState[]>([]); const [openDropDown, setOpenDropDown] = useState(false); @@ -57,13 +59,15 @@ const App = () => { setProgressList((pre) => pre.push(e.payload)); - if (e.payload.progress === 100) { + const { name, percent } = e.payload; + + if (percent === 100) { setProgressList((pre) => pre.remove(e.payload)); setFileList((pre) => { - const t = pre.find((v) => v.name === e.payload.name); + const t = pre.find((v) => v.name === name); - return t ? pre : [...pre, e.payload]; + return t ? pre : [...pre, { name, percent }]; }); } }); @@ -169,7 +173,8 @@ const App = () => { ))} diff --git a/types/index.d.ts b/types/index.d.ts index abc45ba..fa04151 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,6 +1,7 @@ interface TaskMessage { name: string; - progress: number; + percent: number; + speed: number; } interface QrCode { From de4546a79560f225fef9c7c081a27a22b6b617f9 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Mon, 15 Jan 2024 12:41:36 +0800 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 80359a6..d45925d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ 在局域网中使用手机快速向 PC 传递文件的工具,适配 Windows、macOS 和 Linux 桌面发行版。 +在未开发手机端前,只能通过手机浏览器上传文件,建议使用 [Edge](https://play.google.com/store/search?q=edge&c=apps)、[Chrome](https://play.google.com/store/search?q=Chrome&c=apps) 、[Firefox](https://play.google.com/store/apps/details?id=org.mozilla.firefox) 或 [QQ浏览器](https://browser.qq.com/mobile)。 + ## 特点 本软件可能有别于其他同功能软件的特点有: From 86448d6900f58e8340fdb7cf034cbe743e6f5aeb Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Mon, 15 Jan 2024 13:02:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E5=AE=9A=E6=97=B6=E5=99=A8=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E6=9C=AA=E6=B8=85=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不通过二维码扫描而直接进入上传页上传文件不会清除定时器,需要在 useEffect 退出前手动清除定时器。 --- src/App.tsx | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 6dcc882..23d2cfc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -86,18 +86,16 @@ const App = () => { useEffect(() => { if (!qrcode) return; - const query = async () => { - const timer = setInterval(async () => { - const used = await getQrCodeState(qrcode.id); - - if (used) { - clearTimeout(timer); - setQrcode(null); - } - }, 500); - }; + const timer = setInterval(async () => { + const used = await getQrCodeState(qrcode.id); + + if (used) { + clearTimeout(timer); + setQrcode(null); + } + }, 500); - query(); + return () => clearTimeout(timer); }, [qrcode]); const pickDirectory = async () => {