RuntimeErrorContainer.js 2.36 KB
Newer Older
1
2
3
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
Sophie Alpert's avatar
Sophie Alpert committed
4
5
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
6
7
8
9
 */

/* @flow */
import React, { PureComponent } from 'react';
10
import ErrorOverlay from '../components/ErrorOverlay';
11
12
13
14
15
import CloseButton from '../components/CloseButton';
import NavigationBar from '../components/NavigationBar';
import RuntimeError from './RuntimeError';
import Footer from '../components/Footer';

16
import type { ErrorRecord } from './RuntimeError';
17
import type { ErrorLocation } from '../utils/parseCompileError';
18
19
20
21

type Props = {|
  errorRecords: ErrorRecord[],
  close: () => void,
22
  editorHandler: (errorLoc: ErrorLocation) => void,
23
24
25
26
27
28
29
|};

type State = {|
  currentIndex: number,
|};

class RuntimeErrorContainer extends PureComponent<Props, State> {
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
  state = {
    currentIndex: 0,
  };

  previous = () => {
    this.setState((state, props) => ({
      currentIndex:
        state.currentIndex > 0
          ? state.currentIndex - 1
          : props.errorRecords.length - 1,
    }));
  };

  next = () => {
    this.setState((state, props) => ({
      currentIndex:
        state.currentIndex < props.errorRecords.length - 1
          ? state.currentIndex + 1
          : 0,
    }));
  };

  shortcutHandler = (key: string) => {
    if (key === 'Escape') {
      this.props.close();
    } else if (key === 'ArrowLeft') {
      this.previous();
    } else if (key === 'ArrowRight') {
      this.next();
    }
  };

  render() {
    const { errorRecords, close } = this.props;
    const totalErrors = errorRecords.length;
    return (
66
      <ErrorOverlay shortcutHandler={this.shortcutHandler}>
67
        <CloseButton close={close} />
68
        {totalErrors > 1 && (
69
70
71
72
73
          <NavigationBar
            currentError={this.state.currentIndex + 1}
            totalErrors={totalErrors}
            previous={this.previous}
            next={this.next}
74
75
          />
        )}
76
77
        <RuntimeError
          errorRecord={errorRecords[this.state.currentIndex]}
78
          editorHandler={this.props.editorHandler}
79
80
81
82
83
        />
        <Footer
          line1="This screen is visible only in development. It will not appear if the app crashes in production."
          line2="Open your browser’s developer console to further inspect this error."
        />
84
      </ErrorOverlay>
85
86
87
88
89
    );
  }
}

export default RuntimeErrorContainer;