mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
feat: enhance log data
This commit is contained in:
@@ -7,9 +7,10 @@ import { listen } from "@tauri-apps/api/event";
|
||||
import { ApiType } from "../../services/types";
|
||||
import { getInfomation } from "../../services/api";
|
||||
import { getVergeConfig } from "../../services/cmds";
|
||||
import { atomClashPort } from "../../states/setting";
|
||||
import parseTraffic from "../../utils/parse-traffic";
|
||||
import { atomClashPort } from "../../services/states";
|
||||
import useLogSetup from "./use-log-setup";
|
||||
import useTrafficGraph from "./use-traffic-graph";
|
||||
import parseTraffic from "../../utils/parse-traffic";
|
||||
|
||||
const LayoutTraffic = () => {
|
||||
const portValue = useRecoilValue(atomClashPort);
|
||||
@@ -21,6 +22,9 @@ const LayoutTraffic = () => {
|
||||
const { data } = useSWR("getVergeConfig", getVergeConfig);
|
||||
const trafficGraph = data?.traffic_graph ?? true;
|
||||
|
||||
// setup log ws during layout
|
||||
useLogSetup();
|
||||
|
||||
useEffect(() => {
|
||||
let unlisten: () => void = null!;
|
||||
|
||||
|
||||
49
src/components/layout/use-log-setup.ts
Normal file
49
src/components/layout/use-log-setup.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import dayjs from "dayjs";
|
||||
import { useEffect } from "react";
|
||||
import { useSetRecoilState } from "recoil";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { ApiType } from "../../services/types";
|
||||
import { getInfomation } from "../../services/api";
|
||||
import { atomLogData } from "../../services/states";
|
||||
|
||||
const MAX_LOG_NUM = 1000;
|
||||
|
||||
// setup the log websocket
|
||||
export default function useLogSetup() {
|
||||
const setLogData = useSetRecoilState(atomLogData);
|
||||
|
||||
useEffect(() => {
|
||||
let ws: WebSocket = null!;
|
||||
let unlisten: () => void = null!;
|
||||
|
||||
const handler = (event: MessageEvent<any>) => {
|
||||
const data = JSON.parse(event.data) as ApiType.LogItem;
|
||||
const time = dayjs().format("MM-DD HH:mm:ss");
|
||||
setLogData((l) => {
|
||||
if (l.length >= MAX_LOG_NUM) l.shift();
|
||||
return [...l, { ...data, time }];
|
||||
});
|
||||
};
|
||||
|
||||
(async () => {
|
||||
const { server = "", secret = "" } = await getInfomation();
|
||||
|
||||
ws = new WebSocket(`ws://${server}/logs?token=${secret}`);
|
||||
ws.addEventListener("message", handler);
|
||||
|
||||
// reconnect the websocket
|
||||
unlisten = await listen("restart_clash", async () => {
|
||||
const { server = "", secret = "" } = await getInfomation();
|
||||
|
||||
ws?.close();
|
||||
ws = new WebSocket(`ws://${server}/logs?token=${secret}`);
|
||||
ws.addEventListener("message", handler);
|
||||
});
|
||||
})();
|
||||
|
||||
return () => {
|
||||
ws?.close();
|
||||
unlisten?.();
|
||||
};
|
||||
}, []);
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
Typography,
|
||||
} from "@mui/material";
|
||||
import { ApiType } from "../../services/types";
|
||||
import { atomClashPort } from "../../states/setting";
|
||||
import { atomClashPort } from "../../services/states";
|
||||
import { patchClashConfig } from "../../services/cmds";
|
||||
import { SettingList, SettingItem } from "./setting";
|
||||
import { getClashConfig, getVersion, updateConfigs } from "../../services/api";
|
||||
|
||||
Reference in New Issue
Block a user