All files / panel index.js

84.21% Statements 32/38
66.67% Branches 14/21
75% Functions 6/8
84.21% Lines 32/38
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                5x   5x               5x 5x       5x 5x 5x 5x         5x 5x 5x 5x 5x 5x 5x 5x       5x                     2x   2x     2x                               17x 17x   17x 12x   12x 48x 7x       41x                   12x 7x                   17x                           1x          
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import getConfig from '../utils/config';
import generateLink from '../utils/generateLink';
import './styles.css';
 
export default class Panel extends Component {
  constructor(...args) {
    super(...args);
 
    this.state = {
      availableVersions: null,
      currentVersion: '',
      hostname: '',
      localhost: '',
      showLocalhost: (this.props.storybook.getQueryParam('versionsDevMode') === 'true'),
    };
 
    this.devModeChangeHandler = this.devModeChangeHandler.bind(this);
    this.handleVersionClick = this.handleVersionClick.bind(this);
  }
 
  componentWillMount() {
    getConfig().then((data) => {
      const { availableVersions, regex, hostname, localhost } = data;
      Eif (availableVersions) {
        this.setState({
          availableVersions: availableVersions.reverse(),
        });
      }
 
      const url = this.props.location;
      let currentVersion = '';
      const path = url.pathname;
      Eif (path && path !== '/' && regex) {
        const r = new RegExp(regex, 'i');
        const result = r.exec(path);
        Eif (result && result.length > 0) {
          currentVersion = result[1];
        }
      }
 
      this.setState({
        currentVersion,
        hostname,
        localhost,
      });
    }).catch(() => {
      // Ignore, we're not showing anything anyway.
    });
  }
 
  devModeChangeHandler() {
    const newVal = !this.state.showLocalhost;
 
    this.setState({
      showLocalhost: newVal,
    });
    this.props.storybook.setQueryParams({
      versionsDevMode: newVal,
    });
  }
 
  handleVersionClick(e) {
    // We need to handle clicks dynamically so we get all the correct query strings
    const { currentVersion, hostname, localhost } = this.state;
    const location = this.props.location;
    const version = e.target.value;
    const targetHost = version ? (hostname || `${location.hostname}:${location.port}`) : localhost;
    const target = generateLink(location, currentVersion, version, targetHost);
    window.parent.location = target;
  }
 
  render() {
    const { availableVersions, currentVersion, showLocalhost } = this.state;
    let versionsList = <p>No versions found</p>;
 
    if (availableVersions) {
      let keyCounter = 0;
 
      versionsList = availableVersions.map((version) => {
        if (currentVersion === version) {
          return (
            <span className="dark-bg with-border" key={keyCounter++}>{version}</span>
          );
        }
        return (
          <button
            key={keyCounter++}
            onClick={this.handleVersionClick}
            className="light-bg with-border"
            value={version}
          >{version}</button>
        );
      });
 
      if (showLocalhost) {
        versionsList.unshift(
          <button
            key={keyCounter++}
            onClick={this.handleVersionClick}
            className="light-bg with-border"
          >local dev</button>,
        );
      }
    }
 
    return (
      <div className="versions-panel-container">
        <label htmlFor="versionsAddonDevMode"><input
          type="checkbox"
          id="versionsAddonDevMode"
          checked={showLocalhost}
          onChange={this.devModeChangeHandler}
        /> Developer mode</label>
        <div className="versions-panel-list">{versionsList}</div>
      </div>
    );
  }
}
 
Panel.propTypes = {
  // channel: PropTypes.object.isRequired,
  storybook: PropTypes.object.isRequired,
  location: PropTypes.object.isRequired,
};