index.jsx 3.5 KB
Newer Older
J
Jason Park 已提交
1 2 3 4 5 6 7
import React from 'react';
import { connect } from 'react-redux';
import { loadProgressBar } from 'axios-progress-bar'
import { Divider, EditorSection, Header, Navigator, ToastContainer, ViewerSection } from '/components';
import { actions as toastActions } from '/reducers/toast';
import { actions as envActions } from '/reducers/env';
import { calculatePercentageWidth } from '/common/util';
8
import { DirectoryApi } from '/apis';
J
Jason Park 已提交
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
import { tracerManager } from '/core';
import styles from './stylesheet.scss';
import 'axios-progress-bar/dist/nprogress.css'

loadProgressBar();

@connect(
  ({ toast, env }) => ({
    toast,
    env,
  }), {
    ...toastActions,
    ...envActions,
  }
)
class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      navigatorOpened: true,
      navigatorWidth: '16%',
      viewerSectionWidth: '50%',
    }
  }

  componentDidMount() {
J
Jason Park 已提交
36 37
    this.updateDirectory(this.props.match.params);

38 39
    DirectoryApi.getCategories()
      .then(({ categories }) => {
J
Jason Park 已提交
40
        this.props.setCategories(categories);
J
Jason Park 已提交
41 42 43 44 45
        const { categoryKey, algorithmKey, fileKey } = this.props.env;
        const category = categories.find(category => category.key === categoryKey) || categories[0];
        const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey) || category.algorithms[0];
        const file = algorithm.files.find(file => file.key === fileKey) || algorithm.files[0];
        this.props.history.push(`/${category.key}/${algorithm.key}/${file.key}`);
J
Jason Park 已提交
46
      });
J
Jason Park 已提交
47

J
Jason Park 已提交
48 49 50
    tracerManager.setOnError(error => this.props.showErrorToast(error.message));
  }

J
Jason Park 已提交
51 52 53 54 55 56
  componentWillReceiveProps(nextProps) {
    if (nextProps.match.params !== this.props.match.params) {
      this.updateDirectory(nextProps.match.params);
    }
  }

J
Jason Park 已提交
57 58 59 60
  componentWillUnmount() {
    tracerManager.setOnError(null);
  }

J
Jason Park 已提交
61 62 63 64
  updateDirectory({ categoryKey = null, algorithmKey = null, fileKey = null }) {
    this.props.setDirectory(categoryKey, algorithmKey, fileKey);
  }

J
Jason Park 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  toggleNavigator(navigatorOpened = !this.state.navigatorOpened) {
    this.setState({ navigatorOpened });
  }

  handleResizeNavigator(x, y) {
    const navigatorWidth = calculatePercentageWidth(this.elMain, x);
    this.setState({ navigatorWidth });
  }

  handleResizeViewerSection(x, y) {
    const viewerSectionWidth = calculatePercentageWidth(this.elWorkspace, x);
    this.setState({ viewerSectionWidth });
  }

  render() {
    const { navigatorOpened, navigatorWidth, viewerSectionWidth } = this.state;
81
    const { categories, categoryKey, algorithmKey, fileKey } = this.props.env;
J
Jason Park 已提交
82

83
    return categories && categoryKey && algorithmKey && fileKey && (
J
Jason Park 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      <div className={styles.app}>
        <Header onClickTitleBar={() => this.toggleNavigator()} navigatorOpened={navigatorOpened} />
        <main className={styles.main} ref={ref => this.elMain = ref}>
          {
            navigatorOpened &&
            <Navigator className={styles.navigator} style={{ width: navigatorWidth }} />
          }
          <Divider vertical onResize={(x, y) => this.handleResizeNavigator(x, y)} />
          <div className={styles.workspace} ref={ref => this.elWorkspace = ref}>
            <ViewerSection className={styles.viewer_section} style={{ width: viewerSectionWidth }} />
            <Divider vertical onResize={(x, y) => this.handleResizeViewerSection(x, y)} />
            <EditorSection className={styles.editor_section} />
          </div>
        </main>
        <ToastContainer className={styles.toast_container} />
      </div>
    );
  }
}

export default App;