Files
clash-verge-rev/src/pages/connections.tsx
T

89 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-12-25 22:33:29 +08:00
import { useEffect, useState } from "react";
2022-03-18 14:43:22 +08:00
import { useLockFn } from "ahooks";
import { Button, Paper } from "@mui/material";
2021-12-25 22:33:29 +08:00
import { Virtuoso } from "react-virtuoso";
2022-03-12 23:07:45 +08:00
import { useTranslation } from "react-i18next";
2021-12-25 22:33:29 +08:00
import { ApiType } from "../services/types";
2022-03-18 14:43:22 +08:00
import { closeAllConnections, getInfomation } from "../services/api";
2022-02-13 19:27:06 +08:00
import BasePage from "../components/base/base-page";
import ConnectionItem from "../components/connection/connection-item";
2021-12-15 01:52:46 +08:00
2021-12-08 23:36:34 +08:00
const ConnectionsPage = () => {
2021-12-25 22:33:29 +08:00
const initConn = { uploadTotal: 0, downloadTotal: 0, connections: [] };
2022-03-12 23:07:45 +08:00
const { t } = useTranslation();
2022-03-23 14:02:08 +08:00
const [connData, setConnData] = useState<ApiType.Connections>(initConn);
2021-12-15 01:52:46 +08:00
2021-12-25 22:33:29 +08:00
useEffect(() => {
2021-12-25 23:20:59 +08:00
let ws: WebSocket | null = null;
2021-12-25 22:33:29 +08:00
2021-12-25 23:20:59 +08:00
getInfomation().then((result) => {
const { server = "", secret = "" } = result;
ws = new WebSocket(`ws://${server}/connections?token=${secret}`);
ws.addEventListener("message", (event) => {
const data = JSON.parse(event.data) as ApiType.Connections;
2022-03-23 14:02:08 +08:00
setConnData((old) => {
const oldConn = old.connections;
const oldList = oldConn.map((each) => each.id);
const maxLen = data.connections.length;
const connections: typeof oldConn = [];
// 与前一次连接的顺序尽量保持一致
data.connections
.filter((each) => {
const index = oldList.indexOf(each.id);
if (index >= 0 && index < maxLen) {
connections[index] = each;
return false;
}
return true;
})
.forEach((each) => {
for (let i = 0; i < maxLen; ++i) {
if (!connections[i]) {
connections[i] = each;
return;
}
}
});
return { ...data, connections };
});
2021-12-25 23:20:59 +08:00
});
2021-12-25 22:33:29 +08:00
});
2021-12-25 23:20:59 +08:00
return () => ws?.close();
2021-12-15 01:52:46 +08:00
}, []);
2022-03-18 14:43:22 +08:00
const onCloseAll = useLockFn(closeAllConnections);
2021-12-15 01:52:46 +08:00
return (
2022-03-18 14:43:22 +08:00
<BasePage
title={t("Connections")}
contentStyle={{ height: "100%" }}
header={
<Button
size="small"
sx={{ mt: 1 }}
variant="contained"
onClick={onCloseAll}
>
{t("Close All")}
</Button>
}
>
2022-01-16 03:11:07 +08:00
<Paper sx={{ boxShadow: 2, height: "100%" }}>
2021-12-25 22:33:29 +08:00
<Virtuoso
2022-03-18 14:43:22 +08:00
initialTopMostItemIndex={999}
2022-03-23 14:02:08 +08:00
data={connData.connections}
2021-12-25 22:33:29 +08:00
itemContent={(index, item) => <ConnectionItem value={item} />}
/>
</Paper>
2022-01-16 03:11:07 +08:00
</BasePage>
2021-12-15 01:52:46 +08:00
);
2021-12-08 23:36:34 +08:00
};
export default ConnectionsPage;