2019-10-02 18:26:23 +03:00
|
|
|
/*
|
2021-06-22 22:41:26 +03:00
|
|
|
Copyright 2019 - 2021 The Matrix.org Foundation C.I.C.
|
2019-10-02 18:26:23 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-22 22:41:26 +03:00
|
|
|
import React, { ErrorInfo } from 'react';
|
|
|
|
|
2019-10-02 18:26:23 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2021-06-22 22:41:26 +03:00
|
|
|
import { MatrixClientPeg } from '../../../MatrixClientPeg';
|
2019-10-02 18:26:23 +03:00
|
|
|
import PlatformPeg from '../../../PlatformPeg';
|
|
|
|
import Modal from '../../../Modal';
|
2020-09-16 12:50:53 +03:00
|
|
|
import SdkConfig from "../../../SdkConfig";
|
2021-06-22 22:41:26 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
|
|
|
import BugReportDialog from '../dialogs/BugReportDialog';
|
|
|
|
import AccessibleButton from './AccessibleButton';
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
error: Error;
|
|
|
|
}
|
2019-10-02 18:26:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This error boundary component can be used to wrap large content areas and
|
|
|
|
* catch exceptions during rendering in the component tree below them.
|
|
|
|
*/
|
2021-03-09 05:59:41 +03:00
|
|
|
@replaceableComponent("views.elements.ErrorBoundary")
|
2021-06-22 22:41:26 +03:00
|
|
|
export default class ErrorBoundary extends React.PureComponent<{}, IState> {
|
2019-10-02 18:26:23 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
error: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-22 22:41:26 +03:00
|
|
|
static getDerivedStateFromError(error: Error): Partial<IState> {
|
2019-10-02 18:26:23 +03:00
|
|
|
// Side effects are not permitted here, so we only update the state so
|
|
|
|
// that the next render shows an error message.
|
|
|
|
return { error };
|
|
|
|
}
|
|
|
|
|
2021-06-22 22:41:26 +03:00
|
|
|
componentDidCatch(error: Error, { componentStack }: ErrorInfo): void {
|
2019-10-02 18:26:23 +03:00
|
|
|
// Browser consoles are better at formatting output when native errors are passed
|
|
|
|
// in their own `console.error` invocation.
|
|
|
|
console.error(error);
|
|
|
|
console.error(
|
|
|
|
"The above error occured while React was rendering the following components:",
|
|
|
|
componentStack,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-22 22:41:26 +03:00
|
|
|
private onClearCacheAndReload = (): void => {
|
2019-10-02 18:26:23 +03:00
|
|
|
if (!PlatformPeg.get()) return;
|
|
|
|
|
|
|
|
MatrixClientPeg.get().stopClient();
|
2019-11-18 13:03:05 +03:00
|
|
|
MatrixClientPeg.get().store.deleteAllData().then(() => {
|
2019-10-02 18:26:23 +03:00
|
|
|
PlatformPeg.get().reload();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-22 22:41:26 +03:00
|
|
|
private onBugReport = (): void => {
|
2019-10-09 13:59:10 +03:00
|
|
|
Modal.createTrackedDialog('Bug Report Dialog', '', BugReportDialog, {
|
|
|
|
label: 'react-soft-crash',
|
|
|
|
});
|
2019-10-02 18:26:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.error) {
|
2020-08-03 18:02:26 +03:00
|
|
|
const newIssueUrl = "https://github.com/vector-im/element-web/issues/new";
|
2020-09-16 12:50:53 +03:00
|
|
|
|
|
|
|
let bugReportSection;
|
|
|
|
if (SdkConfig.get().bug_report_endpoint_url) {
|
|
|
|
bugReportSection = <React.Fragment>
|
2021-07-20 00:43:11 +03:00
|
|
|
<p>{ _t(
|
2019-10-02 18:26:23 +03:00
|
|
|
"Please <newIssueLink>create a new issue</newIssueLink> " +
|
|
|
|
"on GitHub so that we can investigate this bug.", {}, {
|
|
|
|
newIssueLink: (sub) => {
|
|
|
|
return <a target="_blank" rel="noreferrer noopener" href={newIssueUrl}>{ sub }</a>;
|
|
|
|
},
|
|
|
|
},
|
2021-07-20 00:43:11 +03:00
|
|
|
) }</p>
|
|
|
|
<p>{ _t(
|
2019-10-02 18:26:23 +03:00
|
|
|
"If you've submitted a bug via GitHub, debug logs can help " +
|
|
|
|
"us track down the problem. Debug logs contain application " +
|
|
|
|
"usage data including your username, the IDs or aliases of " +
|
|
|
|
"the rooms or groups you have visited and the usernames of " +
|
|
|
|
"other users. They do not contain messages.",
|
2021-07-20 00:43:11 +03:00
|
|
|
) }</p>
|
2021-06-22 22:41:26 +03:00
|
|
|
<AccessibleButton onClick={this.onBugReport} kind='primary'>
|
2021-07-20 00:43:11 +03:00
|
|
|
{ _t("Submit debug logs") }
|
2019-10-02 18:26:23 +03:00
|
|
|
</AccessibleButton>
|
2020-09-16 12:50:53 +03:00
|
|
|
</React.Fragment>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <div className="mx_ErrorBoundary">
|
|
|
|
<div className="mx_ErrorBoundary_body">
|
2021-07-20 00:43:11 +03:00
|
|
|
<h1>{ _t("Something went wrong!") }</h1>
|
2020-09-16 12:50:53 +03:00
|
|
|
{ bugReportSection }
|
2021-06-22 22:41:26 +03:00
|
|
|
<AccessibleButton onClick={this.onClearCacheAndReload} kind='danger'>
|
2021-07-20 00:43:11 +03:00
|
|
|
{ _t("Clear cache and reload") }
|
2019-10-02 18:26:23 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
}
|
|
|
|
}
|