RuntimeErrorContainer.js 2.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

/* @flow */
import React, { PureComponent } from 'react';
12
import ErrorOverlay from '../components/ErrorOverlay';
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
import CloseButton from '../components/CloseButton';
import NavigationBar from '../components/NavigationBar';
import RuntimeError from './RuntimeError';
import Footer from '../components/Footer';

class RuntimeErrorContainer extends PureComponent {
  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 (
55
      <ErrorOverlay shortcutHandler={this.shortcutHandler}>
56
        <CloseButton close={close} />
57
        {totalErrors > 1 && (
58
59
60
61
62
          <NavigationBar
            currentError={this.state.currentIndex + 1}
            totalErrors={totalErrors}
            previous={this.previous}
            next={this.next}
63
64
          />
        )}
65
66
67
68
69
70
71
72
        <RuntimeError
          errorRecord={errorRecords[this.state.currentIndex]}
          launchEditorEndpoint={this.props.launchEditorEndpoint}
        />
        <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."
        />
73
      </ErrorOverlay>
74
75
76
77
78
    );
  }
}

export default RuntimeErrorContainer;