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
|
import cx from 'clsx';
import * as React from 'react';
import { keyCodes } from 'src/misc/keycode';
import { getDelay, getProxies, NonProxyTypes } from '../../store/proxies';
import { connect } from '../StateProvider';
import s0 from './Proxy.module.scss';
import { ProxyLatency } from './ProxyLatency';
const { useMemo } = React;
const colorMap = {
// green
good: '#67c23a',
// yellow
normal: '#d4b75c',
// orange
bad: '#e67f3c',
// bad: '#F56C6C',
na: '#909399',
};
function getLabelColor({
number,
}: {
number?: number;
} = {}) {
if (number === 0) {
return colorMap.na;
} else if (number < 200) {
return colorMap.good;
} else if (number < 400) {
return colorMap.normal;
} else if (typeof number === 'number') {
return colorMap.bad;
}
return colorMap.na;
}
function getProxyDotBackgroundColor(
latency: {
number?: number;
},
proxyType: string
) {
if (NonProxyTypes.indexOf(proxyType) > -1) {
return 'linear-gradient(135deg, white 15%, #999 15% 30%, white 30% 45%, #999 45% 60%, white 60% 75%, #999 75% 90%, white 90% 100%)';
}
return getLabelColor(latency);
}
type ProxyProps = {
name: string;
now?: boolean;
proxy: any;
latency: any;
isSelectable?: boolean;
onClick?: (proxyName: string) => unknown;
};
function ProxySmallImpl({
now,
name,
proxy,
latency,
isSelectable,
onClick,
}: ProxyProps) {
const color = useMemo(() => getProxyDotBackgroundColor(latency, proxy.type), [
latency,
proxy,
]);
const title = useMemo(() => {
let ret = name;
if (latency && typeof latency.number === 'number') {
ret += ' ' + latency.number + ' ms';
}
return ret;
}, [name, latency]);
const doSelect = React.useCallback(() => {
isSelectable && onClick && onClick(name);
}, [name, onClick, isSelectable]);
const className = useMemo(() => {
return cx(s0.proxySmall, {
[s0.now]: now,
[s0.selectable]: isSelectable,
});
}, [isSelectable, now]);
const handleKeyDown = React.useCallback(
(e: React.KeyboardEvent) => {
if (e.keyCode === keyCodes.Enter) {
doSelect();
}
},
[doSelect]
);
return (
<div
title={title}
className={className}
style={{ background: color }}
onClick={doSelect}
onKeyDown={handleKeyDown}
role={isSelectable ? 'menuitem' : ''}
/>
);
}
function formatProxyType(t: string) {
if (t === 'Shadowsocks') return 'SS';
return t;
}
function ProxyImpl({
now,
name,
proxy,
latency,
isSelectable,
onClick,
}: ProxyProps) {
const color = useMemo(() => getLabelColor(latency), [latency]);
const doSelect = React.useCallback(() => {
isSelectable && onClick && onClick(name);
}, [name, onClick, isSelectable]);
const handleKeyDown = React.useCallback(
(e: React.KeyboardEvent) => {
if (e.keyCode === keyCodes.Enter) {
doSelect();
}
},
[doSelect]
);
const className = useMemo(() => {
return cx(s0.proxy, {
[s0.now]: now,
[s0.error]: latency && latency.error,
[s0.selectable]: isSelectable,
});
}, [isSelectable, now, latency]);
return (
<div
tabIndex={0}
className={className}
onClick={doSelect}
onKeyDown={handleKeyDown}
role={isSelectable ? 'menuitem' : ''}
>
<div className={s0.proxyName}>{name}</div>
<div className={s0.row}>
<span className={s0.proxyType} style={{ opacity: now ? 0.6 : 0.2 }}>
{formatProxyType(proxy.type)}
</span>
{latency && latency.number ? (
<ProxyLatency number={latency.number} color={color} />
) : null}
</div>
</div>
);
}
const mapState = (s: any, { name }) => {
const proxies = getProxies(s);
const delay = getDelay(s);
return {
proxy: proxies[name],
latency: delay[name],
};
};
export const Proxy = connect(mapState)(ProxyImpl);
export const ProxySmall = connect(mapState)(ProxySmallImpl);
|