mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React, {Component, ErrorInfo, ReactNode} from 'react'
2import {msg} from '@lingui/macro'
3import {useLingui} from '@lingui/react'
4
5import {logger} from '#/logger'
6import {ErrorScreen} from './error/ErrorScreen'
7import {CenteredView} from './Views'
8
9interface Props {
10 children?: ReactNode
11 renderError?: (error: any) => ReactNode
12}
13
14interface State {
15 hasError: boolean
16 error: any
17}
18
19export class ErrorBoundary extends Component<Props, State> {
20 public state: State = {
21 hasError: false,
22 error: undefined,
23 }
24
25 public static getDerivedStateFromError(error: Error): State {
26 return {hasError: true, error}
27 }
28
29 public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
30 logger.error(error, {errorInfo})
31 }
32
33 public render() {
34 if (this.state.hasError) {
35 if (this.props.renderError) {
36 return this.props.renderError(this.state.error)
37 }
38
39 return (
40 <CenteredView style={{height: '100%', flex: 1}}>
41 <TranslatedErrorScreen details={this.state.error.toString()} />
42 </CenteredView>
43 )
44 }
45
46 return this.props.children
47 }
48}
49
50function TranslatedErrorScreen({details}: {details?: string}) {
51 const {_} = useLingui()
52
53 return (
54 <ErrorScreen
55 title={_(msg`Oh no!`)}
56 message={_(
57 msg`There was an unexpected issue in the application. Please let us know if this happened to you!`,
58 )}
59 details={details}
60 />
61 )
62}