2019-03-15 05:35:45 +01:00
import React from 'react' ;
import PropTypes from 'prop-types' ;
2019-10-01 20:48:49 +02:00
import { FormattedMessage } from 'react-intl' ;
import { version , source _url } from 'mastodon/initial_state' ;
2019-03-15 05:35:45 +01:00
export default class ErrorBoundary extends React . PureComponent {
static propTypes = {
children : PropTypes . node ,
} ;
state = {
hasError : false ,
stackTrace : undefined ,
componentStack : undefined ,
2019-10-01 20:48:49 +02:00
} ;
2019-03-15 05:35:45 +01:00
2019-10-01 20:48:49 +02:00
componentDidCatch ( error , info ) {
2019-03-15 05:35:45 +01:00
this . setState ( {
hasError : true ,
stackTrace : error . stack ,
componentStack : info && info . componentStack ,
2019-10-01 20:48:49 +02:00
copied : false ,
2019-03-15 05:35:45 +01:00
} ) ;
}
2019-10-01 20:48:49 +02:00
handleCopyStackTrace = ( ) => {
const { stackTrace } = this . state ;
const textarea = document . createElement ( 'textarea' ) ;
textarea . textContent = stackTrace ;
textarea . style . position = 'fixed' ;
document . body . appendChild ( textarea ) ;
try {
textarea . select ( ) ;
document . execCommand ( 'copy' ) ;
} catch ( e ) {
} finally {
document . body . removeChild ( textarea ) ;
}
this . setState ( { copied : true } ) ;
setTimeout ( ( ) => this . setState ( { copied : false } ) , 700 ) ;
}
2019-03-15 05:35:45 +01:00
render ( ) {
2019-10-01 20:48:49 +02:00
const { hasError , copied } = this . state ;
2019-03-15 05:35:45 +01:00
if ( ! hasError ) {
return this . props . children ;
}
return (
2019-10-01 20:48:49 +02:00
< div className = 'error-boundary' >
< div >
< p className = 'error-boundary__error' > < FormattedMessage id = 'error.unexpected_crash.explanation' defaultMessage = 'Due to a bug in our code or a browser compatibility issue, this page could not be displayed correctly.' / > < / p >
< p > < FormattedMessage id = 'error.unexpected_crash.next_steps' defaultMessage = 'Try refreshing the page. If that does not help, you may still be able to use Mastodon through a different browser or native app.' / > < / p >
2020-01-23 22:00:13 +01:00
< p className = 'error-boundary__footer' > Mastodon v { version } · < a href = { source _url } rel = 'noopener noreferrer' target = '_blank' > < FormattedMessage id = 'errors.unexpected_crash.report_issue' defaultMessage = 'Report issue' / > < /a> · <button onClick={this.handleCopyStackTrace} className={copied ? 'copied' : ''}><FormattedMessage id='errors.unexpected_crash.copy_stacktrace' defaultMessage='Copy stacktrace to clipboard' / > < / b u t t o n > < / p >
2019-10-01 20:48:49 +02:00
< / d i v >
2019-03-15 05:35:45 +01:00
< / d i v >
) ;
}
}