summaryrefslogtreecommitdiff
path: root/src/components/ProxyGroup.js
blob: fffb020826e998ef6214f23c21486522464da380 (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
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
import React from 'react';
import cx from 'classnames';
import memoizeOne from 'memoize-one';

import { connect } from './StateProvider';
import { getProxies, getRtFilterSwitch } from '../store/proxies';
import CollapsibleSectionHeader from './CollapsibleSectionHeader';
import Proxy, { ProxySmall } from './Proxy';
import { useToggle } from '../hooks/basic';

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

import { switchProxy } from '../store/proxies';

const { useCallback, useMemo } = React;

function ProxyGroup({ name, all, type, now, apiConfig, dispatch }) {
  const isSelectable = useMemo(() => type === 'Selector', [type]);
  const [isOpen, toggle] = useToggle(true);
  const itemOnTapCallback = useCallback(
    proxyName => {
      if (!isSelectable) return;
      dispatch(switchProxy(apiConfig, name, proxyName));
    },
    [apiConfig, dispatch, name, isSelectable]
  );

  return (
    <div className={s0.group}>
      <CollapsibleSectionHeader
        name={name}
        type={type}
        toggle={toggle}
        qty={all.length}
        isOpen={isOpen}
      />
      {isOpen ? (
        <ProxyList
          all={all}
          now={now}
          isSelectable={isSelectable}
          itemOnTapCallback={itemOnTapCallback}
        />
      ) : (
        <ProxyListSummaryView all={all} />
      )}
    </div>
  );
}

type ProxyListProps = {
  all: string[],
  now?: string,
  isSelectable?: boolean,
  itemOnTapCallback?: string => void,
  show?: boolean
};
export function ProxyList({
  all,
  now,
  isSelectable,
  itemOnTapCallback,
  sortedAll
}: ProxyListProps) {
  const proxies = sortedAll || all;

  return (
    <div className={s0.list}>
      {proxies.map(proxyName => {
        const proxyClassName = cx(s0.proxy, {
          [s0.proxySelectable]: isSelectable
        });
        return (
          <div
            className={proxyClassName}
            key={proxyName}
            onClick={() => {
              if (!isSelectable || !itemOnTapCallback) return;
              itemOnTapCallback(proxyName);
            }}
          >
            <Proxy name={proxyName} now={proxyName === now} />
          </div>
        );
      })}
    </div>
  );
}

const getSortDelay = (d, w) => {
  if (d === undefined) {
    return 0;
  }
  if (!d.error && d.number > 0) {
    return d.number;
  }

  return w;
};

function filterAvailableProxies(list, delay) {
  return list.filter(name => {
    const d = delay[name];
    if (d === undefined) {
      return true;
    }
    if (d.error || d.number === 0) {
      return false;
    } else {
      return true;
    }
  });
}

function filterAvailableProxiesAndSortImpl(all, delay, filterByRt) {
  // all is freezed
  let filtered = [...all];
  if (filterByRt) {
    filtered = filterAvailableProxies(all, delay);
  }

  return filtered.sort((first, second) => {
    const d1 = getSortDelay(delay[first], 999999);
    const d2 = getSortDelay(delay[second], 999999);
    return d1 - d2;
  });
}
export const filterAvailableProxiesAndSort = memoizeOne(
  filterAvailableProxiesAndSortImpl
);

export function ProxyListSummaryView({
  all,
  now,
  isSelectable,
  itemOnTapCallback
}: ProxyListProps) {
  return (
    <div className={s0.list}>
      {all.map(proxyName => {
        const proxyClassName = cx(s0.proxy, {
          [s0.proxySelectable]: isSelectable
        });
        return (
          <div
            className={proxyClassName}
            key={proxyName}
            onClick={() => {
              if (!isSelectable || !itemOnTapCallback) return;
              itemOnTapCallback(proxyName);
            }}
          >
            <ProxySmall name={proxyName} now={proxyName === now} />
          </div>
        );
      })}
    </div>
  );
}

export default connect((s, { name, delay }) => {
  const proxies = getProxies(s);
  const filterByRt = getRtFilterSwitch(s);
  const group = proxies[name];
  const { all, type, now } = group;
  return {
    all: filterAvailableProxiesAndSort(all, delay, filterByRt),
    type,
    now
  };
})(ProxyGroup);