fix: use verge hook
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import useSWR, { useSWRConfig } from "swr";
|
||||
import { mutate } from "swr";
|
||||
import { useState } from "react";
|
||||
import { useLockFn } from "ahooks";
|
||||
import { Menu, MenuItem } from "@mui/material";
|
||||
import { Settings } from "@mui/icons-material";
|
||||
import { changeClashCore, getVergeConfig } from "@/services/cmds";
|
||||
import { changeClashCore } from "@/services/cmds";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import Notice from "@/components/base/base-notice";
|
||||
|
||||
const VALID_CORE = [
|
||||
@@ -12,21 +13,19 @@ const VALID_CORE = [
|
||||
];
|
||||
|
||||
const CoreSwitch = () => {
|
||||
const { mutate } = useSWRConfig();
|
||||
|
||||
const { data: vergeConfig } = useSWR("getVergeConfig", getVergeConfig);
|
||||
const { verge, mutateVerge } = useVerge();
|
||||
|
||||
const [anchorEl, setAnchorEl] = useState<any>(null);
|
||||
const [position, setPosition] = useState({ left: 0, top: 0 });
|
||||
|
||||
const { clash_core = "clash" } = vergeConfig ?? {};
|
||||
const { clash_core = "clash" } = verge ?? {};
|
||||
|
||||
const onCoreChange = useLockFn(async (core: string) => {
|
||||
if (core === clash_core) return;
|
||||
|
||||
try {
|
||||
await changeClashCore(core);
|
||||
mutate("getVergeConfig");
|
||||
mutateVerge();
|
||||
setTimeout(() => {
|
||||
mutate("getClashConfig");
|
||||
mutate("getVersion");
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import useSWR from "swr";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useLockFn } from "ahooks";
|
||||
@@ -11,7 +10,7 @@ import {
|
||||
styled,
|
||||
Typography,
|
||||
} from "@mui/material";
|
||||
import { getVergeConfig, patchVergeConfig } from "@/services/cmds";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { ModalHandler } from "@/hooks/use-modal-handler";
|
||||
import Notice from "@/components/base/base-notice";
|
||||
import HotkeyInput from "./hotkey-input";
|
||||
@@ -51,10 +50,7 @@ const HotkeyViewer = ({ handler }: Props) => {
|
||||
};
|
||||
}
|
||||
|
||||
const { data: vergeConfig, mutate: mutateVerge } = useSWR(
|
||||
"getVergeConfig",
|
||||
getVergeConfig
|
||||
);
|
||||
const { verge, patchVerge } = useVerge();
|
||||
|
||||
const [hotkeyMap, setHotkeyMap] = useState<Record<string, string[]>>({});
|
||||
|
||||
@@ -62,7 +58,7 @@ const HotkeyViewer = ({ handler }: Props) => {
|
||||
if (!open) return;
|
||||
const map = {} as typeof hotkeyMap;
|
||||
|
||||
vergeConfig?.hotkeys?.forEach((text) => {
|
||||
verge?.hotkeys?.forEach((text) => {
|
||||
const [func, key] = text.split(",").map((e) => e.trim());
|
||||
|
||||
if (!func || !key) return;
|
||||
@@ -74,7 +70,7 @@ const HotkeyViewer = ({ handler }: Props) => {
|
||||
});
|
||||
|
||||
setHotkeyMap(map);
|
||||
}, [vergeConfig?.hotkeys, open]);
|
||||
}, [verge?.hotkeys, open]);
|
||||
|
||||
const onSave = useLockFn(async () => {
|
||||
const hotkeys = Object.entries(hotkeyMap)
|
||||
@@ -93,9 +89,8 @@ const HotkeyViewer = ({ handler }: Props) => {
|
||||
.filter(Boolean);
|
||||
|
||||
try {
|
||||
patchVergeConfig({ hotkeys });
|
||||
patchVerge({ hotkeys });
|
||||
setOpen(false);
|
||||
mutateVerge();
|
||||
} catch (err: any) {
|
||||
Notice.error(err.message || err.toString());
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
TextField,
|
||||
} from "@mui/material";
|
||||
import { ModalHandler } from "@/hooks/use-modal-handler";
|
||||
import { useVergeConfig } from "@/hooks/use-verge-config";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import Notice from "@/components/base/base-notice";
|
||||
|
||||
interface Props {
|
||||
@@ -23,7 +23,7 @@ interface Props {
|
||||
|
||||
const MiscViewer = ({ handler }: Props) => {
|
||||
const { t } = useTranslation();
|
||||
const { data, patchVerge } = useVergeConfig();
|
||||
const { verge, patchVerge } = useVerge();
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [values, setValues] = useState({
|
||||
@@ -41,11 +41,11 @@ const MiscViewer = ({ handler }: Props) => {
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setValues({
|
||||
autoCloseConnection: data?.auto_close_connection || false,
|
||||
defaultLatencyTest: data?.default_latency_test || "",
|
||||
autoCloseConnection: verge?.auto_close_connection || false,
|
||||
defaultLatencyTest: verge?.default_latency_test || "",
|
||||
});
|
||||
}
|
||||
}, [open, data]);
|
||||
}, [open, verge]);
|
||||
|
||||
const onSave = useLockFn(async () => {
|
||||
try {
|
||||
|
||||
@@ -18,11 +18,8 @@ import {
|
||||
TextField,
|
||||
Typography,
|
||||
} from "@mui/material";
|
||||
import {
|
||||
getSystemProxy,
|
||||
getVergeConfig,
|
||||
patchVergeConfig,
|
||||
} from "@/services/cmds";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { getSystemProxy } from "@/services/cmds";
|
||||
import { ModalHandler } from "@/hooks/use-modal-handler";
|
||||
import Notice from "@/components/base/base-notice";
|
||||
|
||||
@@ -52,17 +49,14 @@ const SysproxyViewer = ({ handler }: Props) => {
|
||||
};
|
||||
}
|
||||
|
||||
const { data: vergeConfig, mutate: mutateVerge } = useSWR(
|
||||
"getVergeConfig",
|
||||
getVergeConfig
|
||||
);
|
||||
const { verge, patchVerge } = useVerge();
|
||||
|
||||
const {
|
||||
enable_system_proxy: enabled,
|
||||
enable_proxy_guard,
|
||||
system_proxy_bypass,
|
||||
proxy_guard_duration,
|
||||
} = vergeConfig ?? {};
|
||||
} = verge ?? {};
|
||||
|
||||
const { data: sysproxy } = useSWR(
|
||||
open ? "getSystemProxy" : null,
|
||||
@@ -81,7 +75,7 @@ const SysproxyViewer = ({ handler }: Props) => {
|
||||
bypass: system_proxy_bypass,
|
||||
duration: proxy_guard_duration ?? 10,
|
||||
});
|
||||
}, [vergeConfig]);
|
||||
}, [verge]);
|
||||
|
||||
const onSave = useLockFn(async () => {
|
||||
if (value.duration < 5) {
|
||||
@@ -102,8 +96,7 @@ const SysproxyViewer = ({ handler }: Props) => {
|
||||
}
|
||||
|
||||
try {
|
||||
await patchVergeConfig(patch);
|
||||
mutateVerge();
|
||||
await patchVerge(patch);
|
||||
setOpen(false);
|
||||
} catch (err: any) {
|
||||
Notice.error(err.message || err.toString());
|
||||
|
||||
@@ -10,12 +10,8 @@ import {
|
||||
DialogTitle,
|
||||
Typography,
|
||||
} from "@mui/material";
|
||||
import {
|
||||
getClashInfo,
|
||||
getVergeConfig,
|
||||
openWebUrl,
|
||||
patchVergeConfig,
|
||||
} from "@/services/cmds";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { getClashInfo, openWebUrl } from "@/services/cmds";
|
||||
import { ModalHandler } from "@/hooks/use-modal-handler";
|
||||
import BaseEmpty from "@/components/base/base-empty";
|
||||
import WebUIItem from "./web-ui-item";
|
||||
@@ -27,12 +23,10 @@ interface Props {
|
||||
|
||||
const WebUIViewer = ({ handler, onError }: Props) => {
|
||||
const { t } = useTranslation();
|
||||
const { data: vergeConfig, mutate: mutateVerge } = useSWR(
|
||||
"getVergeConfig",
|
||||
getVergeConfig
|
||||
);
|
||||
|
||||
const webUIList = vergeConfig?.web_ui_list || [];
|
||||
const { verge, patchVerge, mutateVerge } = useVerge();
|
||||
|
||||
const webUIList = verge?.web_ui_list || [];
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [editing, setEditing] = useState(false);
|
||||
@@ -47,24 +41,21 @@ const WebUIViewer = ({ handler, onError }: Props) => {
|
||||
const handleAdd = useLockFn(async (value: string) => {
|
||||
const newList = [value, ...webUIList];
|
||||
mutateVerge((old) => (old ? { ...old, web_ui_list: newList } : old), false);
|
||||
await patchVergeConfig({ web_ui_list: newList });
|
||||
await mutateVerge();
|
||||
await patchVerge({ web_ui_list: newList });
|
||||
});
|
||||
|
||||
const handleChange = useLockFn(async (index: number, value?: string) => {
|
||||
const newList = [...webUIList];
|
||||
newList[index] = value ?? "";
|
||||
mutateVerge((old) => (old ? { ...old, web_ui_list: newList } : old), false);
|
||||
await patchVergeConfig({ web_ui_list: newList });
|
||||
await mutateVerge();
|
||||
await patchVerge({ web_ui_list: newList });
|
||||
});
|
||||
|
||||
const handleDelete = useLockFn(async (index: number) => {
|
||||
const newList = [...webUIList];
|
||||
newList.splice(index, 1);
|
||||
mutateVerge((old) => (old ? { ...old, web_ui_list: newList } : old), false);
|
||||
await patchVergeConfig({ web_ui_list: newList });
|
||||
await mutateVerge();
|
||||
await patchVerge({ web_ui_list: newList });
|
||||
});
|
||||
|
||||
const { data: clashInfo } = useSWR("getClashInfo", getClashInfo);
|
||||
|
||||
Reference in New Issue
Block a user