-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathselect-location-hooks.ts
142 lines (126 loc) · 4.38 KB
/
select-location-hooks.ts
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
import { useCallback } from 'react';
import {
BridgeSettings,
RelayLocation,
RelaySettings,
wrapConstraint,
} from '../../../shared/daemon-rpc-types';
import log from '../../../shared/logging';
import { useAppContext } from '../../context';
import { useRelaySettingsModifier } from '../../lib/constraint-updater';
import { useBridgeSettingsModifier } from '../../lib/constraint-updater';
import { useHistory } from '../../lib/history';
import { LocationType, SpecialBridgeLocationType } from './select-location-types';
import { useSelectLocationContext } from './SelectLocationContainer';
export function useOnSelectExitLocation() {
const onSelectLocation = useOnSelectLocation();
const history = useHistory();
const relaySettingsModifier = useRelaySettingsModifier();
const { connectTunnel } = useAppContext();
const onSelectRelay = useCallback(
async (relayLocation: RelayLocation) => {
const settings = relaySettingsModifier((settings) => ({
...settings,
location: wrapConstraint(relayLocation),
}));
history.pop();
await onSelectLocation({ normal: settings });
await connectTunnel();
},
[history, relaySettingsModifier],
);
const onSelectSpecial = useCallback((_location: undefined) => {
throw new Error('relayLocation should never be undefined');
}, []);
return [onSelectRelay, onSelectSpecial] as const;
}
export function useOnSelectEntryLocation() {
const onSelectLocation = useOnSelectLocation();
const { setLocationType } = useSelectLocationContext();
const relaySettingsModifier = useRelaySettingsModifier();
const onSelectRelay = useCallback(
async (entryLocation: RelayLocation) => {
setLocationType(LocationType.exit);
const settings = relaySettingsModifier((settings) => {
settings.wireguardConstraints.entryLocation = wrapConstraint(entryLocation);
return settings;
});
await onSelectLocation({ normal: settings });
},
[relaySettingsModifier],
);
const onSelectSpecial = useCallback(
async (_location: 'any') => {
setLocationType(LocationType.exit);
const settings = relaySettingsModifier((settings) => {
settings.wireguardConstraints.entryLocation = 'any';
return settings;
});
await onSelectLocation({ normal: settings });
},
[relaySettingsModifier],
);
return [onSelectRelay, onSelectSpecial] as const;
}
function useOnSelectLocation() {
const { setRelaySettings } = useAppContext();
return useCallback(async (relaySettings: RelaySettings) => {
try {
await setRelaySettings(relaySettings);
} catch (e) {
const error = e as Error;
log.error(`Failed to select the location: ${error.message}`);
}
}, []);
}
export function useOnSelectBridgeLocation() {
const { updateBridgeSettings } = useAppContext();
const { setLocationType } = useSelectLocationContext();
const bridgeSettingsModifier = useBridgeSettingsModifier();
const setLocation = useCallback(async (bridgeUpdate: BridgeSettings) => {
if (bridgeUpdate) {
setLocationType(LocationType.exit);
try {
await updateBridgeSettings(bridgeUpdate);
} catch (e) {
const error = e as Error;
log.error(`Failed to select the bridge location: ${error.message}`);
}
}
}, []);
const onSelectRelay = useCallback(
(location: RelayLocation) => {
return setLocation(
bridgeSettingsModifier((bridgeSettings) => {
bridgeSettings.type = 'normal';
bridgeSettings.normal.location = wrapConstraint(location);
return bridgeSettings;
}),
);
},
[bridgeSettingsModifier],
);
const onSelectSpecial = useCallback(
(location: SpecialBridgeLocationType) => {
switch (location) {
case SpecialBridgeLocationType.closestToExit:
return setLocation(
bridgeSettingsModifier((bridgeSettings) => {
bridgeSettings.type = 'normal';
bridgeSettings.normal.location = 'any';
return bridgeSettings;
}),
);
case SpecialBridgeLocationType.custom:
return setLocation(
bridgeSettingsModifier((bridgeSettings) => {
bridgeSettings.type = 'custom';
return bridgeSettings;
}),
);
}
},
[bridgeSettingsModifier],
);
return [onSelectRelay, onSelectSpecial] as const;
}