From 63d92a08721188dc9ccd133648cefb8c8d1b7cad Mon Sep 17 00:00:00 2001 From: FoundTheWOUT Date: Thu, 15 Sep 2022 15:52:12 +0800 Subject: [PATCH] Support ordering connection --- src/locales/zh.json | 6 +++- src/pages/connections.tsx | 59 ++++++++++++++++++++++++++++++++++++--- src/services/types.d.ts | 4 +-- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/locales/zh.json b/src/locales/zh.json index 05cd83b..5668480 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -87,5 +87,9 @@ "Back": "返回", "Save": "保存", - "Cancel": "取消" + "Cancel": "取消", + + "Default": "默认", + "Download Speed": "下载速度", + "Upload Speed": "上传速度" } diff --git a/src/pages/connections.tsx b/src/pages/connections.tsx index 3ddb727..759c376 100644 --- a/src/pages/connections.tsx +++ b/src/pages/connections.tsx @@ -1,6 +1,6 @@ import { useEffect, useMemo, useState } from "react"; import { useLockFn } from "ahooks"; -import { Box, Button, Paper, TextField } from "@mui/material"; +import { Box, Button, ButtonGroup, Paper, TextField } from "@mui/material"; import { Virtuoso } from "react-virtuoso"; import { useTranslation } from "react-i18next"; import { closeAllConnections, getInformation } from "@/services/api"; @@ -10,6 +10,14 @@ import BaseEmpty from "@/components/base/base-empty"; const initConn = { uploadTotal: 0, downloadTotal: 0, connections: [] }; +type IOderOpts = { + [key in string]: { + orderFunc( + connections: ApiType.ConnectionsItem[] + ): ApiType.ConnectionsItem[]; + }; +}; + const ConnectionsPage = () => { const { t } = useTranslation(); @@ -22,6 +30,25 @@ const ConnectionsPage = () => { ); }, [connData, filterText]); + const orderOpts: IOderOpts = { + Default: { + orderFunc: (list) => list, + }, + // "Download Traffic": { + // orderFunc: (list) => list, + // }, + // "Upload Traffic": { + // orderFunc: (list) => list, + // }, + "Download Speed": { + orderFunc: (list) => list.sort((a, b) => b.curDownload! - a.curDownload!), + }, + "Upload Speed": { + orderFunc: (list) => list.sort((a, b) => b.curUpload! - a.curUpload!), + }, + }; + const [curOrderOpt, setOrderOpt] = useState("Default"); + useEffect(() => { let ws: WebSocket | null = null; @@ -61,13 +88,19 @@ const ConnectionsPage = () => { } } - return { ...data, connections }; + const orderedConnections = + orderOpts[curOrderOpt].orderFunc(connections); + + return { + ...data, + connections: orderedConnections, + }; }); }); }); return () => ws?.close(); - }, []); + }, [curOrderOpt]); const onCloseAll = useLockFn(closeAllConnections); @@ -122,7 +155,25 @@ const ConnectionsPage = () => { /> - + + + {Object.keys(orderOpts).map((opt) => ( + + ))} + + + + {filterConn.length > 0 ? (