forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuntimeErrorContainer.js
89 lines (79 loc) · 2.36 KB
/
RuntimeErrorContainer.js
1
2
3
4
5
6
7
8
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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* @flow */
import React, { PureComponent } from 'react';
import ErrorOverlay from '../components/ErrorOverlay';
import CloseButton from '../components/CloseButton';
import NavigationBar from '../components/NavigationBar';
import RuntimeError from './RuntimeError';
import Footer from '../components/Footer';
import type { ErrorRecord } from './RuntimeError';
import type { ErrorLocation } from '../utils/parseCompileError';
type Props = {|
errorRecords: ErrorRecord[],
close: () => void,
editorHandler: (errorLoc: ErrorLocation) => void,
|};
type State = {|
currentIndex: number,
|};
class RuntimeErrorContainer extends PureComponent<Props, State> {
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 (
<ErrorOverlay shortcutHandler={this.shortcutHandler}>
<CloseButton close={close} />
{totalErrors > 1 && (
<NavigationBar
currentError={this.state.currentIndex + 1}
totalErrors={totalErrors}
previous={this.previous}
next={this.next}
/>
)}
<RuntimeError
errorRecord={errorRecords[this.state.currentIndex]}
editorHandler={this.props.editorHandler}
/>
<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."
/>
</ErrorOverlay>
);
}
}
export default RuntimeErrorContainer;