From 99c9678d9f9597c4eb4d4f1cf2b081419f65af19 Mon Sep 17 00:00:00 2001 From: tomaszantas Date: Fri, 8 Jul 2022 14:08:52 +0200 Subject: [PATCH] Add ErrorBoundary --- .../launchpad/gui-react/src/ErrorBoundary.tsx | 299 ++++++++++++++++++ .../launchpad/gui-react/src/index.tsx | 5 +- 2 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 applications/launchpad/gui-react/src/ErrorBoundary.tsx diff --git a/applications/launchpad/gui-react/src/ErrorBoundary.tsx b/applications/launchpad/gui-react/src/ErrorBoundary.tsx new file mode 100644 index 0000000000..64ec131163 --- /dev/null +++ b/applications/launchpad/gui-react/src/ErrorBoundary.tsx @@ -0,0 +1,299 @@ +import { Component, CSSProperties, ErrorInfo, ReactNode } from 'react' +import { appWindow } from '@tauri-apps/api/window' + +import linksConfig from './config/links' +import SvgTariLaunchpadLogo from './styles/Icons/TariLaunchpadLogo' +import SvgTBotBase from './styles/Icons/TBotBase' + +interface ErrorBoundaryProps { + children?: ReactNode +} + +interface ErrorBoundaryState { + error?: string + showDetails: boolean +} + +const styles = { + windowContainer: { + width: '100%', + height: '100%', + boxSizing: 'border-box', + display: 'flex', + flexDirection: 'column', + }, + titlebar: { + width: '100%', + minHeight: 60, + background: '#FAFAFA', + padding: '16px 40px', + boxSizing: 'border-box', + display: 'flex', + alignItems: 'center', + }, + titleBarButtons: { + display: 'flex', + alignItems: 'center', + marginRight: 32, + }, + windowBtn: { + margin: 0, + padding: 2, + width: 14, + height: 14, + borderRadius: '50%', + boxShadow: 'none', + border: '1px solid transparent', + display: 'flex', + alignItems: 'center', + justifyContent: 'center', + marginRight: 4, + marginLeft: 4, + cursor: 'pointer', + boxSizing: 'border-box', + }, + mainContainer: { + display: 'flex', + flex: 1, + flexDirection: 'column', + alignItems: 'center', + justifyContent: 'center', + padding: 40, + boxSizing: 'border-box', + }, + content: { + width: '100%', + maxWidth: 700, + minHeight: 100, + padding: 40, + borderRadius: 6, + border: '1px solid #EDECEE', + }, + header: { + display: 'flex', + alignItems: 'center', + marginBottom: 32, + }, + heading: { + fontFamily: 'AvenirMedium', + marginLeft: 16, + marginTop: 8, + marginBottom: 0, + }, + mainMessageContainer: { + paddingTop: 16, + paddingBottom: 16, + }, + text: { + fontFamily: 'AvenirMedium', + color: '#716A78', + maxWidth: 600, + lineHeight: '160%', + }, + linkInText: { + fontFamily: 'AvenirMedium', + color: '#716A78', + }, + buttons: { + paddingTop: 16, + paddingBottom: 16, + display: 'flex', + justifyContent: 'space-between', + alignItems: 'center', + columnGap: 10, + }, + button: { + display: 'flex', + flexDirection: 'row', + justifyContent: 'center', + alignItems: 'center', + padding: '6px 24px', + paddingTop: 10, + gap: 10, + background: 'linear-gradient(239.91deg, #9330FF 0%, #593A9B 131%)', + borderRadius: 8, + borderWidth: 0, + fontFamily: 'AvenirMedium', + boxShadow: 'none', + fontSize: 14, + color: '#fff', + cursor: 'pointer', + }, + detailsContainer: { + display: 'none', + background: '#FAFAFA', + borderRadius: 6, + border: '1px solid #EDECEE', + padding: 12, + maxHeight: 200, + overflow: 'auto', + }, + detailsText: { + fontSize: 14, + fontFamily: 'AvenirMedium', + opacity: 0.7, + }, +} + +/** + * Catches the app exceptions. + * + * This implementation tries to NOT depend on anything, ie. styled-components and themes. + */ +class ErrorBoundary extends Component { + state = { + error: undefined, + showDetails: false, + } + + static getDerivedStateFromError(error: Error) { + return { error: error.toString() } + } + + componentDidCatch(error: Error, errorInfo: ErrorInfo) { + // eslint-disable-next-line no-console + console.error('Uncaught error:', error, errorInfo.componentStack) + } + + render() { + if (this.state.error) { + return ( +
+
+
+ + + +
+ +
+
+
+
+
+ +

Houston, we have a problem!

+
+

+ Something went terribly wrong :( Try to restart the + application, and if that doesn't magically fix the + problem, feel free to let us know on{' '} + + Discord + + . +

+
+
+
+ + +
+
+
+

{this.state.error}

+
+
+
+
+ ) + } + + return this.props.children + } +} + +export default ErrorBoundary diff --git a/applications/launchpad/gui-react/src/index.tsx b/applications/launchpad/gui-react/src/index.tsx index 4f6851d914..42e82ba542 100644 --- a/applications/launchpad/gui-react/src/index.tsx +++ b/applications/launchpad/gui-react/src/index.tsx @@ -7,6 +7,7 @@ import './index.css' import App from './App' import reportWebVitals from './reportWebVitals' import { store, persistor } from './store' +import ErrorBoundary from './ErrorBoundary' const container = document.getElementById('root') // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -15,7 +16,9 @@ root.render( - + + + ,