feat: toggle log ws
This commit is contained in:
parent
3bd9287c5d
commit
4213ee660f
@ -5,15 +5,20 @@ import { listen } from "@tauri-apps/api/event";
|
|||||||
import { getInformation } from "@/services/api";
|
import { getInformation } from "@/services/api";
|
||||||
import { getClashLogs } from "@/services/cmds";
|
import { getClashLogs } from "@/services/cmds";
|
||||||
import { atomLogData } from "@/services/states";
|
import { atomLogData } from "@/services/states";
|
||||||
|
import useLogToggle from "./use-log-toggle";
|
||||||
|
|
||||||
const MAX_LOG_NUM = 1000;
|
const MAX_LOG_NUM = 1000;
|
||||||
|
|
||||||
// setup the log websocket
|
// setup the log websocket
|
||||||
export default function useLogSetup() {
|
export default function useLogSetup() {
|
||||||
const [refresh, setRefresh] = useState({});
|
const [refresh, setRefresh] = useState({});
|
||||||
|
|
||||||
|
const [enableLog] = useLogToggle();
|
||||||
const setLogData = useSetRecoilState(atomLogData);
|
const setLogData = useSetRecoilState(atomLogData);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!enableLog) return;
|
||||||
|
|
||||||
getClashLogs().then(setLogData);
|
getClashLogs().then(setLogData);
|
||||||
|
|
||||||
let ws: WebSocket = null!;
|
let ws: WebSocket = null!;
|
||||||
@ -41,5 +46,5 @@ export default function useLogSetup() {
|
|||||||
ws?.close();
|
ws?.close();
|
||||||
unlisten?.then((fn) => fn());
|
unlisten?.then((fn) => fn());
|
||||||
};
|
};
|
||||||
}, [refresh]);
|
}, [refresh, enableLog]);
|
||||||
}
|
}
|
||||||
|
24
src/components/layout/use-log-toggle.ts
Normal file
24
src/components/layout/use-log-toggle.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { useEffect } from "react";
|
||||||
|
import { useRecoilState } from "recoil";
|
||||||
|
import { atomEnableLog } from "@/services/states";
|
||||||
|
|
||||||
|
const LOG_KEY = "enable-log";
|
||||||
|
|
||||||
|
export default function useLogToggle() {
|
||||||
|
const [enableLog, setEnableLog] = useRecoilState(atomEnableLog);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
try {
|
||||||
|
setEnableLog(localStorage.getItem(LOG_KEY) !== "false");
|
||||||
|
} catch {}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const setter = (enable: boolean) => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(LOG_KEY, enable.toString());
|
||||||
|
} catch {}
|
||||||
|
setEnableLog(enable);
|
||||||
|
};
|
||||||
|
|
||||||
|
return [enableLog, setter];
|
||||||
|
}
|
@ -1,16 +1,29 @@
|
|||||||
import { useMemo, useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import { useRecoilState } from "recoil";
|
import { useRecoilState } from "recoil";
|
||||||
import { Box, Button, MenuItem, Paper, Select, TextField } from "@mui/material";
|
import {
|
||||||
|
Box,
|
||||||
|
Button,
|
||||||
|
IconButton,
|
||||||
|
MenuItem,
|
||||||
|
Paper,
|
||||||
|
Select,
|
||||||
|
TextField,
|
||||||
|
} from "@mui/material";
|
||||||
import { Virtuoso } from "react-virtuoso";
|
import { Virtuoso } from "react-virtuoso";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { atomLogData } from "@/services/states";
|
import {
|
||||||
|
PlayCircleOutlineRounded,
|
||||||
|
PauseCircleOutlineRounded,
|
||||||
|
} from "@mui/icons-material";
|
||||||
|
import { atomEnableLog, atomLogData } from "@/services/states";
|
||||||
import BasePage from "@/components/base/base-page";
|
import BasePage from "@/components/base/base-page";
|
||||||
import LogItem from "@/components/log/log-item";
|
|
||||||
import BaseEmpty from "@/components/base/base-empty";
|
import BaseEmpty from "@/components/base/base-empty";
|
||||||
|
import LogItem from "@/components/log/log-item";
|
||||||
|
|
||||||
const LogPage = () => {
|
const LogPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [logData, setLogData] = useRecoilState(atomLogData);
|
const [logData, setLogData] = useRecoilState(atomLogData);
|
||||||
|
const [enableLog, setEnableLog] = useRecoilState(atomEnableLog);
|
||||||
|
|
||||||
const [logState, setLogState] = useState("all");
|
const [logState, setLogState] = useState("all");
|
||||||
const [filterText, setFilterText] = useState("");
|
const [filterText, setFilterText] = useState("");
|
||||||
@ -29,14 +42,27 @@ const LogPage = () => {
|
|||||||
title={t("Logs")}
|
title={t("Logs")}
|
||||||
contentStyle={{ height: "100%" }}
|
contentStyle={{ height: "100%" }}
|
||||||
header={
|
header={
|
||||||
<Button
|
<Box sx={{ mt: 1, display: "flex", alignItems: "center" }}>
|
||||||
size="small"
|
<IconButton
|
||||||
sx={{ mt: 1 }}
|
size="small"
|
||||||
variant="contained"
|
sx={{ mr: 2 }}
|
||||||
onClick={() => setLogData([])}
|
onClick={() => setEnableLog((e) => !e)}
|
||||||
>
|
>
|
||||||
{t("Clear")}
|
{enableLog ? (
|
||||||
</Button>
|
<PauseCircleOutlineRounded />
|
||||||
|
) : (
|
||||||
|
<PlayCircleOutlineRounded />
|
||||||
|
)}
|
||||||
|
</IconButton>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
variant="contained"
|
||||||
|
onClick={() => setLogData([])}
|
||||||
|
>
|
||||||
|
{t("Clear")}
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<Paper sx={{ boxSizing: "border-box", boxShadow: 2, height: "100%" }}>
|
<Paper sx={{ boxSizing: "border-box", boxShadow: 2, height: "100%" }}>
|
||||||
|
@ -15,6 +15,11 @@ export const atomLogData = atom<ApiType.LogItem[]>({
|
|||||||
default: [],
|
default: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const atomEnableLog = atom<boolean>({
|
||||||
|
key: "atomEnableLog",
|
||||||
|
default: true,
|
||||||
|
});
|
||||||
|
|
||||||
// save the state of each profile item loading
|
// save the state of each profile item loading
|
||||||
export const atomLoadingCache = atom<Record<string, boolean>>({
|
export const atomLoadingCache = atom<Record<string, boolean>>({
|
||||||
key: "atomLoadingCache",
|
key: "atomLoadingCache",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user