index.jsx 7.8 KB
Newer Older
J
Jason Park 已提交
1 2 3
import React from 'react';
import { connect } from 'react-redux';
import InputRange from 'react-input-range';
J
Jason Park 已提交
4
import AutosizeInput from 'react-input-autosize';
J
Jason Park 已提交
5 6 7 8 9 10 11 12 13
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 已提交
14
import faGithub from '@fortawesome/fontawesome-free-brands/faGithub';
J
Jason Park 已提交
15 16 17
import faTrashAlt from '@fortawesome/fontawesome-free-solid/faTrashAlt';
import faSave from '@fortawesome/fontawesome-free-solid/faSave';
import faShare from '@fortawesome/fontawesome-free-solid/faShare';
J
Jason Park 已提交
18
import faStar from '@fortawesome/fontawesome-free-solid/faStar';
J
Jason Park 已提交
19
import { GitHubApi } from '/apis';
J
Jason Park 已提交
20
import { classes } from '/common/util';
J
Jason Park 已提交
21
import { actions } from '/reducers';
J
Jason Park 已提交
22
import { languages } from '/common/config';
J
Jason Park 已提交
23
import { Button, Ellipsis, ListItem } from '/components';
J
Jason Park 已提交
24 25
import { tracerManager } from '/core';
import styles from './stylesheet.scss';
J
Jason Park 已提交
26 27
import Promise from 'bluebird';
import { withRouter } from 'react-router-dom';
J
Jason Park 已提交
28

J
Jason Park 已提交
29 30
@withRouter
@connect(({ current, env }) => ({ current, env }), actions)
J
Jason Park 已提交
31 32 33 34 35
class Header extends React.Component {
  constructor(props) {
    super(props);

    const { interval, paused, started } = tracerManager;
J
Jason Park 已提交
36 37 38 39 40 41 42
    this.state = {
      interval, paused, started,
      profile: {
        avatar_url: null,
        login: null,
      },
    };
J
Jason Park 已提交
43 44 45
  }

  componentDidMount() {
J
Jason Park 已提交
46 47 48 49 50 51 52 53 54 55
    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 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    tracerManager.setOnUpdateStatus(update => this.setState(update));
  }

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

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

J
Jason Park 已提交
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
  handleChangeTitle(e) {
    const { value } = e.target;
    this.props.renameScratchPaper(value);
  }

  saveGist() {
    const { gistId, titles, files } = this.props.current;
    const gist = {
      description: titles[1],
      files: {
        'algorithm-visualizer': {
          content: 'https://algorithm-visualizer.org/',
        },
      },
    };
    files.forEach(file => {
      gist.files[file.name] = {
        content: file.content,
      };
    });
    const savePromise = gistId === 'new' ? GitHubApi.createGist(gist) : GitHubApi.editGist(gistId, gist);
    savePromise.then(data => this.props.saveScratchPaper(data.id)).then(this.props.loadScratchPapers);
  }

  deleteGist() {
    const { gistId } = this.props.current;
    const deletePromise = gistId === 'new' ? Promise.resolve() : GitHubApi.deleteGist(gistId);
    deletePromise.then(() => this.props.loadAlgorithm({})).then(this.props.loadScratchPapers);
  }

J
Jason Park 已提交
103
  render() {
J
Jason Park 已提交
104
    const { interval, paused, started, profile } = this.state;
J
Jason Park 已提交
105 106
    const { className, onClickTitleBar, navigatorOpened, onAction } = this.props;
    const { gistId, titles, saved } = this.props.current;
J
Jason Park 已提交
107
    const { signedIn, ext } = this.props.env;
J
Jason Park 已提交
108 109 110

    return (
      <header className={classes(styles.header, className)}>
J
Jason Park 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        <div className={styles.row}>
          <div className={styles.section}>
            <Button className={styles.title_bar} onClick={onClickTitleBar}>
              {
                titles.map((title, i) => [
                  gistId && i === 1 ?
                    <AutosizeInput className={styles.input_title} key={`title-${i}`} value={title}
                                   onClick={e => e.stopPropagation()} onChange={e => this.handleChangeTitle(e)} /> :
                    <Ellipsis key={`title-${i}`}>{title}</Ellipsis>,
                  i < titles.length - 1 &&
                  <FontAwesomeIcon className={styles.nav_arrow} fixedWidth icon={faAngleRight} key={`arrow-${i}`} />,
                ])
              }
              <FontAwesomeIcon className={styles.nav_caret} fixedWidth
                               icon={navigatorOpened ? faCaretDown : faCaretRight} />
            </Button>
J
Jason Park 已提交
127
          </div>
J
Jason Park 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
          <div className={styles.section}>
            <Button icon={faSave} primary disabled={!gistId || saved} onClick={() => this.saveGist()}>Save</Button>
            <Button icon={faTrashAlt} primary disabled={!gistId} onClick={() => this.deleteGist()}>Delete</Button>
            <Button icon={faShare} primary disabled={gistId === 'new'} onClick={() => this.shareLink()}>Share</Button>
            <Button icon={faExpandArrowsAlt} primary
                    onClick={() => this.handleClickFullScreen()}>Fullscreen</Button>
          </div>
        </div>
        <div className={styles.row}>
          <div className={styles.section}>
            {
              signedIn ?
                <Button className={styles.btn_dropdown} icon={profile.avatar_url}>
                  {profile.login}
                  <div className={styles.dropdown}>
                    <ListItem href="/api/auth/destroy" label="Sign Out" />
                  </div>
                </Button> :
                <Button icon={faGithub} primary href="/api/auth/request">
                  <Ellipsis>Sign In</Ellipsis>
                </Button>
            }
            <Button className={styles.btn_dropdown} icon={faStar}>
              {languages.find(language => language.ext === ext).name}
              <div className={styles.dropdown}>
J
Jason Park 已提交
153 154 155 156 157 158 159
                {
                  languages.map(language => language.ext === ext ? null : (
                    <ListItem key={language.ext} onClick={() => this.props.setExt(language.ext)}
                              label={language.name} />
                  ))
                }
              </div>
J
Jason Park 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
            </Button>
          </div>
          <div className={styles.section} onClick={onAction}>
            {
              started ? (
                <Button icon={faPlay} primary onClick={() => tracerManager.run()} active>Rerun</Button>
              ) : (
                <Button icon={faPlay} primary onClick={() => tracerManager.run()}>Run</Button>
              )
            }
            <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)} />
J
Jason Park 已提交
196
            </div>
J
Jason Park 已提交
197
          </div>
J
Jason Park 已提交
198 199 200 201 202 203 204 205
        </div>
      </header>
    );
  }
}

export default Header;