feat: Add pause/resume functionality for connection tracking

This commit is contained in:
wonfen 2025-02-18 08:14:09 +08:00
parent 2dd646537e
commit 19e623d7c2

View File

@ -3,7 +3,12 @@ import { useLockFn } from "ahooks";
import { Box, Button, IconButton, MenuItem } from "@mui/material"; import { Box, Button, IconButton, MenuItem } from "@mui/material";
import { Virtuoso } from "react-virtuoso"; import { Virtuoso } from "react-virtuoso";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { TableChartRounded, TableRowsRounded } from "@mui/icons-material"; import {
TableChartRounded,
TableRowsRounded,
PlayCircleOutlineRounded,
PauseCircleOutlineRounded,
} from "@mui/icons-material";
import { closeAllConnections } from "@/services/api"; import { closeAllConnections } from "@/services/api";
import { useConnectionSetting } from "@/services/states"; import { useConnectionSetting } from "@/services/states";
import { useClashInfo } from "@/hooks/use-clash"; import { useClashInfo } from "@/hooks/use-clash";
@ -58,6 +63,9 @@ const ConnectionsPage = () => {
list.sort((a, b) => b.curDownload! - a.curDownload!), list.sort((a, b) => b.curDownload! - a.curDownload!),
}; };
const [isPaused, setIsPaused] = useState(false);
const [frozenData, setFrozenData] = useState<IConnections | null>(null);
const { data: connData = initConn } = useSWRSubscription< const { data: connData = initConn } = useSWRSubscription<
IConnections, IConnections,
any, any,
@ -70,9 +78,7 @@ const ConnectionsPage = () => {
`ws://${server}/connections?token=${encodeURIComponent(secret)}`, `ws://${server}/connections?token=${encodeURIComponent(secret)}`,
{ {
onmessage(event) { onmessage(event) {
// meta v1.15.0 出现 data.connections 为 null 的情况
const data = JSON.parse(event.data) as IConnections; const data = JSON.parse(event.data) as IConnections;
// 尽量与前一次 connections 的展示顺序保持一致
next(null, (old = initConn) => { next(null, (old = initConn) => {
const oldConn = old.connections; const oldConn = old.connections;
const maxLen = data.connections?.length; const maxLen = data.connections?.length;
@ -117,9 +123,13 @@ const ConnectionsPage = () => {
}, },
); );
const displayData = useMemo(() => {
return isPaused ? (frozenData ?? connData) : connData;
}, [isPaused, frozenData, connData]);
const [filterConn, download, upload] = useMemo(() => { const [filterConn, download, upload] = useMemo(() => {
const orderFunc = orderOpts[curOrderOpt]; const orderFunc = orderOpts[curOrderOpt];
let connections = connData.connections.filter((conn) => let connections = displayData.connections.filter((conn) =>
match(conn.metadata.host || conn.metadata.destinationIP || ""), match(conn.metadata.host || conn.metadata.destinationIP || ""),
); );
@ -132,7 +142,7 @@ const ConnectionsPage = () => {
upload += x.upload; upload += x.upload;
}); });
return [connections, download, upload]; return [connections, download, upload];
}, [connData, match, curOrderOpt]); }, [displayData, match, curOrderOpt]);
const onCloseAll = useLockFn(closeAllConnections); const onCloseAll = useLockFn(closeAllConnections);
@ -142,6 +152,17 @@ const ConnectionsPage = () => {
setMatch(() => match); setMatch(() => match);
}, []); }, []);
const handlePauseToggle = useCallback(() => {
setIsPaused((prev) => {
if (!prev) {
setFrozenData(connData);
} else {
setFrozenData(null);
}
return !prev;
});
}, [connData]);
return ( return (
<BasePage <BasePage
full full
@ -151,8 +172,6 @@ const ConnectionsPage = () => {
display: "flex", display: "flex",
flexDirection: "column", flexDirection: "column",
overflow: "auto", overflow: "auto",
// margin: "0 10px",
// backgroundColor: isDark ? "#282a36" : "#ffffff",
borderRadius: "8px", borderRadius: "8px",
}} }}
header={ header={
@ -175,16 +194,23 @@ const ConnectionsPage = () => {
} }
> >
{isTableLayout ? ( {isTableLayout ? (
<span title={t("List View")}> <TableRowsRounded titleAccess={t("List View")} />
<TableRowsRounded fontSize="inherit" />
</span>
) : ( ) : (
<span title={t("Table View")}> <TableChartRounded titleAccess={t("Table View")} />
<TableChartRounded fontSize="inherit" /> )}
</span> </IconButton>
<IconButton
color="inherit"
size="small"
onClick={handlePauseToggle}
title={isPaused ? t("Resume") : t("Pause")}
>
{isPaused ? (
<PlayCircleOutlineRounded />
) : (
<PauseCircleOutlineRounded />
)} )}
</IconButton> </IconButton>
<Button size="small" variant="contained" onClick={onCloseAll}> <Button size="small" variant="contained" onClick={onCloseAll}>
<span style={{ whiteSpace: "nowrap" }}>{t("Close All")}</span> <span style={{ whiteSpace: "nowrap" }}>{t("Close All")}</span>
</Button> </Button>
@ -200,7 +226,6 @@ const ConnectionsPage = () => {
display: "flex", display: "flex",
alignItems: "center", alignItems: "center",
userSelect: "text", userSelect: "text",
// backgroundColor: isDark ? "#282a36" : "#ffffff",
position: "sticky", position: "sticky",
top: 0, top: 0,
zIndex: 2, zIndex: 2,
@ -232,7 +257,6 @@ const ConnectionsPage = () => {
<Virtuoso <Virtuoso
style={{ style={{
flex: 1, flex: 1,
// backgroundColor: isDark ? "#282a36" : "#ffffff",
borderRadius: "8px", borderRadius: "8px",
}} }}
data={filterConn} data={filterConn}