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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
import * as React from 'react';
import { LogOut } from 'react-feather';
import { useTranslation } from 'react-i18next';
import * as logsApi from 'src/api/logs';
import Select from 'src/components/shared/Select';
import { ClashGeneralConfig, DispatchFn, State } from 'src/store/types';
import { ClashAPIConfig } from 'src/types';
import {
getClashAPIConfig,
getLatencyTestUrl,
getSelectedChartStyleIndex,
} from '../store/app';
import { fetchConfigs, getConfigs, updateConfigs } from '../store/configs';
import { openModal } from '../store/modals';
import Button from './Button';
import s0 from './Config.module.scss';
import ContentHeader from './ContentHeader';
import Input, { SelfControlledInput } from './Input';
import { Selection2 } from './Selection';
import { connect, useStoreActions } from './StateProvider';
import Switch from './SwitchThemed';
import TrafficChartSample from './TrafficChartSample';
// import ToggleSwitch from './ToggleSwitch';
const { useEffect, useState, useCallback, useRef, useMemo } = React;
const propsList = [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }];
const logLeveOptions = [
['debug', 'Debug'],
['warning', 'Warning'],
['info', 'Info'],
['error', 'Error'],
['silent', 'Silent'],
];
const portFields = [
{ key: 'port', label: 'HTTP Proxy Port' },
{ key: 'socks-port', label: 'SOCKS5 Proxy Port' },
{ key: 'mixed-port', label: 'Mixed Port' },
{ key: 'redir-port', label: 'Redir Port' },
];
const langOptions = [
['zh', '中文'],
['en', 'English'],
];
const modeOptions = [
['Global', 'Global'],
['Rule', 'Rule'],
['Direct', 'Direct'],
];
const mapState = (s: State) => ({
configs: getConfigs(s),
apiConfig: getClashAPIConfig(s),
});
const mapState2 = (s: State) => ({
selectedChartStyleIndex: getSelectedChartStyleIndex(s),
latencyTestUrl: getLatencyTestUrl(s),
apiConfig: getClashAPIConfig(s),
});
const Config = connect(mapState2)(ConfigImpl);
export default connect(mapState)(ConfigContainer);
function ConfigContainer({ dispatch, configs, apiConfig }) {
useEffect(() => {
dispatch(fetchConfigs(apiConfig));
}, [dispatch, apiConfig]);
return <Config configs={configs} />;
}
type ConfigImplProps = {
dispatch: DispatchFn;
configs: ClashGeneralConfig;
selectedChartStyleIndex: number;
latencyTestUrl: string;
apiConfig: ClashAPIConfig;
};
function ConfigImpl({
dispatch,
configs,
selectedChartStyleIndex,
latencyTestUrl,
apiConfig,
}: ConfigImplProps) {
const [configState, setConfigStateInternal] = useState(configs);
const refConfigs = useRef(configs);
useEffect(() => {
if (refConfigs.current !== configs) {
setConfigStateInternal(configs);
}
refConfigs.current = configs;
}, [configs]);
const openAPIConfigModal = useCallback(() => {
dispatch(openModal('apiConfig'));
}, [dispatch]);
const setConfigState = useCallback(
(name, val) => {
setConfigStateInternal({ ...configState, [name]: val });
},
[configState]
);
const handleSwitchOnChange = useCallback(
(checked: boolean) => {
const name = 'allow-lan';
const value = checked;
setConfigState(name, value);
dispatch(updateConfigs(apiConfig, { 'allow-lan': value }));
},
[apiConfig, dispatch, setConfigState]
);
const handleChangeValue = useCallback(
({ name, value }) => {
switch (name) {
case 'mode':
case 'log-level':
setConfigState(name, value);
dispatch(updateConfigs(apiConfig, { [name]: value }));
if (name === 'log-level') {
logsApi.reconnect({ ...apiConfig, logLevel: value });
}
break;
case 'redir-port':
case 'socks-port':
case 'mixed-port':
case 'port':
if (value !== '') {
const num = parseInt(value, 10);
if (num < 0 || num > 65535) return;
}
setConfigState(name, value);
break;
default:
return;
}
},
[apiConfig, dispatch, setConfigState]
);
const handleInputOnChange = useCallback(
(e) => handleChangeValue(e.target),
[handleChangeValue]
);
const { selectChartStyleIndex, updateAppConfig } = useStoreActions();
const handleInputOnBlur = useCallback(
(e) => {
const target = e.target;
const { name, value } = target;
switch (name) {
case 'port':
case 'socks-port':
case 'mixed-port':
case 'redir-port': {
const num = parseInt(value, 10);
if (num < 0 || num > 65535) return;
dispatch(updateConfigs(apiConfig, { [name]: num }));
break;
}
case 'latencyTestUrl': {
updateAppConfig(name, value);
break;
}
default:
throw new Error(`unknown input name ${name}`);
}
},
[apiConfig, dispatch, updateAppConfig]
);
const mode = useMemo(() => {
const m = configState.mode;
return typeof m === 'string' && m[0].toUpperCase() + m.slice(1);
}, [configState.mode]);
const { t, i18n } = useTranslation();
return (
<div>
<ContentHeader title={t('Config')} />
<div className={s0.root}>
{portFields.map((f) =>
configState[f.key] !== undefined ? (
<div key={f.key}>
<div className={s0.label}>{f.label}</div>
<Input
name={f.key}
value={configState[f.key]}
onChange={handleInputOnChange}
// @ts-expect-error ts-migrate(2322) FIXME: Type '{ name: string; value: any; onChange: (e: an... Remove this comment to see the full error message
onBlur={handleInputOnBlur}
/>
</div>
) : null
)}
<div>
<div className={s0.label}>Mode</div>
<Select
options={modeOptions}
selected={mode}
onChange={(e) =>
handleChangeValue({ name: 'mode', value: e.target.value })
}
/>
</div>
<div>
<div className={s0.label}>Log Level</div>
<Select
options={logLeveOptions}
selected={configState['log-level']}
onChange={(e) =>
handleChangeValue({ name: 'log-level', value: e.target.value })
}
/>
</div>
<div>
<div className={s0.label}>Allow LAN</div>
<div className={s0.wrapSwitch}>
<Switch
name="allow-lan"
checked={configState['allow-lan']}
onChange={handleSwitchOnChange}
/>
</div>
</div>
</div>
<div className={s0.sep}>
<div />
</div>
<div className={s0.section}>
<div>
<div className={s0.label}>{t('latency_test_url')}</div>
<SelfControlledInput
name="latencyTestUrl"
type="text"
value={latencyTestUrl}
onBlur={handleInputOnBlur}
/>
</div>
<div>
<div className={s0.label}>{t('lang')}</div>
<div>
<Select
options={langOptions}
selected={i18n.language}
onChange={(e) => i18n.changeLanguage(e.target.value)}
/>
</div>
</div>
<div>
<div className={s0.label}>{t('chart_style')}</div>
<Selection2
OptionComponent={TrafficChartSample}
optionPropsList={propsList}
selectedIndex={selectedChartStyleIndex}
onChange={selectChartStyleIndex}
/>
</div>
<div>
<div className={s0.label}>Action</div>
<Button
start={<LogOut size={16} />}
label="Switch backend"
onClick={openAPIConfigModal}
/>
</div>
</div>
</div>
);
}
|