# All files / src/stores store.js

82.86% Statements 29/35
65% Branches 13/20
85.71% Functions 6/7
85.29% Lines 29/34

    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

    1x

    1x

    1x

    1x

    1x

    1x

    2x

    1x

    1x

    2x
    2x
    2x
    2x
    2x

    2x

    2x
    1x

    1x

     import Reflux from 'reflux';
    import StateMixin from 'reflux-state-mixin';

    /**
     * Host string max length.
     */
    const HOST_STRING_LENGTH = 25;

    /**
     * Ssh Tunnel Status store.
     */
    const SshTunnelStatusStore = Reflux.createStore({
      /**
       * adds a state to the store, similar to React.Component's state
       * @see https://github.com/yonatanmn/Super-Simple-Flux#reflux-state-mixin
       *
       * If you call `this.setState({...})` this will cause the store to trigger
       * and push down its state as props to connected components.
       */
      mixins: [StateMixin.store],

      /**
       * On activatetd listen to the connection.
       *
       * @param {AppRegistry} appRegistry - The app registry.
       */
      onActivated(appRegistry) {
        appRegistry.on('data-service-connected', this.onConnected.bind(this));
      },

      /**
       * when connected to a deployment, checks if the connection is via an ssh
       * tunnel, and if so, extracts hostname and port from the connection model
       * and sets the new state.
       */
      onConnected(err, ds) {
        if (err) return;
        const sshTunnel = ds.client.model.ssh_tunnel !== 'NONE';
        const sshTunnelHostname = sshTunnel ? ds.client.model.ssh_tunnel_hostname : '';
        const sshTunnelPort = sshTunnel ? ds.client.model.ssh_tunnel_options.dstPort : '';
        const sshTunnelHostPortString = sshTunnel ? this._combineHostPort(
          sshTunnelHostname, sshTunnelPort, true) : '';

        this.setState({
          sshTunnel,
          sshTunnelHostname,
          sshTunnelPort,
          sshTunnelHostPortString
        });
      },

      /*I*
       * returns the combined host:port string, possibly truncated in the middle
       * of the host.
       * @param  {String} host       The hostname string
       * @param  {String} port       The port string
       * @param  {Boolean} truncate  Whether the string needs to be truncated
       *
       * @return {String}            The resulting host:port string
       */
      _combineHostPort(host, port, truncate) {
        if (host.length >= HOST_STRING_LENGTH && truncate) {
          return host.slice(0, 9) + '...' + host.slice(-9) + ':' + port;
        }
        return host + ':' + port;
      },

      /**
       * Initialize the Server Version store state.
       *
       * @return {Object} initial store state.
       */
      getInitialState() {
        return {
          sshTunnel: false,
          sshTunnelHostname: '',
          sshTunnelPort: '',
          sshTunnelHostPortString: ''
        };
      }
    });

    export default SshTunnelStatusStore;
    export { SshTunnelStatusStore };