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
|
// @flow
import React, { Component } from 'react';
import { Layout, Container, Header } from './Layout';
import CustomScrollbars from './CustomScrollbars';
import Accordion from './Accordion';
import ChevronDownSVG from '../assets/images/icon-chevron-down.svg';
import ChevronUpSVG from '../assets/images/icon-chevron-up.svg';
import TickSVG from '../assets/images/icon-tick.svg';
import type { SettingsReduxState } from '../redux/settings/reducers';
import type { RelayLocation, RelayListCity, RelayListCountry } from '../lib/ipc-facade';
export type SelectLocationProps = {
settings: SettingsReduxState,
onClose: () => void;
onSelect: (location: RelayLocation) => void;
};
export default class SelectLocation extends Component {
props: SelectLocationProps;
_selectedCell: ?HTMLElement;
state = {
expanded: ([]: Array<string>),
};
constructor(props: SelectLocationProps, context?: any) {
super(props, context);
// set initially expanded country based on relaySettings
const relaySettings = this.props.settings.relaySettings;
if(relaySettings.normal) {
const { location } = relaySettings.normal;
if(location === 'any') {
// no-op
} else if(location.country) {
this.state.expanded.push(location.country);
} else if(location.city) {
this.state.expanded.push(location.city[0]);
}
}
}
componentDidMount() {
// restore scroll to selected cell
const cell = this._selectedCell;
if(cell) {
// this is non-standard webkit method but it works great!
if(typeof(cell.scrollIntoViewIfNeeded) !== 'function') {
console.warn('HTMLElement.scrollIntoViewIfNeeded() is not available anymore! Please replace it with viable alternative.');
return;
}
cell.scrollIntoViewIfNeeded(true);
}
}
render() {
return (
<Layout>
<Header hidden={ true } style={ 'defaultDark' } />
<Container>
<div className="select-location">
<button className="select-location__close" onClick={ this.props.onClose } />
<div className="select-location__container">
<div className="select-location__header">
<h2 className="select-location__title">Select location</h2>
</div>
<CustomScrollbars autoHide={ true }>
<div>
<div className="select-location__subtitle">
While connected, your real location is masked with a private and secure location in the selected region
</div>
{ this.props.settings.relayLocations.countries.map((relayCountry) => {
return this._renderCountry(relayCountry);
}) }
</div>
</CustomScrollbars>
</div>
</div>
</Container>
</Layout>
);
}
_isSelected(selectedLocation: RelayLocation) {
const { relaySettings } = this.props.settings;
if(relaySettings.normal) {
const otherLocation = relaySettings.normal.location;
if(selectedLocation.country && otherLocation.country &&
selectedLocation.country === otherLocation.country) {
return true;
}
if(Array.isArray(selectedLocation.city) && Array.isArray(otherLocation.city)) {
const selectedCity = selectedLocation.city;
const otherCity = otherLocation.city;
return selectedCity.length === otherCity.length &&
selectedCity.every((v, i) => v === otherCity[i]);
}
}
return false;
}
_toggleCollapse = (countryCode: string) => {
this.setState((state) => {
const expanded = state.expanded.slice();
const index = expanded.indexOf(countryCode);
if(index === -1) {
expanded.push(countryCode);
} else {
expanded.splice(index, 1);
}
return { expanded };
});
}
_relayStatusIndicator(active: boolean) {
const statusClass = active ? 'select-location-relay-status--active' : 'select-location-relay-status--inactive';
return (<div className={ 'select-location-relay-status ' + statusClass }></div>);
}
_renderCountry(relayCountry: RelayListCountry) {
const countryHasActiveRelays = relayCountry.cities.some((relayCity) => {
return relayCity.has_active_relays;
});
const isSelected = this._isSelected({ country: relayCountry.code });
// either expanded by user or when the city selected within the country
const isExpanded = this.state.expanded.includes(relayCountry.code);
const handleSelect = (countryHasActiveRelays && !isSelected) ? () => {
this.props.onSelect({ country: relayCountry.code });
} : undefined;
const handleCollapse = (e) => {
this._toggleCollapse(relayCountry.code);
e.stopPropagation();
};
const countryClass = 'select-location__cell ' +
(isSelected ? 'select-location__cell--selected' : '');
const onRef = isSelected ? (element) => {
this._selectedCell = element;
} : undefined;
return (
<div key={ relayCountry.code } className="select-location__country">
<div className={ countryClass }
onClick={ handleSelect }
ref={ onRef }>
<div className="select-location__cell-content">
<div className="select-location__cell-icon">
{ isSelected ?
<TickSVG /> :
this._relayStatusIndicator(countryHasActiveRelays) }
</div>
<div className="select-location__cell-label">{ relayCountry.name }</div>
</div>
{ countryHasActiveRelays && <button type="button" className="select-location__collapse-button" onClick={ handleCollapse }>
{ isExpanded ?
<ChevronUpSVG className="select-location__collapse-icon" /> :
<ChevronDownSVG className="select-location__collapse-icon" /> }
</button> }
</div>
{ countryHasActiveRelays && relayCountry.cities.length > 0 &&
(<Accordion className="select-location__cities" height={ isExpanded ? 'auto' : 0 }>
{ relayCountry.cities.map((relayCity) => this._renderCity(relayCountry.code, relayCity)) }
</Accordion>)
}
</div>
);
}
_renderCity(countryCode: string, relayCity: RelayListCity) {
const relayLocation: RelayLocation = { city: [countryCode, relayCity.code] };
const isSelected = this._isSelected(relayLocation);
const cityHasActiveRelays = relayCity.has_active_relays;
const key = countryCode + '_' + relayCity.code;
const cityClass = 'select-location__sub-cell ' +
(isSelected ? 'select-location__sub-cell--selected' : '');
const handleSelect = (cityHasActiveRelays && !isSelected) ? () => {
this.props.onSelect(relayLocation);
} : undefined;
const onRef = isSelected ? (element) => {
this._selectedCell = element;
} : undefined;
return (
<div key={ key }
className={ cityClass }
onClick={ handleSelect }
ref={ onRef }>
<div className="select-location__cell-icon">
{ isSelected ?
<TickSVG /> :
this._relayStatusIndicator(cityHasActiveRelays) }
</div>
<div className="select-location__cell-label">{ relayCity.name }</div>
</div>
);
}
}
|