-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathindex.tsx
87 lines (81 loc) · 2.56 KB
/
index.tsx
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
import React from 'react';
import { forage } from '@tauri-apps/tauri-forage';
import { DateTime } from 'luxon';
import { useClientContext } from 'src/context/main';
import { useTauriEvents } from 'src/utils';
import { Connected } from './Connected';
import { Disconnected } from './Disconnected';
export const ConnectionPage = () => {
const context = useClientContext();
const [busy, setBusy] = React.useState<boolean>();
useTauriEvents('help://clear-storage', (_event) => {
console.log('About to clear local storage...');
// clear local storage
try {
forage.clear()();
console.log('Local storage cleared');
} catch (e) {
console.error('Failed to clear local storage', e);
}
});
const handleConnectClick = async () => {
const currentStatus = context.connectionStatus;
if (currentStatus === 'connected' || currentStatus === 'disconnected') {
setBusy(true);
// eslint-disable-next-line default-case
switch (currentStatus) {
case 'disconnected':
await context.setSerivceProvider();
await context.startConnecting();
context.setConnectedSince(DateTime.now());
context.setShowInfoModal(true);
break;
case 'connected':
await context.startDisconnecting();
context.setConnectedSince(undefined);
break;
}
setBusy(false);
}
};
const closeInfoModal = () => context.setShowInfoModal(false);
if (context.connectionStatus === 'connected')
return (
<Connected
error={context.error}
clearError={context.clearError}
status={context.connectionStatus}
showInfoModal={context.showInfoModal}
busy={busy}
onConnectClick={handleConnectClick}
ipAddress="127.0.0.1"
port={1080}
gatewayPerformance={context.gatewayPerformance}
connectedSince={context.connectedSince}
serviceProvider={context.selectedProvider}
closeInfoModal={closeInfoModal}
stats={[
{
label: 'in:',
totalBytes: 1024,
rateBytesPerSecond: 1024 * 1024 * 1024 + 10,
},
{
label: 'out:',
totalBytes: 1024 * 1024 * 1024 * 1024 * 20,
rateBytesPerSecond: 1024 * 1024 + 10,
},
]}
/>
);
return (
<Disconnected
busy={busy}
error={context.error}
onConnectClick={handleConnectClick}
clearError={context.clearError}
status={context.connectionStatus}
serviceProvider={context.selectedProvider}
/>
);
};