2020-03-07 10:20:38 -05:00
|
|
|
|
import React from "react";
|
2020-03-09 17:09:45 +01:00
|
|
|
|
import { resetCursor } from "../utils";
|
|
|
|
|
import { t } from "../i18n";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
|
|
interface TopErrorBoundaryState {
|
|
|
|
|
unresolvedError: Error[] | null;
|
|
|
|
|
hasError: boolean;
|
|
|
|
|
stack: string;
|
|
|
|
|
localStorage: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class TopErrorBoundary extends React.Component<
|
|
|
|
|
any,
|
|
|
|
|
TopErrorBoundaryState
|
|
|
|
|
> {
|
|
|
|
|
state: TopErrorBoundaryState = {
|
|
|
|
|
unresolvedError: null,
|
|
|
|
|
hasError: false,
|
|
|
|
|
stack: "",
|
|
|
|
|
localStorage: "",
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-21 02:26:01 -07:00
|
|
|
|
render() {
|
|
|
|
|
return this.state.hasError ? this.errorSplash() : this.props.children;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
|
componentDidCatch(error: Error) {
|
2020-03-09 17:09:45 +01:00
|
|
|
|
resetCursor();
|
2020-03-07 10:20:38 -05:00
|
|
|
|
const _localStorage: any = {};
|
|
|
|
|
for (const [key, value] of Object.entries({ ...localStorage })) {
|
|
|
|
|
try {
|
|
|
|
|
_localStorage[key] = JSON.parse(value);
|
2020-03-09 17:09:45 +01:00
|
|
|
|
} catch (error) {
|
2020-03-07 10:20:38 -05:00
|
|
|
|
_localStorage[key] = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.setState(state => ({
|
|
|
|
|
hasError: true,
|
|
|
|
|
unresolvedError: state.unresolvedError
|
|
|
|
|
? state.unresolvedError.concat(error)
|
|
|
|
|
: [error],
|
|
|
|
|
localStorage: JSON.stringify(_localStorage),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async componentDidUpdate() {
|
|
|
|
|
if (this.state.unresolvedError !== null) {
|
|
|
|
|
let stack = "";
|
|
|
|
|
for (const error of this.state.unresolvedError) {
|
|
|
|
|
if (stack) {
|
|
|
|
|
stack += `\n\n================\n\n`;
|
|
|
|
|
}
|
|
|
|
|
stack += `${error.message}:\n\n`;
|
|
|
|
|
try {
|
|
|
|
|
const StackTrace = await import("stacktrace-js");
|
|
|
|
|
stack += (await StackTrace.fromError(error)).join("\n");
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
stack += error.stack || "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setState(state => ({
|
|
|
|
|
unresolvedError: null,
|
|
|
|
|
stack: `${
|
|
|
|
|
state.stack ? `${state.stack}\n\n================\n\n${stack}` : stack
|
|
|
|
|
}`,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private selectTextArea(event: React.MouseEvent<HTMLTextAreaElement>) {
|
|
|
|
|
if (event.target !== document.activeElement) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
(event.target as HTMLTextAreaElement).select();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async createGithubIssue() {
|
|
|
|
|
let body = "";
|
|
|
|
|
try {
|
|
|
|
|
const templateStr = (await import("../bug-issue-template")).default;
|
|
|
|
|
if (typeof templateStr === "string") {
|
|
|
|
|
body = encodeURIComponent(templateStr);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.open(
|
|
|
|
|
`https://github.com/excalidraw/excalidraw/issues/new?body=${body}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 02:26:01 -07:00
|
|
|
|
private errorSplash() {
|
|
|
|
|
return (
|
|
|
|
|
<div className="ErrorSplash">
|
|
|
|
|
<div className="ErrorSplash-messageContainer">
|
|
|
|
|
<div className="ErrorSplash-paragraph bigger align-center">
|
|
|
|
|
{t("errorSplash.headingMain_pre")}
|
|
|
|
|
<button onClick={() => window.location.reload()}>
|
|
|
|
|
{t("errorSplash.headingMain_button")}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="ErrorSplash-paragraph align-center">
|
|
|
|
|
{t("errorSplash.clearCanvasMessage")}
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
window.location.reload();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t("errorSplash.clearCanvasMessage_button")}
|
|
|
|
|
</button>
|
|
|
|
|
<br />
|
|
|
|
|
<div className="smaller">
|
|
|
|
|
<span role="img" aria-label="warning">
|
|
|
|
|
⚠️
|
|
|
|
|
</span>
|
|
|
|
|
{t("errorSplash.clearCanvasCaveat")}
|
|
|
|
|
<span role="img" aria-hidden="true">
|
|
|
|
|
⚠️
|
|
|
|
|
</span>
|
2020-03-07 10:20:38 -05:00
|
|
|
|
</div>
|
2020-03-21 02:26:01 -07:00
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<div className="ErrorSplash-paragraph">
|
|
|
|
|
{t("errorSplash.openIssueMessage_pre")}
|
|
|
|
|
<button onClick={this.createGithubIssue}>
|
|
|
|
|
{t("errorSplash.openIssueMessage_button")}
|
2020-03-07 10:20:38 -05:00
|
|
|
|
</button>
|
2020-03-21 02:26:01 -07:00
|
|
|
|
{t("errorSplash.openIssueMessage_post")}
|
2020-03-07 10:20:38 -05:00
|
|
|
|
</div>
|
2020-03-21 02:26:01 -07:00
|
|
|
|
<div className="ErrorSplash-paragraph">
|
|
|
|
|
<div className="ErrorSplash-details">
|
|
|
|
|
<label>{t("errorSplash.errorStack")}</label>
|
|
|
|
|
<textarea
|
|
|
|
|
rows={10}
|
|
|
|
|
onPointerDown={this.selectTextArea}
|
|
|
|
|
readOnly={true}
|
|
|
|
|
value={
|
|
|
|
|
this.state.unresolvedError
|
|
|
|
|
? t("errorSplash.errorStack_loading")
|
|
|
|
|
: this.state.stack
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<label>{t("errorSplash.sceneContent")}</label>
|
|
|
|
|
<textarea
|
|
|
|
|
rows={5}
|
|
|
|
|
onPointerDown={this.selectTextArea}
|
|
|
|
|
readOnly={true}
|
|
|
|
|
value={this.state.localStorage}
|
|
|
|
|
/>
|
2020-03-07 10:20:38 -05:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-03-21 02:26:01 -07:00
|
|
|
|
</div>
|
|
|
|
|
);
|
2020-03-07 10:20:38 -05:00
|
|
|
|
}
|
|
|
|
|
}
|