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
|
import * as React from 'react';
import styled from 'styled-components';
import { hasExpired } from '../../shared/account-expiry';
import { AuthFailureKind, parseAuthFailure } from '../../shared/auth-failure';
import NotificationArea from '../components/NotificationArea';
import { LoginState } from '../redux/account/reducers';
import { IConnectionReduxState } from '../redux/connection/reducers';
import { calculateHeaderBarStyle, DefaultHeaderBar } from './HeaderBar';
import ImageView from './ImageView';
import { Container, Layout } from './Layout';
import Map, { MarkerStyle, ZoomLevel } from './Map';
import TunnelControl from './TunnelControl';
interface IProps {
connection: IConnectionReduxState;
loginState: LoginState;
accountExpiry?: string;
blockWhenDisconnected: boolean;
selectedRelayName: string;
onSelectLocation: () => void;
onConnect: () => void;
onDisconnect: () => void;
onReconnect: () => void;
}
type MarkerOrSpinner = 'marker' | 'spinner' | 'none';
const StyledMap = styled(Map)({
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
zIndex: 0,
});
const StyledContainer = styled(Container)({
position: 'relative',
});
const Content = styled.div({
display: 'flex',
flex: 1,
flexDirection: 'column',
position: 'relative', // need this for z-index to work to cover the map
zIndex: 1,
});
const StatusIcon = styled(ImageView)({
position: 'absolute',
alignSelf: 'center',
marginTop: 94,
});
const StyledNotificationArea = styled(NotificationArea)({
position: 'absolute',
left: 0,
top: 0,
right: 0,
});
const StyledMain = styled.main({
display: 'flex',
flexDirection: 'column',
flex: 1,
});
interface IState {
isAccountExpired: boolean;
}
export default class Connect extends React.Component<IProps, IState> {
constructor(props: IProps) {
super(props);
this.state = {
isAccountExpired: this.checkAccountExpired(false),
};
}
public componentDidUpdate() {
this.updateAccountExpired();
}
public render() {
return (
<Layout>
<DefaultHeaderBar barStyle={calculateHeaderBarStyle(this.props.connection.status)} />
<StyledContainer>{this.renderMap()}</StyledContainer>
</Layout>
);
}
private updateAccountExpired() {
const nextAccountExpired = this.checkAccountExpired(this.state.isAccountExpired);
if (nextAccountExpired !== this.state.isAccountExpired) {
this.setState({
isAccountExpired: nextAccountExpired,
});
}
}
private checkAccountExpired(prevAccountExpired: boolean): boolean {
const tunnelState = this.props.connection.status;
// Blocked with auth failure / expired account
if (
tunnelState.state === 'error' &&
tunnelState.details.cause.reason === 'auth_failed' &&
parseAuthFailure(tunnelState.details.cause.reason).kind === AuthFailureKind.expiredAccount
) {
return true;
}
// Use the account expiry to deduce the account state
if (this.props.accountExpiry) {
return hasExpired(this.props.accountExpiry);
}
// Do not assume that the account hasn't expired if the expiry is not available at the moment
// instead return the last known state.
return prevAccountExpired;
}
private renderMap() {
return (
<>
<StyledMap {...this.getMapProps()} />
<Content>
<StyledNotificationArea />
<StyledMain>
{/* show spinner when connecting */}
{this.showMarkerOrSpinner() === 'spinner' ? (
<StatusIcon source="icon-spinner" height={60} width={60} />
) : null}
<TunnelControl
tunnelState={this.props.connection.status}
blockWhenDisconnected={this.props.blockWhenDisconnected}
selectedRelayName={this.props.selectedRelayName}
city={this.props.connection.city}
country={this.props.connection.country}
onConnect={this.props.onConnect}
onDisconnect={this.props.onDisconnect}
onReconnect={this.props.onReconnect}
onSelectLocation={this.props.onSelectLocation}
/>
</StyledMain>
</Content>
</>
);
}
private getMapProps(): Map['props'] {
const mapCenter = this.getMapCenter();
return {
center: mapCenter ?? [0, 0],
showMarker: this.showMarkerOrSpinner() === 'marker',
markerStyle: this.getMarkerStyle(),
zoomLevel: this.getZoomLevel(),
// a magic offset to align marker with spinner
offset: [0, mapCenter ? 123 : 0],
};
}
private getMapCenter(): [number, number] | undefined {
const { longitude, latitude } = this.props.connection;
return typeof longitude === 'number' && typeof latitude === 'number'
? [longitude, latitude]
: undefined;
}
private getMarkerStyle(): MarkerStyle {
const { status } = this.props.connection;
switch (status.state) {
case 'connecting':
case 'connected':
return MarkerStyle.secure;
case 'error':
return !status.details.blockFailure ? MarkerStyle.secure : MarkerStyle.unsecure;
case 'disconnected':
return MarkerStyle.unsecure;
case 'disconnecting':
switch (status.details) {
case 'block':
case 'reconnect':
return MarkerStyle.secure;
case 'nothing':
return MarkerStyle.unsecure;
}
}
}
private showMarkerOrSpinner(): MarkerOrSpinner {
if (!this.getMapCenter()) {
return 'none';
}
switch (this.props.connection.status.state) {
case 'error':
return 'none';
case 'connecting':
case 'disconnecting':
return 'spinner';
case 'connected':
case 'disconnected':
return 'marker';
}
}
private getZoomLevel(): ZoomLevel {
const { longitude, latitude, status } = this.props.connection;
if (typeof longitude === 'number' && typeof latitude === 'number') {
return status.state === 'connected' ? ZoomLevel.high : ZoomLevel.medium;
} else {
return ZoomLevel.low;
}
}
}
|