1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
import cx from 'clsx';
import * as React from 'react';
import { ChevronDown, Zap } from '~/components/shared/FeatherIcons';
import { useQuery } from 'react-query';
import * as proxiesAPI from '~/api/proxies';
import { fetchVersion } from '~/api/version';
import { useFilteredAndSorted } from '~/modules/proxies/hooks';
import { getProxyLatency } from '~/modules/proxies/utils';
import { fetchProxies, switchProxy } from '~/store/proxies';
import { DelayMapping, DispatchFn, ProxiesMapping, ProxyItem } from '~/store/types';
import { ClashAPIConfig } from '~/types';
import Button from '../Button';
import Collapsible from '../Collapsible';
import CollapsibleSectionHeader from '../CollapsibleSectionHeader';
import { useStoreActions } from '../StateProvider';
import s0 from './ProxyGroup.module.scss';
import { ProxyList, ProxyListGroupedByProvider, ProxyListSummaryView } from './ProxyList';
const { memo, useCallback, useLayoutEffect, useMemo, useRef, useState } = React;
function buildNowChain(proxies: ProxiesMapping, groupName: string): string | null {
const group = proxies[groupName] as ProxyItem & { now?: string };
if (!group?.now) return null;
const parts: string[] = [group.now];
let current = proxies[group.now] as ProxyItem & { now?: string };
let depth = 0;
while (current?.now && depth < 3) {
const next = proxies[current.now];
if (!next) break;
parts.push(current.now);
current = next as ProxyItem & { now?: string };
depth++;
}
return parts.join(' ⊙ ');
}
function countAvailableProxies(names: string[], delay: DelayMapping): number {
return names.filter((name) => {
const d = delay[name];
return d && typeof d.number === 'number' && d.number > 0;
}).length;
}
function getLatencyColor(number: number | undefined, httpsTest: boolean): string {
if (!number || number === 0) return '#909399';
const good = httpsTest ? 800 : 200;
const normal = httpsTest ? 1500 : 500;
if (number < good) return '#67c23a';
if (number < normal) return '#d4b75c';
return '#e67f3c';
}
function ZapWrapper() {
return (
<div className={s0.zapWrapper}>
<Zap size={16} />
</div>
);
}
const ProxyAvailabilityBar = memo(function ProxyAvailabilityBar({
all,
delay,
}: {
all: string[];
delay: DelayMapping;
}) {
const total = all.length;
const available = useMemo(() => countAvailableProxies(all, delay), [all, delay]);
const pct = total > 0 ? Math.round((available / total) * 100) : 0;
return (
<div className={s0.availBar}>
<div className={s0.availBarTrack}>
<div className={s0.availBarFill} style={{ width: `${pct}%` }} />
</div>
</div>
);
});
type Props = {
name: string;
delay: DelayMapping;
hideUnavailableProxies: boolean;
proxySortBy: string;
proxies: ProxiesMapping;
isOpen: boolean;
latencyTestUrl: string;
latencyTestTimeout?: number;
apiConfig: ClashAPIConfig;
dispatch: DispatchFn;
proxyGroupByProvider?: boolean;
};
export const ProxyGroup = memo(function ProxyGroup({
name,
delay,
hideUnavailableProxies,
proxySortBy,
proxies,
isOpen,
latencyTestUrl,
latencyTestTimeout = 5000,
apiConfig,
dispatch,
proxyGroupByProvider = false,
}: Props) {
const group = proxies[name] as ProxyItem & { all?: string[]; now?: string };
const { all: allItems = [], type, now } = group || {};
const all = useFilteredAndSorted(allItems, delay, hideUnavailableProxies, proxySortBy, proxies);
const httpsLatencyTest = latencyTestUrl.startsWith('https://');
const nowChain = useMemo(() => buildNowChain(proxies, name), [proxies, name]);
const nowLatency = useMemo(
() => (now ? getProxyLatency(proxies, delay, now) : undefined),
[proxies, delay, now],
);
const availableCount = useMemo(() => countAvailableProxies(allItems, delay), [allItems, delay]);
const qtyLabel = `${availableCount}/${allItems.length}`;
const { data: version } = useQuery(['/version', apiConfig], () =>
fetchVersion('/version', apiConfig),
);
const isSelectable = useMemo(
() => ['Selector', version.meta && 'Fallback', version.meta && 'URLTest'].includes(type),
[type, version.meta],
);
const {
app: { updateCollapsibleIsOpen },
proxies: { requestDelayForProxies },
} = useStoreActions();
const toggle = useCallback(() => {
updateCollapsibleIsOpen('proxyGroup', name, !isOpen);
}, [isOpen, updateCollapsibleIsOpen, name]);
const itemOnTapCallback = useCallback(
(proxyName) => {
if (!isSelectable) return;
dispatch(switchProxy(apiConfig, name, proxyName));
},
[apiConfig, dispatch, name, isSelectable],
);
const [isTestingLatency, setIsTestingLatency] = useState(false);
// measure collapsed container to decide dots vs bar
const summaryContainerRef = useRef<HTMLDivElement>(null);
const [containerWidth, setContainerWidth] = useState(0);
useLayoutEffect(() => {
const el = summaryContainerRef.current;
if (!el) return;
// sync read before first paint to avoid flash
const w = el.offsetWidth;
if (w > 0) setContainerWidth(w);
const ro = new ResizeObserver((entries) => {
setContainerWidth(entries[0].contentRect.width);
});
ro.observe(el);
return () => ro.disconnect();
}, []);
// dot slot = 15px width + 10px gap; padding-left:10px eats into available space
// n items fit when 15 + (n-1)*25 <= containerWidth - 10 → n <= containerWidth/25
const dotsPerRow = containerWidth > 0 ? Math.floor(containerWidth / 25) : Infinity;
const showBar = all.length > dotsPerRow;
const testLatency = useCallback(async () => {
setIsTestingLatency(true);
try {
if (version.meta === true) {
await proxiesAPI.requestDelayForProxyGroup(apiConfig, name, latencyTestUrl, latencyTestTimeout);
await dispatch(fetchProxies(apiConfig));
} else {
await requestDelayForProxies(apiConfig, all);
await dispatch(fetchProxies(apiConfig));
}
} catch (err) {}
setIsTestingLatency(false);
}, [all, apiConfig, dispatch, name, version.meta, latencyTestUrl, latencyTestTimeout, requestDelayForProxies]);
return (
<div className={s0.group}>
<div className={s0.groupHeader}>
<CollapsibleSectionHeader name={name} type={type} toggle={toggle} qty={qtyLabel} />
<div className={s0.btnGroup}>
<Button
kind="minimal"
onClick={toggle}
className={s0.btn}
title="Toggle collapsible section"
>
<span className={cx(s0.arrow, { [s0.isOpen]: isOpen })}>
<ChevronDown size={20} />
</span>
</Button>
<Button
title="Test latency"
kind="minimal"
onClick={testLatency}
isLoading={isTestingLatency}
>
<ZapWrapper />
</Button>
</div>
</div>
<Collapsible isOpen={isOpen}>
{proxyGroupByProvider ? (
<ProxyListGroupedByProvider
apiConfig={apiConfig}
all={all}
delay={delay}
dispatch={dispatch}
latencyTestUrl={latencyTestUrl}
now={now}
isSelectable={isSelectable}
itemOnTapCallback={itemOnTapCallback}
proxies={proxies}
/>
) : (
<ProxyList
apiConfig={apiConfig}
all={all}
delay={delay}
dispatch={dispatch}
latencyTestUrl={latencyTestUrl}
now={now}
isSelectable={isSelectable}
itemOnTapCallback={itemOnTapCallback}
proxies={proxies}
/>
)}
</Collapsible>
<Collapsible isOpen={!isOpen}>
{nowChain && (
<div className={s0.nowRow}>
<span className={s0.nowName}>⊙ {nowChain}</span>
{nowLatency?.number ? (
<span
className={s0.nowLatency}
style={{ color: getLatencyColor(nowLatency.number, httpsLatencyTest) }}
>
{nowLatency.number} ms
</span>
) : null}
</div>
)}
<div ref={summaryContainerRef}>
{showBar ? (
<ProxyAvailabilityBar all={allItems} delay={delay} />
) : (
<ProxyListSummaryView
apiConfig={apiConfig}
all={all}
delay={delay}
dispatch={dispatch}
latencyTestUrl={latencyTestUrl}
now={now}
isSelectable={isSelectable}
itemOnTapCallback={itemOnTapCallback}
proxies={proxies}
/>
)}
</div>
</Collapsible>
</div>
);
});
|