-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ErrorBoundary
- Loading branch information
Showing
4 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { ErrorInfo, ReactNode } from 'react' | ||
import { mobileModel, mobileVendor } from 'react-device-detect' | ||
|
||
interface Props { | ||
children: ReactNode | ||
} | ||
|
||
interface State { | ||
hasError: boolean | ||
deviceInfo: string | ||
} | ||
|
||
class ErrorBoundary extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props) | ||
this.state = { hasError: false, deviceInfo: '' } | ||
} | ||
|
||
static getDerivedStateFromError(error: Error): State { | ||
return { hasError: true, deviceInfo: '' } | ||
} | ||
|
||
componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
console.error('Error caught by ErrorBoundary:', error, errorInfo) | ||
} | ||
|
||
componentDidMount() { | ||
const deviceInfo = `${mobileVendor} ${mobileModel}` | ||
this.setState({ deviceInfo }) | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<> | ||
<h3>All apologies, the app is not yet available on this type of device.</h3> | ||
<br /> | ||
<p>{this.state.deviceInfo}</p> | ||
<br /> | ||
<p>Thank you for using the app from another device.</p> | ||
<br /> | ||
<p> | ||
Feel free to report this to Julien via <a href="https://matrix.to/#/@julienbrg:matrix.org">Element</a>,{' '} | ||
<a href="https://warpcast.com/julien-">Farcaster</a>, <a href="https://t.me/julienbrg">Telegram</a>,{' '} | ||
<a href="https://twitter.com/julienbrg">Twitter</a>, <a href="https://discordapp.com/users/julienbrg">Discord</a> or{' '} | ||
<a href="https://www.linkedin.com/in/julienberanger/">LinkedIn</a>. | ||
</p> | ||
</> | ||
) | ||
} | ||
|
||
return this.props.children | ||
} | ||
} | ||
|
||
export default ErrorBoundary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters