summaryrefslogtreecommitdiff
path: root/src/components/Proxy.js
blob: 939dcc44523c552c53e80a199e6f476b1f01f377 (plain)
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
import React from 'react';
import cx from 'clsx';

import { connect } from './StateProvider';
import ProxyLatency from './ProxyLatency';

import { getProxies, getDelay } from '../store/proxies';

import s0 from './Proxy.module.css';

const { useMemo } = React;

const colorMap = {
  // green
  good: '#67c23a',
  // yellow
  normal: '#d4b75c',
  // orange
  bad: '#e67f3c',
  // bad: '#F56C6C',
  na: '#909399',
};

function getLabelColor({ number } = {}) {
  if (number < 200) {
    return colorMap.good;
  } else if (number < 400) {
    return colorMap.normal;
  } else if (typeof number === 'number') {
    return colorMap.bad;
  }
  return colorMap.na;
}

/*
const colors = {
  Direct: '#408b43',
  Fallback: '#3483e8',
  Selector: '#387cec',
  Vmess: '#ca3487',
  Shadowsocks: '#1a7dc0',
  Socks5: '#2a477a',
  URLTest: '#3483e8',
  Http: '#d3782d'
};
*/

type ProxyProps = {
  name: string,
  now?: boolean,

  // connect injected
  // TODO refine type
  proxy: any,
  latency: any,
};

function ProxySmallImpl({ now, name, latency }: ProxyProps) {
  const color = useMemo(() => getLabelColor(latency), [latency]);
  const title = useMemo(() => {
    let ret = name;
    if (latency && typeof latency.number === 'number') {
      ret += ' ' + latency.number + ' ms';
    }
    return ret;
  }, [name, latency]);
  return (
    <div
      title={title}
      className={cx(s0.proxySmall, { [s0.now]: now })}
      style={{ backgroundColor: color }}
    />
  );
}

function Proxy({ now, name, proxy, latency }: ProxyProps) {
  const color = useMemo(() => getLabelColor(latency), [latency]);
  return (
    <div
      className={cx(s0.proxy, {
        [s0.now]: now,
        [s0.error]: latency && latency.error,
      })}
    >
      <div className={s0.proxyName}>{name}</div>
      <div className={s0.row}>
        <span className={s0.proxyType} style={{ opacity: now ? 0.6 : 0.2 }}>
          {proxy.type}
        </span>
        {latency && latency.number ? (
          <ProxyLatency number={latency.number} color={color} />
        ) : null}
      </div>
    </div>
  );
}

const mapState = (s, { name }) => {
  const proxies = getProxies(s);
  const delay = getDelay(s);
  return {
    proxy: proxies[name],
    latency: delay[name],
  };
};

export default connect(mapState)(Proxy);
export const ProxySmall = connect(mapState)(ProxySmallImpl);