From 20cf1078fc19a7b964368e66005e23d441954e83 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Tue, 21 Jan 2020 15:50:25 +0100 Subject: [PATCH] add top error boundary & reset localStorage on error (#493) * add top error boundary & reset localStorage on error * add issue tracker details and link * add pointer cursor to buttons * Update src/bug-issue-template.js Co-Authored-By: Lipis * Update src/styles.scss Co-Authored-By: Lipis * Update src/bug-issue-template.js Co-Authored-By: Lipis * use open-color colors * use Cascadia font Co-authored-by: Lipis --- src/bug-issue-template.js | 13 ++++++ src/index.tsx | 98 ++++++++++++++++++++++++++++++++++++++- src/styles.scss | 50 ++++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/bug-issue-template.js diff --git a/src/bug-issue-template.js b/src/bug-issue-template.js new file mode 100644 index 00000000..667b86d8 --- /dev/null +++ b/src/bug-issue-template.js @@ -0,0 +1,13 @@ +export default ` +### Stack strace + +\`\`\` +// paste stack trace here +\`\`\` + +### localStorage + +\`\`\` +// paste localStorage content here (if it doesn't contain sensitive data) +\`\`\` +`; diff --git a/src/index.tsx b/src/index.tsx index 3431cb31..f8bfd725 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1371,4 +1371,100 @@ export class App extends React.Component { const AppWithTrans = withTranslation()(App); const rootElement = document.getElementById("root"); -ReactDOM.render(, rootElement); + +class TopErrorBoundary extends React.Component { + state = { hasError: false, stack: "", localStorage: "" }; + + static getDerivedStateFromError(error: any) { + console.error(error); + return { + hasError: true, + localStorage: JSON.stringify({ ...localStorage }), + stack: error.stack + }; + } + + private selectTextArea(event: React.MouseEvent) { + (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 {} + + window.open( + `https://github.com/excalidraw/excalidraw/issues/new?body=${body}` + ); + } + + render() { + if (this.state.hasError) { + return ( +
+
+
+ Encountered an error. Please{" "} + + . +
+
+ If reloading doesn't work. Try{" "} + + .
+
+ (This will unfortunately result in loss of work.) +
+
+
+
+ Before doing so, we'd appreciate if you opened an issue on our{" "} + . + Please include the following error stack trace & localStorage + content (provided it's not private): +
+
+
+ +