2022-02-26 01:21:39 +08:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2022-02-16 02:22:01 +08:00
|
|
|
import { useSWRConfig } from "swr";
|
2022-02-26 17:39:08 +08:00
|
|
|
import { useLockFn } from "ahooks";
|
2022-02-16 02:22:01 +08:00
|
|
|
import { Virtuoso } from "react-virtuoso";
|
2021-12-11 21:28:19 +08:00
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Collapse,
|
|
|
|
Divider,
|
|
|
|
IconButton,
|
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
ListItemText,
|
2022-02-27 00:58:14 +08:00
|
|
|
TextField,
|
2021-12-11 21:28:19 +08:00
|
|
|
} from "@mui/material";
|
|
|
|
import {
|
|
|
|
SendRounded,
|
|
|
|
ExpandLessRounded,
|
|
|
|
ExpandMoreRounded,
|
|
|
|
MyLocationRounded,
|
|
|
|
NetworkCheckRounded,
|
2022-02-27 00:58:14 +08:00
|
|
|
FilterAltRounded,
|
|
|
|
FilterAltOffRounded,
|
|
|
|
VisibilityRounded,
|
|
|
|
VisibilityOffRounded,
|
2021-12-11 21:28:19 +08:00
|
|
|
} from "@mui/icons-material";
|
2022-02-13 19:27:06 +08:00
|
|
|
import { ApiType } from "../../services/types";
|
2022-02-15 01:34:10 +08:00
|
|
|
import { updateProxy } from "../../services/api";
|
2022-02-13 19:27:06 +08:00
|
|
|
import { getProfiles, patchProfile } from "../../services/cmds";
|
2022-02-16 02:22:01 +08:00
|
|
|
import delayManager from "../../services/delay";
|
2022-02-27 00:58:14 +08:00
|
|
|
import useFilterProxy from "./use-filter-proxy";
|
2022-01-13 02:51:30 +08:00
|
|
|
import ProxyItem from "./proxy-item";
|
2021-12-11 21:28:19 +08:00
|
|
|
|
|
|
|
interface Props {
|
2021-12-25 22:33:29 +08:00
|
|
|
group: ApiType.ProxyGroupItem;
|
2021-12-11 21:28:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ProxyGroup = ({ group }: Props) => {
|
2022-02-16 02:22:01 +08:00
|
|
|
const { mutate } = useSWRConfig();
|
2021-12-11 21:28:19 +08:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const [now, setNow] = useState(group.now);
|
2022-02-27 00:58:14 +08:00
|
|
|
const [showType, setShowType] = useState(false);
|
|
|
|
const [showFilter, setShowFilter] = useState(false);
|
|
|
|
const [filterText, setFilterText] = useState("");
|
2021-12-11 21:28:19 +08:00
|
|
|
|
|
|
|
const proxies = group.all ?? [];
|
2022-02-27 00:58:14 +08:00
|
|
|
const virtuosoRef = useRef<any>();
|
|
|
|
const filterProxies = useFilterProxy(proxies, group.name, filterText);
|
2021-12-11 21:28:19 +08:00
|
|
|
|
2022-02-27 00:58:14 +08:00
|
|
|
const onChangeProxy = useLockFn(async (name: string) => {
|
2022-02-17 01:42:25 +08:00
|
|
|
// Todo: support another proxy group type
|
|
|
|
if (group.type !== "Selector") return;
|
|
|
|
|
2021-12-11 21:28:19 +08:00
|
|
|
const oldValue = now;
|
|
|
|
try {
|
|
|
|
setNow(name);
|
2021-12-25 22:33:29 +08:00
|
|
|
await updateProxy(group.name, name);
|
2022-02-17 01:42:25 +08:00
|
|
|
} catch {
|
|
|
|
setNow(oldValue);
|
|
|
|
return; // do not update profile
|
|
|
|
}
|
2021-12-28 01:47:43 +08:00
|
|
|
|
2022-02-17 01:42:25 +08:00
|
|
|
try {
|
|
|
|
const profiles = await getProfiles();
|
2021-12-28 01:47:43 +08:00
|
|
|
const profile = profiles.items![profiles.current!]!;
|
|
|
|
if (!profile) return;
|
|
|
|
if (!profile.selected) profile.selected = [];
|
|
|
|
|
|
|
|
const index = profile.selected.findIndex(
|
|
|
|
(item) => item.name === group.name
|
|
|
|
);
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
profile.selected.push({ name: group.name, now: name });
|
|
|
|
} else {
|
|
|
|
profile.selected[index] = { name: group.name, now: name };
|
|
|
|
}
|
2022-02-17 01:42:25 +08:00
|
|
|
await patchProfile(profiles.current!, profile);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2021-12-11 21:28:19 +08:00
|
|
|
}
|
2022-02-26 17:39:08 +08:00
|
|
|
});
|
2021-12-11 21:28:19 +08:00
|
|
|
|
2022-02-26 01:21:39 +08:00
|
|
|
const onLocation = (smooth = true) => {
|
2022-02-27 00:58:14 +08:00
|
|
|
const index = filterProxies.findIndex((p) => p.name === now);
|
2022-02-15 01:34:10 +08:00
|
|
|
|
|
|
|
if (index >= 0) {
|
2022-02-17 01:42:25 +08:00
|
|
|
virtuosoRef.current?.scrollToIndex?.({
|
2022-02-15 01:34:10 +08:00
|
|
|
index,
|
|
|
|
align: "center",
|
2022-02-26 01:21:39 +08:00
|
|
|
behavior: smooth ? "smooth" : "auto",
|
2022-02-15 01:34:10 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-26 17:39:08 +08:00
|
|
|
const onCheckAll = useLockFn(async () => {
|
2022-02-27 00:58:14 +08:00
|
|
|
const names = filterProxies.map((p) => p.name);
|
|
|
|
const groupName = group.name;
|
2022-02-16 02:22:01 +08:00
|
|
|
|
2022-02-27 00:58:14 +08:00
|
|
|
await delayManager.checkListDelay(
|
|
|
|
{ names, groupName, skipNum: 8, maxTimeout: 600 },
|
|
|
|
() => mutate("getProxies")
|
|
|
|
);
|
2022-02-16 02:22:01 +08:00
|
|
|
|
2022-02-27 00:58:14 +08:00
|
|
|
mutate("getProxies");
|
2022-02-26 17:39:08 +08:00
|
|
|
});
|
2022-02-16 02:22:01 +08:00
|
|
|
|
2022-02-27 00:58:14 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (!showFilter) setFilterText("");
|
|
|
|
}, [showFilter]);
|
|
|
|
|
2022-02-26 01:21:39 +08:00
|
|
|
// auto scroll to current index
|
|
|
|
useEffect(() => {
|
|
|
|
if (open) {
|
|
|
|
setTimeout(() => onLocation(false), 5);
|
|
|
|
}
|
|
|
|
}, [open]);
|
|
|
|
|
2021-12-11 21:28:19 +08:00
|
|
|
return (
|
|
|
|
<>
|
2021-12-12 15:34:17 +08:00
|
|
|
<ListItem button onClick={() => setOpen(!open)} dense>
|
2021-12-11 21:28:19 +08:00
|
|
|
<ListItemText
|
|
|
|
primary={group.name}
|
|
|
|
secondary={
|
|
|
|
<>
|
|
|
|
<SendRounded color="primary" sx={{ mr: 1, fontSize: 14 }} />
|
|
|
|
<span>{now}</span>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
secondaryTypographyProps={{
|
|
|
|
sx: { display: "flex", alignItems: "center" },
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{open ? <ExpandLessRounded /> : <ExpandMoreRounded />}
|
|
|
|
</ListItem>
|
|
|
|
|
|
|
|
<Collapse in={open} timeout="auto" unmountOnExit>
|
2022-02-27 00:58:14 +08:00
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
pl: 4,
|
|
|
|
pr: 3,
|
|
|
|
my: 0.5,
|
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
|
|
|
button: { mr: 0.5 },
|
|
|
|
}}
|
|
|
|
>
|
2022-02-26 01:21:39 +08:00
|
|
|
<IconButton
|
|
|
|
size="small"
|
|
|
|
title="location"
|
|
|
|
onClick={() => onLocation(true)}
|
|
|
|
>
|
2021-12-11 21:28:19 +08:00
|
|
|
<MyLocationRounded />
|
|
|
|
</IconButton>
|
2022-02-27 00:58:14 +08:00
|
|
|
|
2022-02-16 02:22:01 +08:00
|
|
|
<IconButton size="small" title="check" onClick={onCheckAll}>
|
2021-12-11 21:28:19 +08:00
|
|
|
<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 } }}
|
|
|
|
/>
|
|
|
|
)}
|
2021-12-11 21:28:19 +08:00
|
|
|
</Box>
|
|
|
|
|
2022-02-27 00:58:14 +08:00
|
|
|
{!filterProxies.length && (
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
py: 3,
|
|
|
|
fontSize: 18,
|
|
|
|
textAlign: "center",
|
|
|
|
color: "text.secondary",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Empty
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{filterProxies.length >= 10 ? (
|
2021-12-11 21:28:19 +08:00
|
|
|
<Virtuoso
|
2022-02-17 01:42:25 +08:00
|
|
|
ref={virtuosoRef}
|
2022-01-17 02:15:54 +08:00
|
|
|
style={{ height: "320px", marginBottom: "4px" }}
|
2022-02-27 00:58:14 +08:00
|
|
|
totalCount={filterProxies.length}
|
2021-12-11 21:28:19 +08:00
|
|
|
itemContent={(index) => (
|
2022-01-13 02:51:30 +08:00
|
|
|
<ProxyItem
|
2022-02-16 02:22:01 +08:00
|
|
|
groupName={group.name}
|
2022-02-27 00:58:14 +08:00
|
|
|
proxy={filterProxies[index]}
|
|
|
|
selected={filterProxies[index].name === now}
|
|
|
|
showType={showType}
|
2022-01-13 02:51:30 +08:00
|
|
|
sx={{ py: 0, pl: 4 }}
|
2022-02-27 00:58:14 +08:00
|
|
|
onClick={onChangeProxy}
|
2021-12-11 21:28:19 +08:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<List
|
|
|
|
component="div"
|
|
|
|
disablePadding
|
2022-01-17 02:15:54 +08:00
|
|
|
sx={{ maxHeight: "320px", overflow: "auto", mb: "4px" }}
|
2021-12-11 21:28:19 +08:00
|
|
|
>
|
2022-02-27 00:58:14 +08:00
|
|
|
{filterProxies.map((proxy) => (
|
2022-01-13 02:51:30 +08:00
|
|
|
<ProxyItem
|
2021-12-11 21:28:19 +08:00
|
|
|
key={proxy.name}
|
2022-02-16 02:22:01 +08:00
|
|
|
groupName={group.name}
|
2021-12-11 21:28:19 +08:00
|
|
|
proxy={proxy}
|
|
|
|
selected={proxy.name === now}
|
2022-02-27 00:58:14 +08:00
|
|
|
showType={showType}
|
2022-01-13 02:51:30 +08:00
|
|
|
sx={{ py: 0, pl: 4 }}
|
2022-02-27 00:58:14 +08:00
|
|
|
onClick={onChangeProxy}
|
2021-12-11 21:28:19 +08:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Divider variant="middle" />
|
|
|
|
</Collapse>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ProxyGroup;
|