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

61 lines
1.7 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();
2021-12-25 22:33:29 +08:00
const [conn, setConn] = 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;
setConn(data);
});
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}
2021-12-25 22:33:29 +08:00
data={conn.connections}
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;