import { forwardRef, useImperativeHandle, useState } from "react"; import { useLockFn } from "ahooks"; import { useTranslation } from "react-i18next"; import { Box, InputAdornment, List, ListItem, ListItemText, styled, Switch, TextField, Typography, } from "@mui/material"; import { useVerge } from "@/hooks/use-verge"; import { getSystemProxy } from "@/services/cmds"; import { BaseDialog, DialogRef, Notice } from "@/components/base"; export const SysproxyViewer = forwardRef((props, ref) => { const { t } = useTranslation(); const [open, setOpen] = useState(false); const { verge, patchVerge } = useVerge(); type SysProxy = Awaited>; const [sysproxy, setSysproxy] = useState(); const { enable_system_proxy: enabled, enable_proxy_guard, system_proxy_bypass, proxy_guard_duration, } = verge ?? {}; const [value, setValue] = useState({ guard: enable_proxy_guard, bypass: system_proxy_bypass, duration: proxy_guard_duration ?? 10, }); useImperativeHandle(ref, () => ({ open: () => { setOpen(true); setValue({ guard: enable_proxy_guard, bypass: system_proxy_bypass, duration: proxy_guard_duration ?? 10, }); getSystemProxy().then((p) => setSysproxy(p)); }, close: () => setOpen(false), })); const onSave = useLockFn(async () => { if (value.duration < 1) { Notice.error("Proxy guard duration at least 1 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 patchVerge(patch); setOpen(false); } catch (err: any) { Notice.error(err.message || err.toString()); } }); return ( setOpen(false)} onCancel={() => setOpen(false)} onOk={onSave} > 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")} {t("Enable status")} {(!!sysproxy?.enable).toString()} {t("Server Addr")} {sysproxy?.server || "-"} {t("Bypass")} {sysproxy?.bypass || "-"} ); }); const FlexBox = styled("div")` display: flex; margin-top: 4px; .label { flex: none; width: 85px; } `;