Skip to content

Commit

Permalink
console: Refactor error boundary component
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelJankoski committed Nov 17, 2023
1 parent 0e693a8 commit a50e11a
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions pkg/webui/console/components/events/error-boundary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2020 The Things Network Foundation, The Things Industries B.V.
// Copyright © 2023 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import React from 'react'
import React, { useCallback, useState } from 'react'

import Icon from '@ttn-lw/components/icon'

Expand All @@ -24,32 +24,38 @@ import m from './messages'

import style from './events.styl'

class EventErrorBoundary extends React.Component {
state = { hasErrored: false, error: undefined, expanded: false }
const EventErrorBoundary = ({ children }) => {
const [hasErrored, setHasErrored] = useState(false)

static getDerivedStateFromError() {
return { hasErrored: true }
}

render() {
const { hasErrored } = this.state
const { children } = this.props
const handleError = useCallback(() => {
setHasErrored(true)
}, [])

if (hasErrored) {
return (
<div className={style.cellError}>
<Icon icon="error" className={style.eventIcon} />
<Message content={m.errorOverviewEntry} />
</div>
)
}

return children
if (hasErrored) {
return (
<div className={style.cellError}>
<Icon icon="error" className={style.eventIcon} />
<Message content={m.errorOverviewEntry} />
</div>
)
}

return (
<React.Fragment>
{React.Children.map(children, child => {
if (!child) {
return null
}
return React.cloneElement(child, {
onError: handleError,
})
})}
</React.Fragment>
)
}

EventErrorBoundary.propTypes = {
children: PropTypes.node.isRequired,
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]).isRequired,
}

export default EventErrorBoundary

0 comments on commit a50e11a

Please sign in to comment.