index.jsx 5.1 KB
Newer Older
J
Jason Park 已提交
1 2 3 4 5 6 7 8 9 10 11 12
import React from 'react';
import { connect } from 'react-redux';
import InputRange from 'react-input-range';
import screenfull from 'screenfull';
import FontAwesomeIcon from '@fortawesome/react-fontawesome';
import faAngleRight from '@fortawesome/fontawesome-free-solid/faAngleRight';
import faCaretDown from '@fortawesome/fontawesome-free-solid/faCaretDown';
import faCaretRight from '@fortawesome/fontawesome-free-solid/faCaretRight';
import faPlay from '@fortawesome/fontawesome-free-solid/faPlay';
import faChevronLeft from '@fortawesome/fontawesome-free-solid/faChevronLeft';
import faPause from '@fortawesome/fontawesome-free-solid/faPause';
import faExpandArrowsAlt from '@fortawesome/fontawesome-free-solid/faExpandArrowsAlt';
J
Jason Park 已提交
13 14
import faGithub from '@fortawesome/fontawesome-free-brands/faGithub';
import { GitHubApi } from '/apis';
J
Jason Park 已提交
15
import { classes } from '/common/util';
J
Jason Park 已提交
16
import { Button, Ellipsis, ListItem } from '/components';
J
Jason Park 已提交
17 18 19 20
import { tracerManager } from '/core';
import styles from './stylesheet.scss';

@connect(
21 22 23
  ({ directory, env }) => ({
    directory, env,
  }), {},
J
Jason Park 已提交
24 25 26 27 28 29
)
class Header extends React.Component {
  constructor(props) {
    super(props);

    const { interval, paused, started } = tracerManager;
J
Jason Park 已提交
30 31 32 33 34 35 36
    this.state = {
      interval, paused, started,
      profile: {
        avatar_url: null,
        login: null,
      },
    };
J
Jason Park 已提交
37 38 39
  }

  componentDidMount() {
J
Jason Park 已提交
40 41 42 43 44 45 46 47 48 49
    const { signedIn } = this.props.env;
    if (signedIn) {
      GitHubApi.getProfile()
        .then(result => {
          const { avatar_url, login } = result;
          const profile = { avatar_url, login };
          this.setState({ profile });
        });
    }

J
Jason Park 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    tracerManager.setOnUpdateStatus(update => this.setState(update));
  }

  componentWillUnmount() {
    tracerManager.setOnUpdateStatus(null);
  }

  handleClickFullScreen() {
    if (screenfull.enabled) {
      if (screenfull.isFullscreen) {
        screenfull.exit();
      } else {
        screenfull.request();
      }
    }
  }

  render() {
J
Jason Park 已提交
68
    const { interval, paused, started, profile } = this.state;
J
Jason Park 已提交
69
    const { className, onClickTitleBar, navigatorOpened } = this.props;
70 71
    const { current } = this.props.directory;
    const { signedIn } = this.props.env;
J
Jason Park 已提交
72 73 74 75

    return (
      <header className={classes(styles.header, className)}>
        <Button className={styles.title_bar} onClick={onClickTitleBar}>
76
          {
77
            current.titles.map((path, i) => [
78
              <Ellipsis key={`path-${i}`}>{path}</Ellipsis>,
79
              i < current.titles.length - 1 &&
80
              <FontAwesomeIcon className={styles.nav_arrow} fixedWidth icon={faAngleRight} key={`arrow-${i}`} />,
81 82
            ])
          }
J
Jason Park 已提交
83 84 85 86 87 88
          <FontAwesomeIcon className={styles.nav_caret} fixedWidth
                           icon={navigatorOpened ? faCaretDown : faCaretRight} />
        </Button>
        <div className={styles.top_menu_buttons}>
          {
            started ? (
89
              <Button icon={faPlay} primary onClick={() => tracerManager.run()} active>Rerun</Button>
J
Jason Park 已提交
90
            ) : (
91
              <Button icon={faPlay} primary onClick={() => tracerManager.run()}>Run</Button>
J
Jason Park 已提交
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
            )
          }
          <Button icon={faChevronLeft} primary disabled={!started}
                  onClick={() => tracerManager.prev()}>Prev</Button>
          {
            paused ? (
              <Button icon={faPause} primary onClick={() => tracerManager.resume()} active>Resume</Button>
            ) : (
              <Button icon={faPause} primary disabled={!started}
                      onClick={() => tracerManager.pause()}>Pause</Button>
            )
          }
          <Button icon={faCaretRight} reverse primary disabled={!started}
                  onClick={() => tracerManager.next()}>Next</Button>
          <div className={styles.interval}>
            Speed
            <InputRange
              classNames={{
                inputRange: styles.range,
                labelContainer: styles.range_label_container,
                slider: styles.range_slider,
                track: styles.range_track,
              }}
              maxValue={2000}
              minValue={100}
              step={100}
              value={interval}
              onChange={interval => tracerManager.setInterval(interval)} />
          </div>
J
Jason Park 已提交
121 122
          {
            signedIn ?
123
              <Button className={styles.btn_profile} icon={profile.avatar_url}>
J
Jason Park 已提交
124 125 126 127 128 129 130 131 132 133 134
                {profile.login}
                <div className={styles.dropdown}>
                  <Button className={styles.fake} icon={profile.avatar_url}>
                    {profile.login}
                  </Button>
                  <div className={styles.list}>
                    <ListItem href="/api/auth/destroy" label="Sign Out" />
                  </div>
                </div>
              </Button> :
              <Button icon={faGithub} primary href="/api/auth/request">
135
                <Ellipsis>Sign In</Ellipsis>
J
Jason Park 已提交
136 137 138
              </Button>
          }
          <Button icon={faExpandArrowsAlt} primary
J
Jason Park 已提交
139 140 141 142 143 144 145 146 147
                  onClick={() => this.handleClickFullScreen()}>Fullscreen</Button>
        </div>
      </header>
    );
  }
}

export default Header;