2021-12-11 21:28:19 +08:00
|
|
|
import { useState } from "react";
|
|
|
|
import { Virtuoso } from "react-virtuoso";
|
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Collapse,
|
|
|
|
Divider,
|
|
|
|
IconButton,
|
|
|
|
List,
|
|
|
|
ListItem,
|
|
|
|
ListItemText,
|
|
|
|
} from "@mui/material";
|
|
|
|
import {
|
|
|
|
SendRounded,
|
|
|
|
ExpandLessRounded,
|
|
|
|
ExpandMoreRounded,
|
|
|
|
MyLocationRounded,
|
|
|
|
NetworkCheckRounded,
|
|
|
|
} from "@mui/icons-material";
|
2022-02-13 19:27:06 +08:00
|
|
|
import { updateProxy } from "../../services/api";
|
|
|
|
import { ApiType } from "../../services/types";
|
|
|
|
import { getProfiles, patchProfile } from "../../services/cmds";
|
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) => {
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const [now, setNow] = useState(group.now);
|
|
|
|
|
|
|
|
const proxies = group.all ?? [];
|
|
|
|
|
|
|
|
const onUpdate = async (name: string) => {
|
|
|
|
// can not call update
|
|
|
|
if (group.type !== "Selector") {
|
|
|
|
// Todo
|
|
|
|
// error Tips
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const oldValue = now;
|
|
|
|
try {
|
|
|
|
setNow(name);
|
2021-12-25 22:33:29 +08:00
|
|
|
await updateProxy(group.name, name);
|
2021-12-28 01:47:43 +08:00
|
|
|
|
|
|
|
const profiles = await getProfiles().catch(console.error);
|
|
|
|
if (!profiles) return;
|
|
|
|
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-01-05 02:00:59 +08:00
|
|
|
patchProfile(profiles.current!, profile).catch(console.error);
|
2021-12-11 21:28:19 +08:00
|
|
|
} catch {
|
|
|
|
setNow(oldValue);
|
|
|
|
// Todo
|
|
|
|
// error tips
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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>
|
|
|
|
<Box sx={{ pl: 4, pr: 3, my: 0.5 }}>
|
|
|
|
<IconButton size="small" title="location">
|
|
|
|
<MyLocationRounded />
|
|
|
|
</IconButton>
|
|
|
|
<IconButton size="small" title="check">
|
|
|
|
<NetworkCheckRounded />
|
|
|
|
</IconButton>
|
|
|
|
</Box>
|
|
|
|
|
|
|
|
{proxies.length >= 10 ? (
|
|
|
|
<Virtuoso
|
2022-01-17 02:15:54 +08:00
|
|
|
style={{ height: "320px", marginBottom: "4px" }}
|
2021-12-11 21:28:19 +08:00
|
|
|
totalCount={proxies.length}
|
|
|
|
itemContent={(index) => (
|
2022-01-13 02:51:30 +08:00
|
|
|
<ProxyItem
|
2021-12-11 21:28:19 +08:00
|
|
|
proxy={proxies[index]}
|
|
|
|
selected={proxies[index].name === now}
|
2022-01-13 02:51:30 +08:00
|
|
|
sx={{ py: 0, pl: 4 }}
|
2021-12-11 21:28:19 +08:00
|
|
|
onClick={onUpdate}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<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
|
|
|
>
|
|
|
|
{proxies.map((proxy) => (
|
2022-01-13 02:51:30 +08:00
|
|
|
<ProxyItem
|
2021-12-11 21:28:19 +08:00
|
|
|
key={proxy.name}
|
|
|
|
proxy={proxy}
|
|
|
|
selected={proxy.name === now}
|
2022-01-13 02:51:30 +08:00
|
|
|
sx={{ py: 0, pl: 4 }}
|
2021-12-11 21:28:19 +08:00
|
|
|
onClick={onUpdate}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</List>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Divider variant="middle" />
|
|
|
|
</Collapse>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ProxyGroup;
|