clash-verge/src/components/proxy/proxy-global.tsx

194 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-03-22 12:38:59 +08:00
import useSWR, { useSWRConfig } from "swr";
2022-02-26 17:39:08 +08:00
import { useEffect, useRef, useState } from "react";
import { useLockFn } from "ahooks";
import { Virtuoso } from "react-virtuoso";
2022-02-27 00:58:14 +08:00
import { Box, IconButton, TextField } from "@mui/material";
import {
MyLocationRounded,
NetworkCheckRounded,
FilterAltRounded,
FilterAltOffRounded,
VisibilityRounded,
VisibilityOffRounded,
} from "@mui/icons-material";
2022-02-26 17:39:08 +08:00
import { ApiType } from "../../services/types";
import { updateProxy } from "../../services/api";
2022-03-22 12:38:59 +08:00
import { getProfiles, patchProfile } from "../../services/cmds";
2022-02-26 17:39:08 +08:00
import delayManager from "../../services/delay";
2022-02-27 00:58:14 +08:00
import useFilterProxy from "./use-filter-proxy";
2022-02-26 17:39:08 +08:00
import ProxyItem from "./proxy-item";
interface Props {
groupName: string;
curProxy?: string;
proxies: ApiType.ProxyItem[];
}
2022-03-22 12:38:59 +08:00
// this component will be used for DIRECT/GLOBAL
2022-02-26 17:39:08 +08:00
const ProxyGlobal = (props: Props) => {
const { groupName, curProxy, proxies } = props;
const { mutate } = useSWRConfig();
const [now, setNow] = useState(curProxy || "DIRECT");
2022-02-27 01:33:22 +08:00
const [showType, setShowType] = useState(true);
2022-02-27 00:58:14 +08:00
const [showFilter, setShowFilter] = useState(false);
const [filterText, setFilterText] = useState("");
const virtuosoRef = useRef<any>();
const filterProxies = useFilterProxy(proxies, groupName, filterText);
2022-02-26 17:39:08 +08:00
2022-03-22 12:38:59 +08:00
const { data: profiles } = useSWR("getProfiles", getProfiles);
2022-02-26 17:39:08 +08:00
const onChangeProxy = useLockFn(async (name: string) => {
2022-03-22 12:38:59 +08:00
await updateProxy(groupName, name);
2022-02-26 17:39:08 +08:00
setNow(name);
2022-03-22 12:38:59 +08:00
if (groupName === "DIRECT") return;
// update global selected
const profile = profiles?.items?.find((p) => p.uid === profiles.current);
if (!profile) return;
if (!profile.selected) profile.selected = [];
const index = profile.selected.findIndex((item) => item.name === groupName);
if (index < 0) {
profile.selected.unshift({ name: groupName, now: name });
} else {
profile.selected[index] = { name: groupName, now: name };
}
await patchProfile(profiles!.current!, { selected: profile.selected });
2022-02-26 17:39:08 +08:00
});
const onLocation = (smooth = true) => {
2022-02-27 00:58:14 +08:00
const index = filterProxies.findIndex((p) => p.name === now);
2022-02-26 17:39:08 +08:00
if (index >= 0) {
virtuosoRef.current?.scrollToIndex?.({
index,
align: "center",
behavior: smooth ? "smooth" : "auto",
});
}
};
const onCheckAll = useLockFn(async () => {
2022-02-27 00:58:14 +08:00
const names = filterProxies.map((p) => p.name);
2022-02-26 17:39:08 +08:00
2022-02-27 00:58:14 +08:00
await delayManager.checkListDelay(
{ names, groupName, skipNum: 8, maxTimeout: 600 },
() => mutate("getProxies")
);
2022-02-26 17:39:08 +08:00
2022-02-27 00:58:14 +08:00
mutate("getProxies");
2022-02-26 17:39:08 +08:00
});
useEffect(() => onLocation(false), [groupName]);
2022-02-27 00:58:14 +08:00
useEffect(() => {
if (!showFilter) setFilterText("");
}, [showFilter]);
2022-02-26 17:39:08 +08:00
useEffect(() => {
if (groupName === "DIRECT") setNow("DIRECT");
2022-03-23 12:49:34 +08:00
else if (groupName === "GLOBAL") {
if (profiles) {
const current = profiles.current;
const profile = profiles.items?.find((p) => p.uid === current);
profile?.selected?.forEach((item) => {
if (item.name === "GLOBAL") {
if (item.now && item.now !== curProxy) {
updateProxy("GLOBAL", item.now).then(() => setNow(item!.now!));
mutate("getProxies");
}
}
});
}
setNow(curProxy || "DIRECT");
}
}, [groupName, curProxy, profiles]);
2022-02-26 17:39:08 +08:00
return (
<>
2022-02-27 00:58:14 +08:00
<Box
sx={{
px: 3,
my: 0.5,
display: "flex",
alignItems: "center",
button: { mr: 0.5 },
}}
>
2022-02-26 17:39:08 +08:00
<IconButton
size="small"
title="location"
2022-04-06 22:52:00 +08:00
color="inherit"
2022-02-26 17:39:08 +08:00
onClick={() => onLocation(true)}
>
<MyLocationRounded />
</IconButton>
2022-02-27 00:58:14 +08:00
2022-04-06 22:52:00 +08:00
<IconButton
size="small"
title="delay check"
color="inherit"
onClick={onCheckAll}
>
2022-02-26 17:39:08 +08:00
<NetworkCheckRounded />
</IconButton>
2022-02-27 00:58:14 +08:00
<IconButton
size="small"
2022-03-30 01:29:25 +08:00
title="proxy detail"
2022-04-06 22:52:00 +08:00
color="inherit"
2022-02-27 00:58:14 +08:00
onClick={() => setShowType(!showType)}
>
{showType ? <VisibilityRounded /> : <VisibilityOffRounded />}
</IconButton>
<IconButton
size="small"
2022-03-30 01:29:25 +08:00
title="filter"
2022-04-06 22:52:00 +08:00
color="inherit"
2022-02-27 00:58:14 +08:00
onClick={() => setShowFilter(!showFilter)}
>
{showFilter ? <FilterAltRounded /> : <FilterAltOffRounded />}
</IconButton>
{showFilter && (
<TextField
autoFocus
hiddenLabel
value={filterText}
size="small"
variant="outlined"
placeholder="Filter conditions"
onChange={(e) => setFilterText(e.target.value)}
sx={{ ml: 0.5, flex: "1 1 auto", input: { py: 0.65, px: 1 } }}
/>
)}
2022-02-26 17:39:08 +08:00
</Box>
<Virtuoso
ref={virtuosoRef}
style={{ height: "calc(100% - 40px)" }}
2022-02-27 00:58:14 +08:00
totalCount={filterProxies.length}
2022-02-26 17:39:08 +08:00
itemContent={(index) => (
<ProxyItem
groupName={groupName}
2022-02-27 00:58:14 +08:00
proxy={filterProxies[index]}
selected={filterProxies[index].name === now}
2022-02-27 01:33:22 +08:00
showType={showType}
2022-02-26 17:39:08 +08:00
onClick={onChangeProxy}
sx={{ py: 0, px: 2 }}
/>
)}
/>
</>
);
};
export default ProxyGlobal;