Files
clash-verge-rev/src/hooks/use-system-proxy-state.ts
T
oomeow 7fc238c27b refactor: invock mihomo api by use tauri-plugin-mihomo (#4926)
* feat: add tauri-plugin-mihomo

* refactor: invock mihomo api by use tauri-plugin-mihomo

* chore: todo

* chore: update

* chore: update

* chore: update

* chore: update

* fix: incorrect delay status and update pretty config

* chore: update

* chore: remove cache

* chore: update

* chore: update

* fix: app freezed when change group proxy

* chore: update

* chore: update

* chore: add rustfmt.toml to tauri-plugin-mihomo

* chore: happy clippy

* refactor: connect mihomo websocket

* chore: update

* chore: update

* fix: parse bigint to number

* chore: update

* Revert "fix: parse bigint to number"

This reverts commit 74c006522e.

* chore: use number instead of bigint

* chore: cleanup

* fix: rule data not refresh when switch profile

* chore: update

* chore: cleanup

* chore: update

* fix: traffic graph data display

* feat: add ipc connection pool

* chore: update

* chore: clippy

* fix: incorrect delay status

* fix: typo

* fix: empty proxies tray menu

* chore: clippy

* chore: import tauri-plugin-mihomo by using git repo

* chore: cleanup

* fix: mihomo api

* fix: incorrect delay status

* chore: update tauri-plugin-mihomo dep

chore: update
2025-10-08 12:32:40 +08:00

80 lines
2.2 KiB
TypeScript

import useSWR, { mutate } from "swr";
import { closeAllConnections } from "tauri-plugin-mihomo-api";
import { useVerge } from "@/hooks/use-verge";
import { useAppData } from "@/providers/app-data-context";
import { getAutotemProxy } from "@/services/cmds";
// 系统代理状态检测统一逻辑
export const useSystemProxyState = () => {
const { verge, mutateVerge, patchVerge } = useVerge();
const { sysproxy } = useAppData();
const { data: autoproxy } = useSWR("getAutotemProxy", getAutotemProxy, {
revalidateOnFocus: true,
revalidateOnReconnect: true,
});
const { enable_system_proxy, proxy_auto_config } = verge ?? {};
const getSystemProxyActualState = () => {
const userEnabled = enable_system_proxy ?? false;
// 用户配置状态应该与系统实际状态一致
// 如果用户启用了系统代理,检查实际的系统状态
if (userEnabled) {
if (proxy_auto_config) {
return autoproxy?.enable ?? false;
} else {
return sysproxy?.enable ?? false;
}
}
// 用户没有启用时,返回 false
return false;
};
const getSystemProxyIndicator = () => {
if (proxy_auto_config) {
return autoproxy?.enable ?? false;
} else {
return sysproxy?.enable ?? false;
}
};
const updateProxyStatus = async () => {
await new Promise((resolve) => setTimeout(resolve, 100));
await mutate("getSystemProxy");
await mutate("getAutotemProxy");
};
const toggleSystemProxy = (enabled: boolean) => {
mutateVerge({ ...verge, enable_system_proxy: enabled }, false);
setTimeout(async () => {
try {
if (!enabled && verge?.auto_close_connection) {
closeAllConnections();
}
await patchVerge({ enable_system_proxy: enabled });
updateProxyStatus();
} catch (error) {
console.warn("[useSystemProxyState] toggleSystemProxy failed:", error);
mutateVerge({ ...verge, enable_system_proxy: !enabled }, false);
}
}, 0);
return Promise.resolve();
};
return {
actualState: getSystemProxyActualState(),
indicator: getSystemProxyIndicator(),
configState: enable_system_proxy ?? false,
sysproxy,
autoproxy,
proxy_auto_config,
toggleSystemProxy,
};
};