|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +/* @flow */ |
| 11 | +import React, { PureComponent } from 'react'; |
| 12 | +import CloseButton from './CloseButton'; |
| 13 | +import NavigationBar from './NavigationBar'; |
| 14 | +import ErrorView from './ErrorView'; |
| 15 | +import Footer from './Footer'; |
| 16 | +import { black } from '../styles'; |
| 17 | + |
| 18 | +const overlayStyle = { |
| 19 | + position: 'relative', |
| 20 | + display: 'inline-flex', |
| 21 | + flexDirection: 'column', |
| 22 | + height: '100%', |
| 23 | + width: '1024px', |
| 24 | + maxWidth: '100%', |
| 25 | + overflowX: 'hidden', |
| 26 | + overflowY: 'auto', |
| 27 | + padding: '0.5rem', |
| 28 | + boxSizing: 'border-box', |
| 29 | + textAlign: 'left', |
| 30 | + fontFamily: 'Consolas, Menlo, monospace', |
| 31 | + fontSize: '11px', |
| 32 | + whiteSpace: 'pre-wrap', |
| 33 | + wordBreak: 'break-word', |
| 34 | + lineHeight: 1.5, |
| 35 | + color: black, |
| 36 | +}; |
| 37 | + |
| 38 | +class ErrorOverlay extends PureComponent { |
| 39 | + state = { |
| 40 | + currentIndex: 0, |
| 41 | + }; |
| 42 | + iframeWindow: window = null; |
| 43 | + |
| 44 | + previous = () => { |
| 45 | + this.setState((state, props) => ({ |
| 46 | + currentIndex: state.currentIndex > 0 |
| 47 | + ? state.currentIndex - 1 |
| 48 | + : props.errorRecords.length - 1, |
| 49 | + })); |
| 50 | + }; |
| 51 | + |
| 52 | + next = () => { |
| 53 | + this.setState((state, props) => ({ |
| 54 | + currentIndex: state.currentIndex < props.errorRecords.length - 1 |
| 55 | + ? state.currentIndex + 1 |
| 56 | + : 0, |
| 57 | + })); |
| 58 | + }; |
| 59 | + |
| 60 | + handleSortcuts = (e: KeyboardEvent) => { |
| 61 | + const { key } = e; |
| 62 | + if (key === 'Escape') { |
| 63 | + this.props.close(); |
| 64 | + } else if (key === 'ArrowLeft') { |
| 65 | + this.previous(); |
| 66 | + } else if (key === 'ArrowRight') { |
| 67 | + this.next(); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + getIframeWindow = (element: HTMLDivElement) => { |
| 72 | + if (element) { |
| 73 | + const document = element.ownerDocument; |
| 74 | + this.iframeWindow = document.defaultView; |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + componentDidMount() { |
| 79 | + window.addEventListener('keydown', this.handleSortcuts); |
| 80 | + if (this.iframeWindow) { |
| 81 | + this.iframeWindow.addEventListener('keydown', this.handleSortcuts); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + componentWillUnmount() { |
| 86 | + window.removeEventListener('keydown', this.handleSortcuts); |
| 87 | + if (this.iframeWindow) { |
| 88 | + this.iframeWindow.removeEventListener('keydown', this.handleSortcuts); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + render() { |
| 93 | + const { errorRecords, close } = this.props; |
| 94 | + const totalErrors = errorRecords.length; |
| 95 | + return ( |
| 96 | + <div style={overlayStyle} ref={this.getIframeWindow}> |
| 97 | + <CloseButton close={close} /> |
| 98 | + {totalErrors > 1 && |
| 99 | + <NavigationBar |
| 100 | + currentError={this.state.currentIndex + 1} |
| 101 | + totalErrors={totalErrors} |
| 102 | + previous={this.previous} |
| 103 | + next={this.next} |
| 104 | + />} |
| 105 | + <ErrorView errorRecord={errorRecords[this.state.currentIndex]} /> |
| 106 | + <Footer /> |
| 107 | + </div> |
| 108 | + ); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export default ErrorOverlay; |
0 commit comments