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

198 lines
5.4 KiB
TypeScript
Raw Normal View History

2022-03-01 08:58:47 +08:00
import useSWR, { useSWRConfig } from "swr";
import { useEffect, useRef, useState } from "react";
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,
List,
ListItem,
ListItemText,
} from "@mui/material";
import {
SendRounded,
ExpandLessRounded,
ExpandMoreRounded,
} 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";
import useSortProxy, { ProxySortType } from "./use-sort-proxy";
2022-02-27 00:58:14 +08:00
import useFilterProxy from "./use-filter-proxy";
import delayManager from "../../services/delay";
import ProxyHead from "./proxy-head";
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 [sortType, setSortType] = useState<ProxySortType>(0);
2022-02-27 00:58:14 +08:00
const [filterText, setFilterText] = useState("");
2022-02-27 00:58:14 +08:00
const virtuosoRef = useRef<any>();
2022-04-08 01:35:18 +08:00
const filterProxies = useFilterProxy(group.all, group.name, filterText);
const sortedProxies = useSortProxy(filterProxies, group.name, sortType);
2022-03-01 08:58:47 +08:00
const { data: profiles } = useSWR("getProfiles", getProfiles);
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 {
2022-03-01 08:58:47 +08:00
const profile = profiles?.items?.find((p) => p.uid === profiles.current);
2021-12-28 01:47:43 +08:00
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-03-07 01:41:42 +08:00
await patchProfile(profiles!.current!, { selected: profile.selected });
2022-02-17 01:42:25 +08:00
} catch (err) {
console.error(err);
}
2022-02-26 17:39:08 +08:00
});
const onLocation = (smooth = true) => {
const index = sortedProxies.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 () => {
const names = sortedProxies.map((p) => p.name);
2022-02-27 00:58:14 +08:00
const groupName = group.name;
2022-02-16 02:22:01 +08:00
await delayManager.checkListDelay({ names, groupName, skipNum: 8 }, () =>
mutate("getProxies")
2022-02-27 00:58:14 +08:00
);
2022-02-16 02:22:01 +08:00
console.log("finish");
2022-02-26 17:39:08 +08:00
});
2022-02-16 02:22:01 +08:00
// 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>
<ProxyHead
sx={{ pl: 4, pr: 3, my: 0.5, button: { mr: 0.5 } }}
showType={showType}
sortType={sortType}
groupName={group.name}
filterText={filterText}
onLocation={onLocation}
onCheckDelay={onCheckAll}
onShowType={setShowType}
onSortType={setSortType}
onFilterText={setFilterText}
/>
{!sortedProxies.length && (
2022-02-27 00:58:14 +08:00
<Box
sx={{
py: 3,
fontSize: 18,
textAlign: "center",
color: "text.secondary",
}}
>
Empty
</Box>
)}
{sortedProxies.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" }}
totalCount={sortedProxies.length}
itemContent={(index) => (
2022-01-13 02:51:30 +08:00
<ProxyItem
2022-02-16 02:22:01 +08:00
groupName={group.name}
proxy={sortedProxies[index]}
selected={sortedProxies[index].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
component="div"
disablePadding
2022-01-17 02:15:54 +08:00
sx={{ maxHeight: "320px", overflow: "auto", mb: "4px" }}
>
{sortedProxies.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;