mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import useSWR, { useSWRConfig } from "swr";
|
|
import { useEffect } from "react";
|
|
import { useLockFn } from "ahooks";
|
|
import { Button, ButtonGroup, List, Paper } from "@mui/material";
|
|
import { getClashConfig, updateConfigs } from "../services/api";
|
|
import { patchClashConfig } from "../services/cmds";
|
|
import { getProxies } from "../services/api";
|
|
import BasePage from "../components/base/base-page";
|
|
import ProxyGroup from "../components/proxy/proxy-group";
|
|
import ProxyGlobal from "../components/proxy/proxy-global";
|
|
|
|
const ProxyPage = () => {
|
|
const { mutate } = useSWRConfig();
|
|
const { data: proxiesData } = useSWR("getProxies", getProxies);
|
|
const { data: clashConfig } = useSWR("getClashConfig", getClashConfig);
|
|
|
|
const modeList = ["rule", "global", "direct"];
|
|
const curMode = clashConfig?.mode.toLowerCase() ?? "direct";
|
|
const { groups = [], proxies = [] } = proxiesData ?? {};
|
|
|
|
// make sure that fetch the proxies successfully
|
|
useEffect(() => {
|
|
if (
|
|
(curMode === "rule" && !groups.length) ||
|
|
(curMode === "global" && proxies.length < 2)
|
|
) {
|
|
setTimeout(() => mutate("getProxies"), 500);
|
|
}
|
|
}, [groups, proxies, curMode]);
|
|
|
|
const onChangeMode = useLockFn(async (mode: string) => {
|
|
// switch rapidly
|
|
await updateConfigs({ mode });
|
|
await patchClashConfig({ mode });
|
|
mutate("getClashConfig");
|
|
});
|
|
|
|
// difference style
|
|
const showGroup = curMode === "rule" && !!groups.length;
|
|
const pageStyle = showGroup ? {} : { height: "100%" };
|
|
const paperStyle: any = showGroup
|
|
? { mb: 0.5 }
|
|
: { py: 1, height: "100%", boxSizing: "border-box" };
|
|
|
|
return (
|
|
<BasePage
|
|
contentStyle={pageStyle}
|
|
title={showGroup ? "Proxy Groups" : "Proxies"}
|
|
header={
|
|
<ButtonGroup size="small">
|
|
{modeList.map((mode) => (
|
|
<Button
|
|
key={mode}
|
|
variant={mode === curMode ? "contained" : "outlined"}
|
|
onClick={() => onChangeMode(mode)}
|
|
sx={{ textTransform: "capitalize" }}
|
|
>
|
|
{mode}
|
|
</Button>
|
|
))}
|
|
</ButtonGroup>
|
|
}
|
|
>
|
|
<Paper sx={{ borderRadius: 1, boxShadow: 2, ...paperStyle }}>
|
|
{curMode === "rule" && !!groups.length && (
|
|
<List>
|
|
{groups.map((group) => (
|
|
<ProxyGroup key={group.name} group={group} />
|
|
))}
|
|
</List>
|
|
)}
|
|
{((curMode === "rule" && !groups.length) || curMode === "global") && (
|
|
<ProxyGlobal
|
|
groupName="GLOBAL"
|
|
curProxy={proxiesData?.global?.now}
|
|
proxies={proxies}
|
|
/>
|
|
)}
|
|
{curMode === "direct" && (
|
|
<ProxyGlobal
|
|
groupName="DIRECT"
|
|
curProxy="DIRECT"
|
|
proxies={[proxiesData?.direct!].filter(Boolean)}
|
|
/>
|
|
)}
|
|
</Paper>
|
|
</BasePage>
|
|
);
|
|
};
|
|
|
|
export default ProxyPage;
|