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

145 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-03-22 12:38:59 +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";
import { Virtuoso } from "react-virtuoso";
import { providerHealthCheck, updateProxy } from "@/services/api";
2022-08-06 02:35:11 +08:00
import { getProfiles, patchProfile } from "@/services/cmds";
import delayManager from "@/services/delay";
2022-06-04 18:55:39 +08:00
import useHeadState from "./use-head-state";
import useFilterSort from "./use-filter-sort";
2022-04-08 01:35:18 +08:00
import ProxyHead from "./proxy-head";
import ProxyItem from "./proxy-item";
2022-02-26 17:39:08 +08:00
interface Props {
groupName: string;
curProxy?: string;
proxies: ApiType.ProxyItem[];
}
2022-03-22 12:38:59 +08:00
// this component will be used for DIRECT/GLOBAL
2022-02-26 17:39:08 +08:00
const ProxyGlobal = (props: Props) => {
const { groupName, curProxy, proxies } = props;
const { mutate } = useSWRConfig();
const [now, setNow] = useState(curProxy || "DIRECT");
2022-04-08 01:35:18 +08:00
2022-06-04 18:55:39 +08:00
const [headState, setHeadState] = useHeadState(groupName);
2022-02-27 00:58:14 +08:00
const virtuosoRef = useRef<any>();
const sortedProxies = useFilterSort(
2022-06-04 18:55:39 +08:00
proxies,
groupName,
headState.filterText,
2022-06-04 18:55:39 +08:00
headState.sortType
);
2022-04-08 01:35:18 +08:00
2022-03-22 12:38:59 +08:00
const { data: profiles } = useSWR("getProfiles", getProfiles);
2022-02-26 17:39:08 +08:00
const onChangeProxy = useLockFn(async (name: string) => {
2022-03-22 12:38:59 +08:00
await updateProxy(groupName, name);
2022-02-26 17:39:08 +08:00
setNow(name);
2022-03-22 12:38:59 +08:00
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 });
2022-02-26 17:39:08 +08:00
});
const onLocation = (smooth = true) => {
2022-04-08 01:35:18 +08:00
const index = sortedProxies.findIndex((p) => p.name === now);
2022-02-26 17:39:08 +08:00
if (index >= 0) {
virtuosoRef.current?.scrollToIndex?.({
index,
align: "center",
behavior: smooth ? "smooth" : "auto",
});
}
};
const onCheckAll = useLockFn(async () => {
const providers = new Set(
sortedProxies.map((p) => p.provider!).filter(Boolean)
);
if (providers.size) {
Promise.allSettled(
[...providers].map((p) => providerHealthCheck(p))
).then(() => mutate("getProxies"));
}
2022-02-26 17:39:08 +08:00
await delayManager.checkListDelay(
sortedProxies.filter((p) => !p.provider).map((p) => p.name),
groupName,
16
2022-02-27 00:58:14 +08:00
);
mutate("getProxies");
2022-02-26 17:39:08 +08:00
});
useEffect(() => onLocation(false), [groupName]);
useEffect(() => {
if (groupName === "DIRECT") setNow("DIRECT");
2022-03-23 12:49:34 +08:00
else if (groupName === "GLOBAL") {
if (profiles) {
const current = profiles.current;
const profile = profiles.items?.find((p) => p.uid === current);
profile?.selected?.forEach((item) => {
if (item.name === "GLOBAL") {
if (item.now && item.now !== curProxy) {
updateProxy("GLOBAL", item.now).then(() => setNow(item!.now!));
mutate("getProxies");
}
}
});
}
setNow(curProxy || "DIRECT");
}
}, [groupName, curProxy, profiles]);
2022-02-26 17:39:08 +08:00
return (
<>
2022-04-08 01:35:18 +08:00
<ProxyHead
sx={{ px: 3, my: 0.5, button: { mr: 0.5 } }}
groupName={groupName}
2022-06-04 18:55:39 +08:00
headState={headState}
2022-04-08 01:35:18 +08:00
onLocation={onLocation}
onCheckDelay={onCheckAll}
2022-06-04 18:55:39 +08:00
onHeadState={setHeadState}
2022-04-08 01:35:18 +08:00
/>
2022-02-26 17:39:08 +08:00
<Virtuoso
ref={virtuosoRef}
style={{ height: "calc(100% - 40px)" }}
2022-04-08 01:35:18 +08:00
totalCount={sortedProxies.length}
2022-02-26 17:39:08 +08:00
itemContent={(index) => (
<ProxyItem
groupName={groupName}
2022-04-08 01:35:18 +08:00
proxy={sortedProxies[index]}
selected={sortedProxies[index].name === now}
2022-06-04 18:55:39 +08:00
showType={headState.showType}
2022-02-26 17:39:08 +08:00
onClick={onChangeProxy}
sx={{ py: 0, px: 2 }}
/>
)}
/>
</>
);
};
export default ProxyGlobal;