summaryrefslogtreecommitdiffhomepage
path: root/app/components/AdvancedSettings.js
blob: c4f014d210e47d1f17fc97b920200c03e89a597d (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
// @flow

import * as React from 'react';
import { Component, Text, View } from 'reactxp';
import { Button } from './styled';
import { Layout, Container } from './Layout';
import CustomScrollbars from './CustomScrollbars';
import styles from './AdvancedSettingsStyles';
import Img from './Img';

export class AdvancedSettings extends Component {

  props: {
    protocol: string,
    port: string | number,
    onUpdate: (protocol: string, port: string | number) => void,
    onClose: () => void,
  };

  render() {
    let portSelector = null;
    let protocol = this.props.protocol.toUpperCase();

    if (protocol === 'AUTOMATIC') {
      protocol = 'Automatic';
    } else {
      portSelector = this._createPortSelector();
    }

    return <BaseLayout onClose={ this.props.onClose }>

      <Selector
        title={ 'Network protocols' }
        values={ ['Automatic', 'UDP', 'TCP'] }
        value={ protocol }
        onSelect={ protocol => {
          this.props.onUpdate(protocol, 'Automatic');
        }}/>

      <View style={ styles.advanced_settings__cell_spacer }></View>

      { portSelector }

    </BaseLayout>;
  }

  _createPortSelector() {
    const protocol = this.props.protocol.toUpperCase();
    const ports = protocol === 'TCP'
      ? ['Automatic', 80, 443]
      : ['Automatic', 1194, 1195, 1196, 1197, 1300, 1301, 1302];

    return <Selector
      title={ protocol + ' port' }
      values={ ports }
      value={ this.props.port }
      onSelect={ port => {
        this.props.onUpdate(protocol, port);
      }} />;
  }
}

class Selector extends Component {

  props: {
    title: string,
    values: Array<*>,
    value: *,
    onSelect: (*) => void,
  }

  state = { hoveredButtonIndex: -1 };

  handleButtonHover = (value) => {
    this.setState({ hoveredButtonIndex: value });
  }

  render() {
    return <View>
      <View style={ styles.advanced_settings__section_title }>
        { this.props.title }
      </View>

      { this.props.values.map(value => this._renderCell(value)) }
    </View>;
  }

  _renderCell(value) {
    const selected = value === this.props.value;
    if (selected) {
      return this._renderSelectedCell(value);
    } else {
      return this._renderUnselectedCell(value);
    }
  }

  _renderSelectedCell(value) {
    return <Button style={[ styles.advanced_settings__cell, value === this.state.hoveredButtonIndex ? styles.advanced_settings__cell_selected_hover : null ]}
      onPress={ () => this.props.onSelect(value) }
      onHoverStart={() => this.handleButtonHover(value)}
      onHoverEnd={() => this.handleButtonHover(-1)}
      key={ value }>
      <Img style={ styles.advanced_settings__cell_icon }
        source='icon-tick'
        tintColor='currentColor'/>
      <Text style={ styles.advanced_settings__cell_label }>{ value }</Text>
    </Button>;
  }

  _renderUnselectedCell(value) {
    return <Button style={[ styles.advanced_settings__cell_dimmed, value === this.state.hoveredButtonIndex ? styles.advanced_settings__cell_hover : null ]}
      onPress={ () => this.props.onSelect(value) }
      onHoverStart={() => this.handleButtonHover(value)}
      onHoverEnd={() => this.handleButtonHover(-1)}
      key={ value }>
      <View style={ styles.advanced_settings__cell_icon }></View>
      <Text style={ styles.advanced_settings__cell_label }>{ value }</Text>
    </Button>;
  }
}

function BaseLayout(props) {
  return <Layout>
    <Container>
      <View style={ styles.advanced_settings }>
        <Button style={ styles.advanced_settings__close }
          onPress={ props.onClose }
          testName='closeButton'>
          <View style={ styles.advanced_settings__close_content }>
            <Img height={24} width={24} style={ styles.advanced_settings__close_icon } source="icon-back" />
            <Text style={ styles.advanced_settings__close_title }>Settings</Text>
          </View>
        </Button>
        <View style={ styles.advanced_settings__container }>
          <View style={ styles.advanced_settings__header }>
            <Text style={ styles.advanced_settings__title }>Advanced</Text>
          </View>
          <CustomScrollbars style={styles.advanced_settings__scrollview} autoHide={ true }>
            <View style={ styles.advanced_settings__content }>
              { props.children }
            </View>
          </CustomScrollbars>
        </View>
      </View>
    </Container>
  </Layout>;
}