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;