From 73e9e8bd17c74a5bb40c69106064fc2f2b9892ab Mon Sep 17 00:00:00 2001 From: Pavel Jankoski Date: Fri, 17 Nov 2023 17:53:34 +0100 Subject: [PATCH] console: Refactor gateway connection --- .../gateway-connection/gateway-connection.js | 102 +++++++++--------- 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/pkg/webui/console/containers/gateway-connection/gateway-connection.js b/pkg/webui/console/containers/gateway-connection/gateway-connection.js index f27334cfd7..54463ba764 100644 --- a/pkg/webui/console/containers/gateway-connection/gateway-connection.js +++ b/pkg/webui/console/containers/gateway-connection/gateway-connection.js @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import React from 'react' +import React, { useEffect, useMemo } from 'react' import classnames from 'classnames' import { FormattedNumber, defineMessages } from 'react-intl' @@ -44,45 +44,26 @@ const m = defineMessages({ 'The amount of received uplinks and sent downlinks of this gateway since the last (re)connect. Note that some gateway types reconnect frequently causing the counter to be reset.', }) -class GatewayConnection extends React.PureComponent { - static propTypes = { - className: PropTypes.string, - error: PropTypes.oneOfType([PropTypes.error, PropTypes.shape({ message: PropTypes.message })]), - fetching: PropTypes.bool, - isOtherCluster: PropTypes.bool.isRequired, - lastSeen: PropTypes.oneOfType([ - PropTypes.string, - PropTypes.number, // Support timestamps. - PropTypes.instanceOf(Date), - ]), - startStatistics: PropTypes.func.isRequired, - statistics: PropTypes.gatewayStats, - stopStatistics: PropTypes.func.isRequired, - } - - static defaultProps = { - className: undefined, - fetching: false, - error: null, - statistics: null, - lastSeen: undefined, - } - - componentDidMount() { - const { startStatistics } = this.props - +const GatewayConnection = props => { + const { + startStatistics, + stopStatistics, + statistics, + error, + fetching, + lastSeen, + isOtherCluster, + className, + } = props + + useEffect(() => { startStatistics() - } - - componentWillUnmount() { - const { stopStatistics } = this.props - - stopStatistics() - } - - get status() { - const { statistics, error, fetching, lastSeen, isOtherCluster } = this.props + return () => { + stopStatistics() + } + }, [startStatistics, stopStatistics]) + const status = useMemo(() => { const statsNotFound = Boolean(error) && isNotFoundError(error) const isDisconnected = Boolean(statistics) && Boolean(statistics.disconnected_at) const isFetching = !Boolean(statistics) && fetching @@ -170,11 +151,9 @@ class GatewayConnection extends React.PureComponent { } return node - } - - get messages() { - const { statistics } = this.props + }, [error, fetching, isOtherCluster, lastSeen, statistics]) + const messages = useMemo(() => { if (!statistics) { return null } @@ -199,18 +178,37 @@ class GatewayConnection extends React.PureComponent { ) - } + }, [statistics]) + + return ( +
+ {messages} + {status} +
+ ) +} - render() { - const { className } = this.props +GatewayConnection.propTypes = { + className: PropTypes.string, + error: PropTypes.oneOfType([PropTypes.error, PropTypes.shape({ message: PropTypes.message })]), + fetching: PropTypes.bool, + isOtherCluster: PropTypes.bool.isRequired, + lastSeen: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.number, // Support timestamps. + PropTypes.instanceOf(Date), + ]), + startStatistics: PropTypes.func.isRequired, + statistics: PropTypes.gatewayStats, + stopStatistics: PropTypes.func.isRequired, +} - return ( -
- {this.messages} - {this.status} -
- ) - } +GatewayConnection.defaultProps = { + className: undefined, + fetching: false, + error: null, + statistics: null, + lastSeen: undefined, } export default GatewayConnection