import { useState } from "react"; import { Virtuoso } from "react-virtuoso"; import { alpha, Box, Collapse, Divider, IconButton, List, ListItem, ListItemButton, ListItemIcon, ListItemText, } from "@mui/material"; import { SendRounded, ExpandLessRounded, ExpandMoreRounded, MyLocationRounded, NetworkCheckRounded, CheckCircleOutlineRounded, } from "@mui/icons-material"; import { updateProxy } from "../services/api"; import { ApiType } from "../services/types"; import { getProfiles, setProfiles } from "../services/cmds"; import noop from "../utils/noop"; interface ItemProps { proxy: ApiType.ProxyItem; selected: boolean; onClick?: (name: string) => void; } const Item = ({ proxy, selected, onClick }: ItemProps) => { return ( onClick?.(proxy.name)} sx={[ { borderRadius: 1, }, ({ palette: { mode, primary } }) => { const bgcolor = mode === "light" ? alpha(primary.main, 0.15) : alpha(primary.main, 0.35); const color = mode === "light" ? primary.main : primary.light; return { "&.Mui-selected": { bgcolor }, "&.Mui-selected .MuiListItemText-secondary": { color }, }; }, ]} > {selected && } ); }; interface Props { group: ApiType.ProxyGroupItem; } 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); await updateProxy(group.name, name); 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 }; } setProfiles(profiles.current!, profile).catch(console.error); } catch { setNow(oldValue); // Todo // error tips } }; return ( <> setOpen(!open)} dense> {now} } secondaryTypographyProps={{ sx: { display: "flex", alignItems: "center" }, }} /> {open ? : } {proxies.length >= 10 ? ( ( )} /> ) : ( {proxies.map((proxy) => ( ))} )} ); }; export default ProxyGroup;