From 3b7326dac8558008c91bd768aae44f90444a902c Mon Sep 17 00:00:00 2001 From: Taso Date: Wed, 21 Jul 2021 12:23:47 -0400 Subject: [PATCH] feat(phone/error-handling): add top level error boundary for reload --- phone/src/Phone.tsx | 35 +++++----- phone/src/locale/en.json | 5 ++ .../ui/components/TopLevelErrorComponent.tsx | 64 +++++++++++++++++++ 3 files changed, 88 insertions(+), 16 deletions(-) create mode 100644 phone/src/ui/components/TopLevelErrorComponent.tsx diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index d595fce..d7ca21c 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -32,6 +32,7 @@ import PhoneWrapper from './PhoneWrapper'; import dayjs from 'dayjs'; import DefaultConfig from '../../config.json'; +import { TopLevelErrorComponent } from './ui/components/TopLevelErrorComponent'; function Phone() { const { t, i18n } = useTranslation(); @@ -78,22 +79,24 @@ function Phone() { return (
- - - -
- <> - - {callModal && } - {apps.map((App) => ( - - ))} - - - -
- -
+ + + + +
+ <> + + {callModal && } + {apps.map((App) => ( + + ))} + + + +
+ +
+
); } diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index d6a982f..0724eb2 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -1,5 +1,10 @@ { "translation": { + "MISC": { + "TOP_LEVEL_ERR_TITLE": "Fatal Error", + "TOP_LEVEL_ERR_ACTION": "Reload", + "TOP_LEVEL_ERR_MSG": "NPWD has encountered an error it was unable to recover from. You will need to reload the NUI window. The error is shown below for developer reference." + }, "DATE_FORMAT": "MM/DD/YYYY", "DATE_TIME_FORMAT": "MM/DD/YYYY hh:mm A", "CALLS": { diff --git a/phone/src/ui/components/TopLevelErrorComponent.tsx b/phone/src/ui/components/TopLevelErrorComponent.tsx new file mode 100644 index 0000000..c11e458 --- /dev/null +++ b/phone/src/ui/components/TopLevelErrorComponent.tsx @@ -0,0 +1,64 @@ +import React from 'react'; +import { + Button, + Dialog, + DialogActions, + DialogContent, + DialogContentText, + DialogTitle, +} from '@material-ui/core'; +import { useTranslation } from 'react-i18next'; + +interface TopLevelErrorCompProps { + hasError: boolean; + errorMsg: string; +} + +interface ErrorDialogCompProps { + isOpen: boolean; + errorMsg: string; +} + +const ErrorDialogComp: React.FC = ({ isOpen, errorMsg }) => { + const { t } = useTranslation(); + + const handleReloadClick = () => window.location.reload(); + + return ( + + {t('MISC.TOP_LEVEL_ERR_TITLE')} + + + {t('MISC.TOP_LEVEL_ERR_MSG')} +
+
+ {errorMsg} +
+ + + +
+
+ ); +}; + +export class TopLevelErrorComponent extends React.Component { + public state = { + hasError: false, + errorMsg: '', + }; + + static getDerivedStateFromError(error: Error) { + return { hasError: true, errorMsg: error.message }; + } + + render() { + if (this.state.hasError) { + return ; + } + + return this.props.children; + } +}