2021-04-23 09:07:48 +08:00
|
|
|
import React, { PureComponent } from 'react';
|
2021-03-26 05:22:50 +08:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
2022-02-15 04:20:50 +08:00
|
|
|
import Button from '/imports/ui/components/common/button/component';
|
2021-03-26 05:22:50 +08:00
|
|
|
import ConnectionStatusModalContainer from '/imports/ui/components/connection-status/modal/container';
|
|
|
|
import ConnectionStatusService from '/imports/ui/components/connection-status/service';
|
2021-04-23 09:07:48 +08:00
|
|
|
import Icon from '/imports/ui/components/connection-status/icon/component';
|
2021-11-09 04:11:14 +08:00
|
|
|
import Styled from './styles';
|
2021-03-26 05:22:50 +08:00
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
label: {
|
|
|
|
id: 'app.connection-status.label',
|
|
|
|
description: 'Connection status button label',
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
id: 'app.connection-status.description',
|
|
|
|
description: 'Connection status button description',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-04-23 09:07:48 +08:00
|
|
|
class ConnectionStatusButton extends PureComponent {
|
2023-03-21 01:57:36 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isModalOpen: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 09:07:48 +08:00
|
|
|
renderIcon(level = 'normal') {
|
|
|
|
return(
|
2021-11-09 04:11:14 +08:00
|
|
|
<Styled.IconWrapper>
|
2021-04-23 09:07:48 +08:00
|
|
|
<Icon
|
|
|
|
level={level}
|
|
|
|
grayscale
|
|
|
|
/>
|
2021-11-09 04:11:14 +08:00
|
|
|
</Styled.IconWrapper>
|
2021-04-23 09:07:48 +08:00
|
|
|
);
|
2021-03-26 05:22:50 +08:00
|
|
|
}
|
|
|
|
|
2023-03-21 01:57:36 +08:00
|
|
|
setModalIsOpen = (isOpen) => this.setState({ isModalOpen: isOpen });
|
|
|
|
|
|
|
|
renderModal(isModalOpen) {
|
|
|
|
return (
|
|
|
|
isModalOpen ?
|
|
|
|
<ConnectionStatusModalContainer
|
|
|
|
{...{
|
|
|
|
isModalOpen,
|
|
|
|
setModalIsOpen: this.setModalIsOpen
|
|
|
|
}}
|
|
|
|
/> : null
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-26 05:22:50 +08:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
intl,
|
|
|
|
connected,
|
2021-04-23 09:07:48 +08:00
|
|
|
} = this.props;
|
2023-03-21 01:57:36 +08:00
|
|
|
const { isModalOpen } = this.state;
|
|
|
|
|
2021-04-23 09:07:48 +08:00
|
|
|
|
|
|
|
if (!connected) {
|
|
|
|
return (
|
2021-11-09 04:11:14 +08:00
|
|
|
<Styled.ButtonWrapper>
|
2021-04-23 09:07:48 +08:00
|
|
|
<Button
|
|
|
|
customIcon={this.renderIcon()}
|
|
|
|
label={intl.formatMessage(intlMessages.label)}
|
|
|
|
hideLabel
|
|
|
|
aria-label={intl.formatMessage(intlMessages.description)}
|
|
|
|
size="sm"
|
|
|
|
disabled
|
|
|
|
ghost
|
|
|
|
circle
|
|
|
|
onClick={() => {}}
|
|
|
|
data-test="connectionStatusButton"
|
|
|
|
/>
|
2023-03-21 01:57:36 +08:00
|
|
|
{this.renderModal(isModalOpen)}
|
2021-11-09 04:11:14 +08:00
|
|
|
</Styled.ButtonWrapper>
|
2021-04-23 09:07:48 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
2021-03-26 05:22:50 +08:00
|
|
|
stats,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
let color;
|
2021-04-23 09:07:48 +08:00
|
|
|
switch (stats) {
|
|
|
|
case 'warning':
|
|
|
|
color = 'success';
|
|
|
|
break;
|
|
|
|
case 'danger':
|
|
|
|
color = 'warning';
|
|
|
|
ConnectionStatusService.notification('warning', intl);
|
|
|
|
break;
|
|
|
|
case 'critical':
|
|
|
|
color = 'danger';
|
|
|
|
ConnectionStatusService.notification('error', intl);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
color = 'success';
|
2021-03-26 05:22:50 +08:00
|
|
|
}
|
|
|
|
|
2022-09-15 09:16:27 +08:00
|
|
|
const currentStatus = stats ? stats : 'normal';
|
2021-04-23 09:07:48 +08:00
|
|
|
|
2021-03-26 05:22:50 +08:00
|
|
|
return (
|
2021-11-09 04:11:14 +08:00
|
|
|
<Styled.ButtonWrapper>
|
2021-04-23 09:07:48 +08:00
|
|
|
<Button
|
2022-09-15 09:16:27 +08:00
|
|
|
customIcon={this.renderIcon(currentStatus)}
|
2021-04-23 09:07:48 +08:00
|
|
|
label={intl.formatMessage(intlMessages.label)}
|
|
|
|
hideLabel
|
|
|
|
aria-label={intl.formatMessage(intlMessages.description)}
|
|
|
|
size="sm"
|
|
|
|
color={color}
|
|
|
|
circle
|
2023-03-21 01:57:36 +08:00
|
|
|
onClick={() => this.setState({isModalOpen: true})}
|
2021-04-23 09:07:48 +08:00
|
|
|
data-test="connectionStatusButton"
|
|
|
|
/>
|
2023-03-21 01:57:36 +08:00
|
|
|
{this.renderModal(isModalOpen)}
|
2021-11-09 04:11:14 +08:00
|
|
|
</Styled.ButtonWrapper>
|
2021-03-26 05:22:50 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 01:57:36 +08:00
|
|
|
export default injectIntl(ConnectionStatusButton);
|