import useSWR, { useSWRConfig } from "swr"; import { useEffect, useRef, useState } from "react"; import { useLockFn } from "ahooks"; import { Virtuoso } from "react-virtuoso"; import { Box, IconButton, TextField } from "@mui/material"; import { MyLocationRounded, NetworkCheckRounded, FilterAltRounded, FilterAltOffRounded, VisibilityRounded, VisibilityOffRounded, } from "@mui/icons-material"; import { ApiType } from "../../services/types"; import { updateProxy } from "../../services/api"; import { getProfiles, patchProfile } from "../../services/cmds"; import delayManager from "../../services/delay"; import useFilterProxy from "./use-filter-proxy"; import ProxyItem from "./proxy-item"; interface Props { groupName: string; curProxy?: string; proxies: ApiType.ProxyItem[]; } // this component will be used for DIRECT/GLOBAL const ProxyGlobal = (props: Props) => { const { groupName, curProxy, proxies } = props; const { mutate } = useSWRConfig(); const [now, setNow] = useState(curProxy || "DIRECT"); const [showType, setShowType] = useState(true); const [showFilter, setShowFilter] = useState(false); const [filterText, setFilterText] = useState(""); const virtuosoRef = useRef(); const filterProxies = useFilterProxy(proxies, groupName, filterText); const { data: profiles } = useSWR("getProfiles", getProfiles); const onChangeProxy = useLockFn(async (name: string) => { await updateProxy(groupName, name); setNow(name); if (groupName === "DIRECT") return; // update global selected const profile = profiles?.items?.find((p) => p.uid === profiles.current); if (!profile) return; if (!profile.selected) profile.selected = []; const index = profile.selected.findIndex((item) => item.name === groupName); if (index < 0) { profile.selected.unshift({ name: groupName, now: name }); } else { profile.selected[index] = { name: groupName, now: name }; } await patchProfile(profiles!.current!, { selected: profile.selected }); }); const onLocation = (smooth = true) => { const index = filterProxies.findIndex((p) => p.name === now); if (index >= 0) { virtuosoRef.current?.scrollToIndex?.({ index, align: "center", behavior: smooth ? "smooth" : "auto", }); } }; const onCheckAll = useLockFn(async () => { const names = filterProxies.map((p) => p.name); await delayManager.checkListDelay( { names, groupName, skipNum: 8, maxTimeout: 600 }, () => mutate("getProxies") ); mutate("getProxies"); }); useEffect(() => onLocation(false), [groupName]); useEffect(() => { if (!showFilter) setFilterText(""); }, [showFilter]); useEffect(() => { if (groupName === "DIRECT") setNow("DIRECT"); if (groupName === "GLOBAL") setNow(curProxy || "DIRECT"); }, [groupName, curProxy]); return ( <> onLocation(true)} > setShowType(!showType)} > {showType ? : } setShowFilter(!showFilter)} > {showFilter ? : } {showFilter && ( setFilterText(e.target.value)} sx={{ ml: 0.5, flex: "1 1 auto", input: { py: 0.65, px: 1 } }} /> )} ( )} /> ); }; export default ProxyGlobal;