diff --git a/src/components/setting/mods/sysproxy-tooltip.tsx b/src/components/setting/mods/sysproxy-tooltip.tsx deleted file mode 100644 index 5641fa0..0000000 --- a/src/components/setting/mods/sysproxy-tooltip.tsx +++ /dev/null @@ -1,56 +0,0 @@ -import { useEffect, useState } from "react"; -import { InfoRounded } from "@mui/icons-material"; -import { ClickAwayListener, Tooltip } from "@mui/material"; -import { getSystemProxy } from "@/services/cmds"; - -const SysproxyTooltip = () => { - const [open, setOpen] = useState(false); - const [info, setInfo] = useState({}); - - const onShow = async () => { - const data = await getSystemProxy(); - setInfo(data ?? {}); - setOpen(true); - }; - - useEffect(() => { - if (!open) return; - const timer = setTimeout(() => setOpen(false), 2000); - return () => clearTimeout(timer); - }, [open]); - - // todo: add error info - const showTitle = ( -
-
Enable: {(!!info.enable).toString()}
-
Server: {info.server}
-
Bypass: {info.bypass}
-
- ); - - return ( - setOpen(false)}> - setOpen(false)} - open={open} - disableFocusListener - disableHoverListener - disableTouchListener - placement="top" - title={showTitle} - arrow - > - - - - ); -}; - -export default SysproxyTooltip; diff --git a/src/components/setting/mods/sysproxy-viewer.tsx b/src/components/setting/mods/sysproxy-viewer.tsx new file mode 100644 index 0000000..ec7939e --- /dev/null +++ b/src/components/setting/mods/sysproxy-viewer.tsx @@ -0,0 +1,204 @@ +import useSWR from "swr"; +import { useEffect, useState } from "react"; +import { useLockFn } from "ahooks"; +import { useTranslation } from "react-i18next"; +import { + Box, + Button, + Dialog, + DialogActions, + DialogContent, + DialogTitle, + InputAdornment, + List, + ListItem, + ListItemText, + styled, + Switch, + TextField, + Typography, +} from "@mui/material"; +import { + getSystemProxy, + getVergeConfig, + patchVergeConfig, +} from "@/services/cmds"; +import { ModalHandler } from "@/hooks/use-modal-handler"; +import Notice from "@/components/base/base-notice"; + +interface Props { + handler: ModalHandler; +} + +const FlexBox = styled("div")` + display: flex; + margin-top: 4px; + + .label { + flex: none; + width: 80px; + } +`; + +const SysproxyViewer = ({ handler }: Props) => { + const { t } = useTranslation(); + + const [open, setOpen] = useState(false); + + if (handler) { + handler.current = { + open: () => setOpen(true), + close: () => setOpen(false), + }; + } + + const { data: vergeConfig, mutate: mutateVerge } = useSWR( + "getVergeConfig", + getVergeConfig + ); + + const { + enable_system_proxy: enabled, + enable_proxy_guard, + system_proxy_bypass, + proxy_guard_duration, + } = vergeConfig ?? {}; + + const { data: sysproxy } = useSWR( + open ? "getSystemProxy" : null, + getSystemProxy + ); + + const [value, setValue] = useState({ + guard: enable_proxy_guard, + bypass: system_proxy_bypass, + duration: proxy_guard_duration ?? 10, + }); + + useEffect(() => { + setValue({ + guard: enable_proxy_guard, + bypass: system_proxy_bypass, + duration: proxy_guard_duration ?? 10, + }); + }, [vergeConfig]); + + const onSave = useLockFn(async () => { + if (value.duration < 5) { + Notice.error("Proxy guard duration at least 5 seconds"); + return; + } + + const patch: Partial = {}; + + if (value.guard !== enable_proxy_guard) { + patch.enable_proxy_guard = value.guard; + } + if (value.duration !== proxy_guard_duration) { + patch.proxy_guard_duration = value.duration; + } + if (value.bypass !== system_proxy_bypass) { + patch.system_proxy_bypass = value.bypass; + } + + try { + await patchVergeConfig(patch); + mutateVerge(); + setOpen(false); + } catch (err: any) { + Notice.error(err.message || err.toString()); + } + }); + + return ( + setOpen(false)}> + {t("System Proxy Setting")} + + + + + + setValue((v) => ({ ...v, guard: e }))} + /> + + + + + s, + }} + onChange={(e) => { + setValue((v) => ({ + ...v, + duration: +e.target.value.replace(/\D/, ""), + })); + }} + /> + + + + + + setValue((v) => ({ ...v, bypass: e.target.value })) + } + /> + + + + + + {t("Current System Proxy")} + + + + Enable: + + {(!!sysproxy?.enable).toString()} + + + + + Server: + {sysproxy?.server || "-"} + + + + Bypass: + {sysproxy?.bypass || "-"} + + + + + + + + + + ); +}; + +export default SysproxyViewer; diff --git a/src/components/setting/setting-system.tsx b/src/components/setting/setting-system.tsx index 7ff8aba..1d15bee 100644 --- a/src/components/setting/setting-system.tsx +++ b/src/components/setting/setting-system.tsx @@ -1,18 +1,19 @@ -import useSWR, { useSWRConfig } from "swr"; +import useSWR from "swr"; import { useState } from "react"; import { useTranslation } from "react-i18next"; -import { IconButton, Switch, TextField } from "@mui/material"; -import { ArrowForward, PrivacyTipRounded } from "@mui/icons-material"; +import { IconButton, Switch } from "@mui/material"; +import { ArrowForward, PrivacyTipRounded, Settings } from "@mui/icons-material"; import { checkService, getVergeConfig, patchVergeConfig, } from "@/services/cmds"; import { SettingList, SettingItem } from "./setting"; +import useModalHandler from "@/hooks/use-modal-handler"; import getSystem from "@/utils/get-system"; import GuardState from "./mods/guard-state"; import ServiceMode from "./mods/service-mode"; -import SysproxyTooltip from "./mods/sysproxy-tooltip"; +import SysproxyViewer from "./mods/sysproxy-viewer"; interface Props { onError?: (err: Error) => void; @@ -22,8 +23,11 @@ const isWIN = getSystem() === "windows"; const SettingSystem = ({ onError }: Props) => { const { t } = useTranslation(); - const { mutate } = useSWRConfig(); - const { data: vergeConfig } = useSWR("getVergeConfig", getVergeConfig); + + const { data: vergeConfig, mutate: mutateVerge } = useSWR( + "getVergeConfig", + getVergeConfig + ); // service mode const [serviceOpen, setServiceOpen] = useState(false); @@ -39,17 +43,19 @@ const SettingSystem = ({ onError }: Props) => { enable_service_mode, enable_silent_start, enable_system_proxy, - system_proxy_bypass, - enable_proxy_guard, } = vergeConfig ?? {}; const onSwitchFormat = (_e: any, value: boolean) => value; const onChangeData = (patch: Partial) => { - mutate("getVergeConfig", { ...vergeConfig, ...patch }, false); + mutateVerge({ ...vergeConfig, ...patch }, false); }; + const sysproxyHandler = useModalHandler(); + return ( + + { /> )} + sysproxyHandler.current.open()} + /> + } + > + onChangeData({ enable_system_proxy: e })} + onGuard={async (e) => { + await patchVergeConfig({ enable_system_proxy: e }); + mutateVerge(); // update bypass value + }} + > + + + + { - - }> - onChangeData({ enable_system_proxy: e })} - onGuard={async (e) => { - await patchVergeConfig({ enable_system_proxy: e }); - mutate("getVergeConfig"); // update bypass value - }} - > - - - - - {enable_system_proxy && ( - - onChangeData({ enable_proxy_guard: e })} - onGuard={(e) => patchVergeConfig({ enable_proxy_guard: e })} - > - - - - )} - - {enable_system_proxy && ( - - e.target.value} - onChange={(e) => onChangeData({ system_proxy_bypass: e })} - onGuard={(e) => patchVergeConfig({ system_proxy_bypass: e })} - waitTime={1000} - > - - - - )} ); }; diff --git a/src/locales/en.json b/src/locales/en.json index 037e1b9..cbaab63 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -65,8 +65,11 @@ "Auto Launch": "Auto Launch", "Silent Start": "Silent Start", "System Proxy": "System Proxy", + "System Proxy Setting": "System Proxy Setting", "Proxy Guard": "Proxy Guard", + "Guard Duration": "Guard Duration", "Proxy Bypass": "Proxy Bypass", + "Current System Proxy": "Current System Proxy", "Theme Mode": "Theme Mode", "Theme Blur": "Theme Blur", "Theme Setting": "Theme Setting", diff --git a/src/locales/zh.json b/src/locales/zh.json index 30f67f4..05cd83b 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -65,8 +65,11 @@ "Auto Launch": "开机自启", "Silent Start": "静默启动", "System Proxy": "系统代理", + "System Proxy Setting": "系统代理设置", "Proxy Guard": "系统代理守卫", + "Guard Duration": "代理守卫间隔", "Proxy Bypass": "Proxy Bypass", + "Current System Proxy": "当前系统代理", "Theme Mode": "主题模式", "Theme Blur": "背景模糊", "Theme Setting": "主题设置", diff --git a/src/services/cmds.ts b/src/services/cmds.ts index e4f9ebf..d99c60a 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -121,7 +121,11 @@ export async function patchVergeConfig(payload: CmdType.VergeConfig) { } export async function getSystemProxy() { - return invoke("get_sys_proxy"); + return invoke<{ + enable: boolean; + server: string; + bypass: string; + }>("get_sys_proxy"); } export async function changeClashCore(clashCore: string) { diff --git a/src/services/types.d.ts b/src/services/types.d.ts index 37115a6..fa26048 100644 --- a/src/services/types.d.ts +++ b/src/services/types.d.ts @@ -144,6 +144,7 @@ declare namespace CmdType { enable_silent_start?: boolean; enable_system_proxy?: boolean; enable_proxy_guard?: boolean; + proxy_guard_duration?: number; system_proxy_bypass?: string; web_ui_list?: string[]; theme_setting?: {