summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/components/SelectLocation.tsx
blob: 7a1340c44faaa0cb3b649a48ac3d97d0bfb66926 (plain)
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
import * as React from 'react';
import ReactDOM from 'react-dom';
import { Component, View } from 'reactxp';
import { colors } from '../../config.json';
import { LiftedConstraint, RelayLocation } from '../../shared/daemon-rpc-types';
import { messages } from '../../shared/gettext';
import { IRelayLocationRedux } from '../redux/settings/reducers';
import { LocationScope } from '../redux/userinterface/reducers';
import * as Cell from './Cell';
import CustomScrollbars from './CustomScrollbars';
import { Container, Layout } from './Layout';
import LocationList from './LocationList';
import {
  CloseBarItem,
  NavigationBar,
  NavigationContainer,
  NavigationScrollbars,
  ScopeBar,
  ScopeBarItem,
  StickyContentContainer,
  StickyContentHolder,
  TitleBarItem,
} from './NavigationBar';
import styles from './SelectLocationStyles';
import SettingsHeader, { HeaderSubTitle, HeaderTitle } from './SettingsHeader';

interface IProps {
  locationScope: LocationScope;
  selectedExitLocation?: RelayLocation;
  selectedBridgeLocation?: LiftedConstraint<RelayLocation>;
  relayLocations: IRelayLocationRedux[];
  bridgeLocations: IRelayLocationRedux[];
  allowBridgeSelection: boolean;
  onClose: () => void;
  onChangeLocationScope: (location: LocationScope) => void;
  onSelectExitLocation: (location: RelayLocation) => void;
  onSelectBridgeLocation: (location: RelayLocation) => void;
  onSelectClosestToExit: () => void;
}

export default class SelectLocation extends Component<IProps> {
  private scrollView = React.createRef<CustomScrollbars>();
  private exitLocationList = React.createRef<LocationList>();
  private bridgeLocationList = React.createRef<LocationList>();

  public componentDidMount() {
    this.scrollToSelectedCell();
  }

  public componentDidUpdate(prevProps: IProps) {
    if (this.props.locationScope !== prevProps.locationScope) {
      this.scrollToSelectedCell();
    }
  }

  public render() {
    return (
      <Layout>
        <Container>
          <View style={styles.select_location}>
            <NavigationContainer>
              <NavigationBar>
                <CloseBarItem action={this.props.onClose} />
                <TitleBarItem>
                  {// TRANSLATORS: Title label in navigation bar
                  messages.pgettext('select-location-nav', 'Select location')}
                </TitleBarItem>
              </NavigationBar>
              <StickyContentContainer style={styles.container}>
                <NavigationScrollbars ref={this.scrollView}>
                  <View style={styles.content}>
                    <SettingsHeader
                      style={this.props.allowBridgeSelection ? styles.headerWithScope : undefined}>
                      <HeaderTitle>
                        {messages.pgettext('select-location-view', 'Select location')}
                      </HeaderTitle>
                      <HeaderSubTitle>
                        {this.props.locationScope === LocationScope.relay
                          ? messages.pgettext(
                              'select-location-view',
                              'While connected, your real location is masked with a private and secure location in the selected region',
                            )
                          : messages.pgettext(
                              'select-location-view',
                              'While connected, your traffic will be routed through two secure locations, the entry point (a bridge server) and the exit point (a VPN server)',
                            )}
                      </HeaderSubTitle>
                    </SettingsHeader>

                    {this.props.allowBridgeSelection && (
                      <StickyContentHolder style={styles.stickyHolder}>
                        <View style={styles.stickyContent}>
                          <ScopeBar
                            defaultSelectedIndex={this.props.locationScope}
                            onChange={this.props.onChangeLocationScope}>
                            <ScopeBarItem>
                              {messages.pgettext('select-location-nav', 'Entry')}
                            </ScopeBarItem>
                            <ScopeBarItem>
                              {messages.pgettext('select-location-nav', 'Exit')}
                            </ScopeBarItem>
                          </ScopeBar>
                        </View>
                      </StickyContentHolder>
                    )}

                    {this.props.locationScope === LocationScope.relay ? (
                      <LocationList
                        key={'exit-locations'}
                        ref={this.exitLocationList}
                        selectedLocation={this.props.selectedExitLocation}
                        relayLocations={this.props.relayLocations}
                        onSelect={this.props.onSelectExitLocation}
                      />
                    ) : (
                      <React.Fragment>
                        <View>
                          <ClosestToExitCell
                            onSelect={this.props.onSelectClosestToExit}
                            isSelected={this.props.selectedBridgeLocation === 'any'}
                          />
                        </View>
                        <LocationList
                          key={'bridge-locations'}
                          ref={this.bridgeLocationList}
                          selectedLocation={
                            this.props.selectedBridgeLocation !== 'any'
                              ? this.props.selectedBridgeLocation
                              : undefined
                          }
                          relayLocations={this.props.bridgeLocations}
                          onSelect={this.props.onSelectBridgeLocation}
                        />
                      </React.Fragment>
                    )}
                  </View>
                </NavigationScrollbars>
              </StickyContentContainer>
            </NavigationContainer>
          </View>
        </Container>
      </Layout>
    );
  }

  private scrollToSelectedCell() {
    const ref =
      this.props.locationScope === LocationScope.relay
        ? this.exitLocationList
        : this.bridgeLocationList;
    const locationList = ref.current;

    if (locationList) {
      const cell = locationList.selectedCell.current;
      const scrollView = this.scrollView.current;

      if (scrollView) {
        if (cell) {
          const cellDOMNode = ReactDOM.findDOMNode(cell as Element);
          if (cellDOMNode instanceof HTMLElement) {
            scrollView.scrollToElement(cellDOMNode, 'middle');
          }
        } else {
          scrollView.scrollToTop();
        }
      }
    }
  }
}

interface IClosestToExitCellProps {
  isSelected: boolean;
  onSelect: () => void;
}

function ClosestToExitCell(props: IClosestToExitCellProps) {
  return (
    <Cell.CellButton
      style={props.isSelected ? styles.selectedCell : undefined}
      cellHoverStyle={props.isSelected ? styles.selectedCell : undefined}
      onPress={props.onSelect}>
      <Cell.Icon
        source={props.isSelected ? 'icon-tick' : 'icon-nearest'}
        tintColor={colors.white}
        height={24}
        width={24}
      />
      <Cell.Label>{messages.pgettext('select-location-view', 'Closest to exit server')}</Cell.Label>
    </Cell.CellButton>
  );
}