diff --git a/server/components/home/bot-network.jsx b/server/components/home/bot-network.jsx index 6fe1563..abc3feb 100644 --- a/server/components/home/bot-network.jsx +++ b/server/components/home/bot-network.jsx @@ -1,91 +1,102 @@ import React, { useEffect, useState } from "react"; import { NETWORK_BASE_URL } from "../../constants/system.js"; +// 测试链接配置 +const TESTING_LINKS = [ + { + name: "bilibili", + url: "https://bilibili.com/", + icon: ( + + ) + }, + { + name: "Github", + url: "https://github.com/", + icon: ( + + ) + }, + { + name: "YouTube", + url: "https://youtube.com/", + icon: ( + + ) + }, + { + name: "Tiktok", + url: "https://tiktok.com/", + icon: ( + + ) + }, + // ... 其他链接配置类似 +]; + export function BotNetwork() { + const [linksTime, setLinksTime] = useState(new Array(TESTING_LINKS.length).fill('NaN ms')); + const [isLoading, setIsLoading] = useState(false); - const [linksTime, setLinksTime] = useState(['0 ms', '0 ms', '0 ms', '0 ms']); - - useEffect(() => { - const waitingForTestingLink = [ - "https://kimi.moonshot.cn/", - "https://github.com/", - "https://youtube.com/", - "https://www.google.com/" - ] - async function getNetwork() { - for (const value of waitingForTestingLink) { - const index = waitingForTestingLink.indexOf(value); - const response = await fetch(NETWORK_BASE_URL + value); - let tmp = linksTime; - tmp[index] = `${ (await response.json()).time }ms`; - setLinksTime(tmp); - } + // 测试单个链接 + const testSingleLink = async (url, index) => { + try { + const response = await fetch(NETWORK_BASE_URL + url); + const data = await response.json(); + setLinksTime(prev => { + const newTimes = [...prev]; + newTimes[index] = `${data.time}ms`; + return newTimes; + }); + } catch (error) { + console.error(`测试链接失败: ${url}`, error); + setLinksTime(prev => { + const newTimes = [...prev]; + newTimes[index] = '超时'; + return newTimes; + }); } - // 每隔10秒更新一次系统信息 - // const intervalId = setInterval(getNetwork, 10000); - // - // // 清除定时器,避免内存泄漏 - // return () => clearInterval(intervalId); - }, []); + }; + + // 一键测速 + const handleTestAll = async () => { + setIsLoading(true); + setLinksTime(new Array(TESTING_LINKS.length).fill('测试中...')); + + try { + await Promise.all( + TESTING_LINKS.map((link, index) => + testSingleLink(link.url, index) + ) + ); + } finally { + setIsLoading(false); + } + }; return (
-

🐔网速

-
-
- - - - - - { linksTime[0] } +
+

🌐网速

+
+
+
+ { TESTING_LINKS.map((link, index) => ( +
+ { link.icon } + { linksTime[index] } +
+ )) }
-
- - - - { linksTime[1] } -
-
- - - - - { linksTime[2] } -
-
- - - - - - - { linksTime[3] } +
+