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

246 lines
6.4 KiB
TypeScript
Raw Normal View History

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";
import {
Box,
Collapse,
Divider,
IconButton,
List,
ListItem,
ListItemText,
2022-02-27 00:58:14 +08:00
TextField,
} from "@mui/material";
import {
SendRounded,
ExpandLessRounded,
ExpandMoreRounded,
MyLocationRounded,
NetworkCheckRounded,
2022-02-27 00:58:14 +08:00
FilterAltRounded,
FilterAltOffRounded,
VisibilityRounded,
VisibilityOffRounded,
} 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";
interface Props {
2021-12-25 22:33:29 +08:00
group: ApiType.ProxyGroupItem;
}
const ProxyGroup = ({ group }: Props) => {
2022-02-16 02:22:01 +08:00
const { mutate } = useSWRConfig();
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("");
const proxies = group.all ?? [];
2022-02-27 00:58:14 +08:00
const virtuosoRef = useRef<any>();
const filterProxies = useFilterProxy(proxies, group.name, filterText);
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;
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);
}
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-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",
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]);
// auto scroll to current index
useEffect(() => {
if (open) {
setTimeout(() => onLocation(false), 5);
}
}, [open]);
return (
<>
2021-12-12 15:34:17 +08:00
<ListItem button onClick={() => setOpen(!open)} dense>
<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 },
}}
>
<IconButton
size="small"
title="location"
onClick={() => onLocation(true)}
>
<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}>
<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 } }}
/>
)}
</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 ? (
<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}
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}
/>
)}
/>
) : (
<List
component="div"
disablePadding
2022-01-17 02:15:54 +08:00
sx={{ maxHeight: "320px", overflow: "auto", mb: "4px" }}
>
2022-02-27 00:58:14 +08:00
{filterProxies.map((proxy) => (
2022-01-13 02:51:30 +08:00
<ProxyItem
key={proxy.name}
2022-02-16 02:22:01 +08:00
groupName={group.name}
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}
/>
))}
</List>
)}
<Divider variant="middle" />
</Collapse>
</>
);
};
export default ProxyGroup;