summaryrefslogtreecommitdiffhomepage
path: root/app/containers/ConnectPage.js
blob: 532dfc8626542823e84186adf4373900d7a6d106 (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
// @flow

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { push } from 'react-router-redux';
import { links } from '../config';
import Connect from '../components/Connect';
import connectActions from '../redux/connection/actions';
import { openLink } from '../lib/platform';

import type { ReduxState, ReduxDispatch } from '../redux/store';
import type { SharedRouteProps } from '../routes';

import type { RelaySettingsRedux, RelayLocationRedux } from '../redux/settings/reducers';

function getRelayName(
  relaySettings: RelaySettingsRedux,
  relayLocations: Array<RelayLocationRedux>,
): string {
  if (relaySettings.normal) {
    const location = relaySettings.normal.location;

    if (location === 'any') {
      return 'Automatic';
    } else if (location.country) {
      const country = relayLocations.find(({ code }) => code === location.country);
      if (country) {
        return country.name;
      }
    } else if (location.city) {
      const [countryCode, cityCode] = location.city;
      const country = relayLocations.find(({ code }) => code === countryCode);
      if (country) {
        const city = country.cities.find(({ code }) => code === cityCode);
        if (city) {
          return city.name;
        }
      }
    }

    return 'Unknown';
  } else if (relaySettings.custom_tunnel_endpoint) {
    return 'Custom';
  } else {
    throw new Error('Unsupported relay settings.');
  }
}

const mapStateToProps = (state: ReduxState) => {
  return {
    accountExpiry: state.account.expiry,
    selectedRelayName: getRelayName(state.settings.relaySettings, state.settings.relayLocations),
    connection: state.connection,
  };
};

const mapDispatchToProps = (dispatch: ReduxDispatch, props: SharedRouteProps) => {
  const { connect, disconnect, copyIPAddress } = bindActionCreators(connectActions, dispatch);
  const { push: pushHistory } = bindActionCreators({ push }, dispatch);
  const { backend } = props;

  return {
    onSettings: () => {
      pushHistory('/settings');
    },
    onSelectLocation: () => {
      pushHistory('/select-location');
    },
    onConnect: () => {
      connect(backend);
    },
    onCopyIP: () => {
      copyIPAddress();
    },
    onDisconnect: () => {
      disconnect(backend);
    },
    onExternalLink: (type) => openLink(links[type]),
  };
};

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(Connect);