import assert from 'assert'; import React, { Component, PropTypes } from 'react'; import { If, Then, Else } from 'react-if'; import ReactMapboxGl, { Marker } from 'react-mapbox-gl'; import cheapRuler from 'cheap-ruler'; import { Layout, Container, Header } from './Layout'; import { mapbox as mapboxConfig } from '../config'; import { ConnectionState } from '../enums'; export default class Connect extends Component { static propTypes = { settings: PropTypes.object.isRequired, onSettings: PropTypes.func.isRequired, onConnect: PropTypes.func.isRequired, onDisconnect: PropTypes.func.isRequired, getServerInfo: PropTypes.func.isRequired }; constructor() { super(); this.state = { isFirstPass: true }; } // Component Lifecycle componentDidMount() { this.setState({ isFirstPass: false }); } componentWillUnmount() { this.setState({ isFirstPass: true }); } render() { const preferredServer = this.props.settings.preferredServer; const serverInfo = this.props.getServerInfo(preferredServer); const isConnecting = this.props.connect.status === ConnectionState.connecting; const isConnected = this.props.connect.status === ConnectionState.connected; const isDisconnected = this.props.connect.status === ConnectionState.disconnected; const altitude = (isConnecting ? 300 : 100) * 1000; const displayLocation = this.displayLocation(); const bounds = this.getBounds(displayLocation.location, altitude); const userLocation = this.toLngLat(this.props.user.location); const serverLocation = this.toLngLat(serverInfo.location); const mapBounds = this.toLngLatBounds(bounds); const mapBoundsOptions = { offset: [0, -113], animate: !this.state.isFirstPass }; return (
{ /* show spinner when connecting */ }
{ this.networkSecurityMessage() }
{ 'Fastest' } { 'Nearest' } { /* silly but react-if does not have ElseIf */ } { displayLocation.country }

{ displayLocation.city }
{ displayLocation.country }
{ this.props.connect.clientIp }
{ /* footer when disconnected */ }
Connect to
{ serverInfo.name }
{ /* footer when connecting */ }
{ /* footer when connected */ }
); } // Handlers onConnect() { const server = this.props.settings.preferredServer; const serverInfo = this.props.getServerInfo(server); this.props.onConnect(serverInfo.address); } // Private headerStyle() { const S = Header.Style; switch(this.props.connect.status) { case ConnectionState.disconnected: return S.error; case ConnectionState.connected: return S.success; default: return S.default; } } networkSecurityClass() { let classes = ['connect__status-security']; if(this.props.connect.status === ConnectionState.connected) { classes.push('connect__status-security--secure'); } else if(this.props.connect.status === ConnectionState.disconnected) { classes.push('connect__status-security--unsecured'); } return classes.join(' '); } networkSecurityMessage() { switch(this.props.connect.status) { case ConnectionState.connected: return 'Secure connection'; case ConnectionState.connecting: return 'Creating secure connection'; default: return 'Unsecured connection'; } } ipAddressClass() { var classes = ['connect__status-ipaddress']; if(this.props.connect.status === ConnectionState.connecting) { classes.push('connect__status-ipaddress--invisible'); } return classes.join(' '); } displayLocation() { if(this.props.connect.status === ConnectionState.disconnected) { const { location, country, city } = this.props.user; return { location, country, city }; } const preferredServer = this.props.settings.preferredServer; return this.props.getServerInfo(preferredServer); } // Geo helpers getBounds(center, altitude) { const ruler = cheapRuler(center[0], 'meters'); return ruler.bufferPoint(center, altitude); } toLngLat(pos) { assert(pos.length === 2); return [ pos[1], pos[0] ]; } toLngLatBounds(bounds) { assert(bounds.length % 2 === 0); let result = []; for(let i = 0; i < bounds.length; i += 2) { result.push(bounds.slice(i, i + 2).reverse()); } return result; } }