import React, { Component } from "react";
import PropTypes from "prop-types";
import * as Sentry from "@sentry/browser";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faExclamationCircle } from "@fortawesome/free-solid-svg-icons/faExclamationCircle";
const InternalError = ({ message, secondsLeft }) => (
Internal error
{message}
This page will auto refresh in {secondsLeft}s
);
InternalError.propTypes = {
message: PropTypes.node.isRequired,
secondsLeft: PropTypes.number.isRequired
};
class ErrorBoundary extends Component {
static propTypes = {
children: PropTypes.any
};
constructor(props) {
super(props);
this.timer = null;
this.state = { cachedError: null, reloadSeconds: 60 };
}
reloadApp = () => {
if (this.state.reloadSeconds <= 1) {
window.location.reload();
} else {
this.setState({ reloadSeconds: this.state.reloadSeconds - 1 });
}
};
componentDidCatch(error, errorInfo) {
this.setState({ cachedError: error });
Sentry.configureScope(scope => {
Object.keys(errorInfo).forEach(key => {
scope.setExtra(key, errorInfo[key]);
});
});
Sentry.captureException(error);
// reload after 60s, this is to fix wall monitors automatically
// but only if the timer isn't set yet
if (this.timer === null) {
this.timer = setInterval(this.reloadApp, 1000);
}
}
render() {
if (this.state.cachedError !== null) {
return (
);
}
return this.props.children;
}
}
export { ErrorBoundary };