From f6a4d551bafde805507084bf95045dfd251411f3 Mon Sep 17 00:00:00 2001 From: YetAnotherZephyruso <176294927+YetAnotherZephyruso@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:28:55 +0800 Subject: [PATCH] fix: enum of not connect for mihomo (#868) * fix: enum of not connect for mihomo & replace all latencyMap() with a get * feat: unify connections style --- src/components/Header.tsx | 2 +- src/components/IPv6Support.tsx | 4 ++-- src/components/Latency.tsx | 4 ++-- src/components/ProxyPreviewBar.tsx | 4 ++-- src/components/ProxyPreviewDots.tsx | 4 ++-- src/constants/index.ts | 4 ++-- src/helpers/proxies.ts | 29 ++++++++++++----------------- src/pages/Connections.tsx | 14 +++++++------- src/pages/Logs.tsx | 6 +++--- src/pages/Proxies.tsx | 12 ++---------- src/pages/Rules.tsx | 4 ++-- src/signals/logs.ts | 2 +- src/signals/proxies.ts | 23 ++++++++++++----------- 13 files changed, 50 insertions(+), 62 deletions(-) diff --git a/src/components/Header.tsx b/src/components/Header.tsx index cf33b0c..5dde325 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -110,7 +110,7 @@ export const Header = () => { checked={openedDrawer()} /> -
+
diff --git a/src/components/IPv6Support.tsx b/src/components/IPv6Support.tsx index 932cf6a..da315a7 100644 --- a/src/components/IPv6Support.tsx +++ b/src/components/IPv6Support.tsx @@ -1,7 +1,7 @@ import { useProxies } from '~/signals' import { proxyIPv6SupportMap } from '~/signals/ipv6' -export const IPv6Support = (props: { name?: string; class?: string }) => { +export const IPv6Support = (props: { name?: string }) => { const { getNowProxyNodeName } = useProxies() const support = createMemo(() => { @@ -10,7 +10,7 @@ export const IPv6Support = (props: { name?: string; class?: string }) => { return ( - IPv6 + IPv6 ) } diff --git a/src/components/Latency.tsx b/src/components/Latency.tsx index dd042d9..73b6902 100644 --- a/src/components/Latency.tsx +++ b/src/components/Latency.tsx @@ -4,10 +4,10 @@ import { latencyQualityMap, useProxies } from '~/signals' export const Latency = (props: { name?: string; class?: string }) => { const [t] = useI18n() - const { getNowProxyNodeName, latencyMap } = useProxies() + const { getLatencyByName } = useProxies() const [textClassName, setTextClassName] = createSignal('') const latency = createMemo(() => { - return latencyMap()[getNowProxyNodeName(props.name || '')] + return getLatencyByName(props.name || '') }) createEffect(() => { diff --git a/src/components/ProxyPreviewBar.tsx b/src/components/ProxyPreviewBar.tsx index 756ad69..6f68d09 100644 --- a/src/components/ProxyPreviewBar.tsx +++ b/src/components/ProxyPreviewBar.tsx @@ -5,9 +5,9 @@ export const ProxyPreviewBar = (props: { proxyNameList: string[] now?: string }) => { - const { latencyMap } = useProxies() + const { getLatencyByName } = useProxies() const latencyList = createMemo(() => - props.proxyNameList.map((name) => latencyMap()[name]), + props.proxyNameList.map((name) => getLatencyByName(name)), ) const all = createMemo(() => latencyList().length) diff --git a/src/components/ProxyPreviewDots.tsx b/src/components/ProxyPreviewDots.tsx index 1bf3015..18b2bde 100644 --- a/src/components/ProxyPreviewDots.tsx +++ b/src/components/ProxyPreviewDots.tsx @@ -40,7 +40,7 @@ export const ProxyPreviewDots = (props: { proxyNameList: string[] now?: string }) => { - const { latencyMap } = useProxies() + const { getLatencyByName } = useProxies() return (
@@ -48,7 +48,7 @@ export const ProxyPreviewDots = (props: { [ name, - latencyMap()[name], + getLatencyByName(name), ])} > {([name, latency]) => { diff --git a/src/constants/index.ts b/src/constants/index.ts index e22172a..a07de63 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -80,13 +80,13 @@ export const DEFAULT_CHART_OPTIONS: ApexOptions = { } export enum LATENCY_QUALITY_MAP_HTTP { - NOT_CONNECTED = -1, + NOT_CONNECTED = 0, MEDIUM = 200, HIGH = 500, } export enum LATENCY_QUALITY_MAP_HTTPS { - NOT_CONNECTED = -1, + NOT_CONNECTED = 0, MEDIUM = 800, HIGH = 1500, } diff --git a/src/helpers/proxies.ts b/src/helpers/proxies.ts index 4077367..5ed37cd 100644 --- a/src/helpers/proxies.ts +++ b/src/helpers/proxies.ts @@ -1,5 +1,5 @@ import { PROXIES_ORDERING_TYPE } from '~/constants' -import { latencyQualityMap } from '~/signals' +import { latencyQualityMap, useProxies } from '~/signals' export const formatProxyType = (type = '') => { const t = type.toLowerCase() @@ -36,21 +36,17 @@ export const filterSpecialProxyType = (type = '') => { export const sortProxiesByOrderingType = ( proxyNames: string[], - proxyLatencyMap: Record, orderingType: PROXIES_ORDERING_TYPE, - proxyGroupNames: Set | undefined, ) => { + const { getLatencyByName } = useProxies() + if (orderingType === PROXIES_ORDERING_TYPE.NATURAL) { return proxyNames } return proxyNames.sort((a, b) => { - if (proxyGroupNames?.has(a) && !proxyGroupNames?.has(b)) return -1 - - if (proxyGroupNames?.has(b) && !proxyGroupNames?.has(a)) return 1 - - const prevLatency = proxyLatencyMap[a] - const nextLatency = proxyLatencyMap[b] + const prevLatency = getLatencyByName(a) + const nextLatency = getLatencyByName(b) switch (orderingType) { case PROXIES_ORDERING_TYPE.LATENCY_ASC: @@ -81,14 +77,13 @@ export const sortProxiesByOrderingType = ( export const filterProxiesByAvailability = ( proxyNames: string[], - proxyLatencyMap: Record, - excludes: Set, enabled?: boolean, -) => - enabled - ? proxyNames.filter((name) => - excludes?.has(name) - ? true - : proxyLatencyMap[name] !== latencyQualityMap().NOT_CONNECTED, +) => { + const { getLatencyByName } = useProxies() + + return enabled + ? proxyNames.filter( + (name) => getLatencyByName(name) !== latencyQualityMap().NOT_CONNECTED, ) : proxyNames +} diff --git a/src/pages/Connections.tsx b/src/pages/Connections.tsx index 653b332..0124bc1 100644 --- a/src/pages/Connections.tsx +++ b/src/pages/Connections.tsx @@ -358,13 +358,13 @@ export default () => {
-
+
{(tab) => (
setGlobalFilter(e.target.value)} />
setGlobalFilter(e.currentTarget.value)} diff --git a/src/signals/logs.ts b/src/signals/logs.ts index da5b07a..0b7776e 100644 --- a/src/signals/logs.ts +++ b/src/signals/logs.ts @@ -19,7 +19,7 @@ createEffect(() => { seq++ }) -export function useLogs() { +export const useLogs = () => { return { logs, paused, diff --git a/src/signals/proxies.ts b/src/signals/proxies.ts index 5eac5ab..295f922 100644 --- a/src/signals/proxies.ts +++ b/src/signals/proxies.ts @@ -56,13 +56,11 @@ const [isAllProviderUpdating, setIsAllProviderUpdating] = createSignal(false) // these signals should be global state const [proxies, setProxies] = createSignal([]) -const [proxyGroupNames, setProxyGroupNames] = createSignal>( - new Set(), -) const [proxyProviders, setProxyProviders] = createSignal< (ProxyProvider & { proxies: ProxyNodeWithProvider[] })[] >([]) +// DO NOT use latency from latency map directly use getLatencyByName instead const [latencyMap, setLatencyMap] = createSignal>({}) const [proxyNodeMap, setProxyNodeMap] = createSignal>( {}, @@ -152,9 +150,6 @@ export const useProxies = () => { batch(() => { setProxies(sortedProxies) - setProxyGroupNames( - new Set(['DIRECT', 'REJECT', ...sortedProxies.map((p) => p.name)]), - ) setProxyProviders(sortedProviders) setProxiesInfo(allProxies) }) @@ -185,10 +180,12 @@ export const useProxies = () => { } const proxyLatencyTest = async (proxyName: string, provider: string) => { - setProxyLatencyTestingMap(proxyName, async () => { - await proxyIPv6SupportTest(proxyName, provider) + const nodeName = getNowProxyNodeName(proxyName) + + setProxyLatencyTestingMap(nodeName, async () => { + await proxyIPv6SupportTest(nodeName, provider) const { delay } = await proxyLatencyTestAPI( - proxyName, + nodeName, provider, urlForLatencyTest(), latencyTestTimeoutDuration(), @@ -196,7 +193,7 @@ export const useProxies = () => { setLatencyMap((latencyMap) => ({ ...latencyMap, - [proxyName]: delay, + [nodeName]: delay, })) }) } @@ -257,6 +254,10 @@ export const useProxies = () => { return node.name } + const getLatencyByName = (name: string) => { + return latencyMap()[getNowProxyNodeName(name)] + } + return { proxyLatencyTestingMap, proxyGroupLatencyTestingMap, @@ -264,7 +265,6 @@ export const useProxies = () => { updatingMap, isAllProviderUpdating, proxies, - proxyGroupNames, proxyProviders, proxyLatencyTest, proxyGroupLatencyTest, @@ -276,5 +276,6 @@ export const useProxies = () => { updateAllProvider, proxyProviderLatencyTest, getNowProxyNodeName, + getLatencyByName, } }