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
|
import * as React from 'react';
import { useRecoilState } from 'recoil';
import { DelayMapping, ProxiesMapping, ProxyItem } from 'src/store/types';
import {
// types
NonProxyTypes,
// atom
proxyFilterText,
} from '../../store/proxies';
const { useMemo } = React;
function filterAvailableProxies(list: string[], delay: DelayMapping) {
return list.filter((name) => {
const d = delay[name];
if (d === undefined) {
return true;
}
if (d.number === 0) {
return false;
} else {
return true;
}
});
}
const getSortDelay = (
d:
| undefined
| {
number?: number;
},
proxyInfo: ProxyItem
) => {
if (d && typeof d.number === 'number' && d.number > 0) {
return d.number;
}
const type = proxyInfo && proxyInfo.type;
if (type && NonProxyTypes.indexOf(type) > -1) return -1;
return 999999;
};
const ProxySortingFns = {
Natural: (proxies: string[]) => proxies,
LatencyAsc: (
proxies: string[],
delay: DelayMapping,
proxyMapping?: ProxiesMapping
) => {
return proxies.sort((a, b) => {
const d1 = getSortDelay(delay[a], proxyMapping && proxyMapping[a]);
const d2 = getSortDelay(delay[b], proxyMapping && proxyMapping[b]);
return d1 - d2;
});
},
LatencyDesc: (
proxies: string[],
delay: DelayMapping,
proxyMapping?: ProxiesMapping
) => {
return proxies.sort((a, b) => {
const d1 = getSortDelay(delay[a], proxyMapping && proxyMapping[a]);
const d2 = getSortDelay(delay[b], proxyMapping && proxyMapping[b]);
return d2 - d1;
});
},
NameAsc: (proxies: string[]) => {
return proxies.sort();
},
NameDesc: (proxies: string[]) => {
return proxies.sort((a, b) => {
if (a > b) return -1;
if (a < b) return 1;
return 0;
});
},
};
function filterStrArr(all: string[], searchText: string) {
const segments = searchText
.toLowerCase()
.split(' ')
.map((x) => x.trim())
.filter((x) => !!x);
if (segments.length === 0) return all;
return all.filter((name) => {
let i = 0;
for (; i < segments.length; i++) {
const seg = segments[i];
if (name.toLowerCase().indexOf(seg) > -1) return true;
}
return false;
});
}
function filterAvailableProxiesAndSort(
all: string[],
delay: DelayMapping,
hideUnavailableProxies: boolean,
filterText: string,
proxySortBy: string,
proxies?: ProxiesMapping
) {
// all is freezed
let filtered = [...all];
if (hideUnavailableProxies) {
filtered = filterAvailableProxies(all, delay);
}
if (typeof filterText === 'string' && filterText !== '') {
filtered = filterStrArr(filtered, filterText);
}
return ProxySortingFns[proxySortBy](filtered, delay, proxies);
}
export function useFilteredAndSorted(
all: string[],
delay: DelayMapping,
hideUnavailableProxies: boolean,
proxySortBy: string,
proxies?: ProxiesMapping
) {
const [filterText] = useRecoilState(proxyFilterText);
return useMemo(
() =>
filterAvailableProxiesAndSort(
all,
delay,
hideUnavailableProxies,
filterText,
proxySortBy,
proxies
),
[all, delay, hideUnavailableProxies, filterText, proxySortBy, proxies]
);
}
|