-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add MessageErrorBoundary component
- Loading branch information
1 parent
6596497
commit a9d4a08
Showing
6 changed files
with
65 additions
and
5 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
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
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
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
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
41 changes: 41 additions & 0 deletions
41
src/renderer/src/pages/home/Messages/MessageErrorBoundary.tsx
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,41 @@ | ||
import { Alert } from 'antd' | ||
import React from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
interface Props { | ||
fallback?: React.ReactNode | ||
children: React.ReactNode | ||
} | ||
|
||
interface State { | ||
hasError: boolean | ||
} | ||
|
||
const ErrorFallback = ({ fallback }: { fallback?: React.ReactNode }) => { | ||
const { t } = useTranslation() | ||
return ( | ||
fallback || ( | ||
<Alert message={t('error.render.title')} description={t('error.render.description')} type="error" showIcon /> | ||
) | ||
) | ||
} | ||
|
||
class MessageErrorBoundary extends React.Component<Props, State> { | ||
constructor(props: Props) { | ||
super(props) | ||
this.state = { hasError: false } | ||
} | ||
|
||
static getDerivedStateFromError() { | ||
return { hasError: true } | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return <ErrorFallback fallback={this.props.fallback} /> | ||
} | ||
return this.props.children | ||
} | ||
} | ||
|
||
export default MessageErrorBoundary |