File size: 966 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React, { ReactNode } from 'react'
import { Text, MessageBox } from '@adminjs/design-system'

import { useTranslation } from '../../hooks/index.js'

type State = {
  error: any;
}

const ErrorMessage: React.FC<State> = ({ error }) => {
  const { translateMessage } = useTranslation()
  return (
    <MessageBox m="xxl" variant="danger" message="Javascript Error">
      <Text>{error.toString()}</Text>
      <Text mt="default">{translateMessage('seeConsoleForMore')}</Text>
    </MessageBox>
  )
}

export class ErrorBoundary extends React.Component<any, State> {
  constructor(props) {
    super(props)
    this.state = {
      error: null,
    }
  }

  componentDidCatch(error): void {
    this.setState({ error })
  }

  render(): ReactNode {
    const { children } = this.props

    const { error } = this.state

    if (error !== null) {
      return (<ErrorMessage error={error} />)
    }

    return children || null
  }
}

export default ErrorBoundary