lbry-desktop/ui/component/errorBoundary/view.jsx

118 lines
3.2 KiB
React
Raw Normal View History

// @flow
2019-08-13 07:35:13 +02:00
import type { Node } from 'react';
2020-01-03 20:45:34 +01:00
import React from 'react';
import Yrbl from 'component/yrbl';
import Button from 'component/button';
import { withRouter } from 'react-router';
import analytics from 'analytics';
2020-01-03 20:45:34 +01:00
import I18nMessage from 'component/i18nMessage';
2020-02-05 15:45:20 +01:00
import Native from 'native';
import { Lbry } from 'lbry-redux';
type Props = {
2019-08-13 07:35:13 +02:00
children: Node,
history: {
replace: string => void,
},
};
type State = {
hasError: boolean,
2020-02-05 04:46:00 +01:00
eventId: ?string,
};
class ErrorBoundary extends React.Component<Props, State> {
constructor() {
super();
2020-02-05 04:46:00 +01:00
this.state = { hasError: false, eventId: undefined };
(this: any).refresh = this.refresh.bind(this);
}
static getDerivedStateFromError() {
return { hasError: true };
}
2020-02-05 04:46:00 +01:00
componentDidCatch(error, errorInfo) {
2020-02-05 15:45:20 +01:00
// analytics.error(error, errorInfo).then(eventId => {
// this.setState({ eventId });
// });
let errorMessage = 'Uncaught error\n';
// @if TARGET='web'
errorMessage += 'lbry.tv\n';
errorMessage += `page: ${window.location.pathname + window.location.search}\n`;
errorMessage += error.stack;
analytics.error(errorMessage);
// @endif
// @if TARGET='app'
Native.getAppVersionInfo().then(({ localVersion }) => {
Lbry.version().then(({ lbrynet_version: sdkVersion }) => {
errorMessage += `app version: ${localVersion}\n`;
errorMessage += `sdk version: ${sdkVersion}\n`;
errorMessage += `page: ${window.location.href.split('.html')[1]}\n`;
errorMessage += `${error.stack}`;
analytics.error(errorMessage);
});
2019-05-20 18:02:22 +02:00
});
2020-02-05 15:45:20 +01:00
// @endif
2019-05-20 18:02:22 +02:00
}
refresh() {
const { history } = this.props;
2020-02-05 04:46:00 +01:00
// use history.replace instead of history.push so the user can't click back to the errored page
history.replace('');
this.setState({ hasError: false });
}
render() {
2020-02-05 15:45:20 +01:00
const { hasError } = this.state;
2020-02-05 04:46:00 +01:00
if (hasError) {
return (
<div className="main main--empty">
<Yrbl
type="sad"
title={__('Aw shucks!')}
subtitle={
2020-01-03 20:45:34 +01:00
<I18nMessage
tokens={{
refreshing_the_app_link: (
<Button
button="link"
className="load-screen__button"
label={__('refreshing the app')}
onClick={this.refresh}
/>
),
}}
>
2020-02-05 04:46:00 +01:00
There was an error. Try %refreshing_the_app_link% to fix it. If that doesn't work, try pressing
Ctrl+R/Cmd+R.
2020-01-03 20:45:34 +01:00
</I18nMessage>
}
/>
2020-02-05 15:45:20 +01:00
{/* {eventId === null && (
2020-02-05 04:46:00 +01:00
<div className="error-wrapper">
<span className="error-text">
{__('You are not currently sharing diagnostic data so this error was not reported.')}
</span>
</div>
)}
{eventId && (
<div className="error-wrapper">
<span className="error-text">{__('Error ID: %eventId%', { eventId })}</span>
</div>
2020-02-05 15:45:20 +01:00
)} */}
</div>
);
}
return this.props.children;
}
}
export default withRouter(ErrorBoundary);