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

151 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-02-26 17:39:08 +08:00
import { useEffect, useRef, useState } from "react";
import { useSWRConfig } from "swr";
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";
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[];
}
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
const onChangeProxy = useLockFn(async (name: string) => {
await updateProxy("GLOBAL", name);
mutate("getProxies");
setNow(name);
});
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");
if (groupName === "GLOBAL") setNow(curProxy || "DIRECT");
}, [groupName, curProxy]);
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"
onClick={() => onLocation(true)}
>
<MyLocationRounded />
</IconButton>
2022-02-27 00:58:14 +08:00
2022-02-26 17:39:08 +08:00
<IconButton size="small" title="check" onClick={onCheckAll}>
<NetworkCheckRounded />
</IconButton>
2022-02-27 00:58:14 +08:00
<IconButton
size="small"
title="check"
onClick={() => setShowType(!showType)}
>
{showType ? <VisibilityRounded /> : <VisibilityOffRounded />}
</IconButton>
<IconButton
size="small"
title="check"
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;