2020-03-27 13:59:38 -07:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
import { FormattedMessage } from 'react-intl' ;
export default class ErrorBoundary extends React . PureComponent {
static propTypes = {
children : PropTypes . node ,
} ;
state = {
hasError : false ,
componentStack : undefined ,
}
componentDidCatch ( error , info ) {
this . setState ( {
hasError : true ,
2021-07-05 16:08:04 -07:00
error ,
2020-03-27 13:59:38 -07:00
componentStack : info && info . componentStack ,
} ) ;
}
2021-07-05 16:08:04 -07:00
setTextareaRef = c => {
this . textarea = c ;
}
handleCopy = e => {
if ( ! this . textarea ) return ;
this . textarea . select ( ) ;
this . textarea . setSelectionRange ( 0 , 99999 ) ;
document . execCommand ( 'copy' ) ;
}
getErrorText = ( ) => {
const { error , componentStack } = this . state ;
return error + componentStack ;
}
2021-04-22 11:57:47 -07:00
clearCookies = e => {
localStorage . clear ( ) ;
sessionStorage . clear ( ) ;
}
2020-03-27 13:59:38 -07:00
render ( ) {
const { hasError } = this . state ;
if ( ! hasError ) {
return this . props . children ;
}
2021-07-05 16:08:04 -07:00
const errorText = this . getErrorText ( ) ;
2020-03-27 13:59:38 -07:00
return (
< div className = 'error-boundary' >
< div >
2021-04-22 11:57:47 -07:00
< i className = 'fa fa-frown-o' aria - hidden = 'true' / >
2020-03-27 13:59:38 -07:00
< FormattedMessage id = 'alert.unexpected.message' defaultMessage = 'An unexpected error occurred.' / >
2021-04-22 11:57:47 -07:00
< a href = '/' className = 'return-home' >
< i className = 'fa fa-reply' aria - hidden = 'true' / > & nbsp ;
< FormattedMessage id = 'alert.unexpected.return_home' defaultMessage = 'Return Home' / >
< / a >
2021-07-05 16:08:04 -07:00
{ errorText && < textarea
ref = { this . setTextareaRef }
className = 'error-boundary__component-stack'
value = { errorText }
onClick = { this . handleCopy }
readOnly
/ > }
2021-04-22 11:57:47 -07:00
< p className = 'help-text' >
< FormattedMessage
id = 'alert.unexpected.help_text'
defaultMessage = 'If the problem persists, please notify a site admin with a screenshot and information about your web browser. You may also {clear_cookies} (this will log you out).'
values = { { clear _cookies : (
< a href = '/' onClick = { this . clearCookies } >
< FormattedMessage
id = 'alert.unexpected.clear_cookies'
defaultMessage = 'clear cookies and browser data'
/ >
< / a >
) } }
/ >
< / p >
2020-03-27 13:59:38 -07:00
< / d i v >
< / d i v >
) ;
}
}