bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/status-notifier/component.jsx

203 lines
6.1 KiB
React
Raw Normal View History

2020-06-30 10:43:20 +08:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { defineMessages, injectIntl } from 'react-intl';
import { toast } from 'react-toastify';
2022-02-15 22:51:51 +08:00
import Icon from '/imports/ui/components/common/icon/component';
2020-06-30 10:43:20 +08:00
import { ENTER } from '/imports/utils/keyCodes';
2021-11-09 03:54:19 +08:00
import Styled from './styles';
2020-12-01 00:09:35 +08:00
import {Meteor} from "meteor/meteor";
2022-02-25 01:00:27 +08:00
import TooltipContainer from '/imports/ui/components/common/tooltip/container';
2020-06-30 10:43:20 +08:00
const messages = defineMessages({
lowerHandsLabel: {
id: 'app.statusNotifier.lowerHands',
description: 'text displayed to clear all raised hands',
},
2022-02-25 01:00:27 +08:00
lowerHandDescOneUser: {
id: 'app.statusNotifier.lowerHandDescOneUser',
description: 'text displayed to clear a single user raised hands',
},
2020-06-30 10:43:20 +08:00
raisedHandsTitle: {
id: 'app.statusNotifier.raisedHandsTitle',
description: 'heading for raised hands toast',
},
raisedHandDesc: {
id: 'app.statusNotifier.raisedHandDesc',
description: 'label for multiple users with raised hands',
},
raisedHandDescOneUser: {
id: 'app.statusNotifier.raisedHandDescOneUser',
description: 'label for a single user with raised hand',
},
2020-06-30 10:43:20 +08:00
and: {
id: 'app.statusNotifier.and',
description: 'used as conjunction word',
},
});
const MAX_AVATAR_COUNT = 3;
class StatusNotifier extends Component {
constructor(props) {
super(props);
this.statusNotifierId = null;
2020-12-01 00:09:35 +08:00
this.audio = new Audio(`${Meteor.settings.public.app.cdn + Meteor.settings.public.app.basename + Meteor.settings.public.app.instanceId}/resources/sounds/bbb-handRaise.mp3`);
2020-06-30 10:43:20 +08:00
this.renderRaisedHands = this.renderRaisedHands.bind(this);
this.getRaisedHandNames = this.getRaisedHandNames.bind(this);
this.raisedHandAvatars = this.raisedHandAvatars.bind(this);
}
componentDidUpdate(prevProps) {
const {
emojiUsers, raiseHandAudioAlert, raiseHandPushAlert, status, isViewer, isPresenter,
2020-06-30 10:43:20 +08:00
} = this.props;
if (isViewer && !isPresenter) {
if (this.statusNotifierId) toast.dismiss(this.statusNotifierId);
return false;
}
2020-06-30 10:43:20 +08:00
switch (status) {
case 'raiseHand':
if (emojiUsers.length === 0) return toast.dismiss(this.statusNotifierId);
2020-06-30 10:43:20 +08:00
if (raiseHandAudioAlert && emojiUsers.length > prevProps.emojiUsers.length) {
this.audio.play();
}
if (raiseHandPushAlert) {
if (this.statusNotifierId) {
return toast.update(this.statusNotifierId, {
render: this.renderRaisedHands(),
});
}
this.statusNotifierId = toast(this.renderRaisedHands(), {
onClose: () => { this.statusNotifierId = null; },
autoClose: false,
closeOnClick: false,
closeButton: false,
2022-02-25 01:00:27 +08:00
className: "raiseHandToast",
2020-06-30 10:43:20 +08:00
});
}
break;
default:
break;
}
return true;
}
getRaisedHandNames() {
const { emojiUsers, intl } = this.props;
if (emojiUsers.length === 0) return '';
const _names = emojiUsers.map(u => u.name);
const { length } = _names;
const and = intl.formatMessage(messages.and);
let formattedNames = '';
switch (length) {
case 1:
formattedNames = _names;
break;
case 2:
formattedNames = _names.join(` ${and} `);
break;
case 3:
formattedNames = _names.slice(0, length - 1).join(', ');
formattedNames += ` ${and} ${_names.slice(length - 1)}`;
break;
default:
formattedNames = _names.slice(0, MAX_AVATAR_COUNT).join(', ');
formattedNames += ` ${and} ${length - MAX_AVATAR_COUNT}+ `;
2020-06-30 10:43:20 +08:00
break;
}
const raisedHandMessageString
= length === 1 ? messages.raisedHandDescOneUser : messages.raisedHandDesc;
return intl.formatMessage(raisedHandMessageString, { 0: formattedNames });
2020-06-30 10:43:20 +08:00
}
raisedHandAvatars() {
2022-02-25 01:00:27 +08:00
const { emojiUsers, clearUserStatus, intl } = this.props;
2020-06-30 10:43:20 +08:00
let users = emojiUsers;
if (emojiUsers.length > MAX_AVATAR_COUNT) users = users.slice(0, MAX_AVATAR_COUNT);
const avatars = users.map(u => (
2022-02-25 01:00:27 +08:00
<TooltipContainer
2020-06-30 10:43:20 +08:00
key={`statusToastAvatar-${u.userId}`}
2022-02-25 01:00:27 +08:00
title={intl.formatMessage(messages.lowerHandDescOneUser, { 0: u.name })}>
<Styled.Avatar
role="button"
tabIndex={0}
style={{ backgroundColor: `${u.color}` }}
onClick={() => clearUserStatus(u.userId)}
onKeyDown={e => (e.keyCode === ENTER ? clearUserStatus(u.userId) : null)}
data-test="avatarsWrapperAvatar"
>
{u.name.slice(0, 2)}
</Styled.Avatar>
</TooltipContainer>
2020-06-30 10:43:20 +08:00
));
if (emojiUsers.length > MAX_AVATAR_COUNT) {
avatars.push(
2021-11-09 03:54:19 +08:00
<Styled.AvatarsExtra key={`statusToastAvatar-${emojiUsers.length}`}>
2020-06-30 10:43:20 +08:00
{emojiUsers.length}
2021-11-09 03:54:19 +08:00
</Styled.AvatarsExtra>,
2020-06-30 10:43:20 +08:00
);
}
return avatars;
}
renderRaisedHands() {
const { emojiUsers, intl, clearUserStatus } = this.props;
const formattedRaisedHands = this.getRaisedHandNames();
return (
<div>
2021-11-09 03:54:19 +08:00
<Styled.ToastIcon>
<Styled.IconWrapper>
2020-06-30 10:43:20 +08:00
<Icon iconName="hand" />
2021-11-09 03:54:19 +08:00
</Styled.IconWrapper>
</Styled.ToastIcon>
<Styled.AvatarsWrapper data-test="avatarsWrapper">
2020-06-30 10:43:20 +08:00
{this.raisedHandAvatars()}
2021-11-09 03:54:19 +08:00
</Styled.AvatarsWrapper>
<Styled.ToastMessage>
2020-06-30 10:43:20 +08:00
<div>{intl.formatMessage(messages.raisedHandsTitle)}</div>
{formattedRaisedHands}
2021-11-09 03:54:19 +08:00
</Styled.ToastMessage>
2021-11-11 19:47:39 +08:00
<Styled.ToastSeparator />
2021-11-09 03:54:19 +08:00
<Styled.ClearButton
2020-06-30 10:43:20 +08:00
label={intl.formatMessage(messages.lowerHandsLabel)}
color="default"
size="md"
onClick={() => {
emojiUsers.map(u => clearUserStatus(u.userId));
}}
/>
</div>
);
}
render() { return null; }
}
export default injectIntl(StatusNotifier);
StatusNotifier.propTypes = {
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
clearUserStatus: PropTypes.func.isRequired,
emojiUsers: PropTypes.instanceOf(Array).isRequired,
status: PropTypes.string.isRequired,
raiseHandAudioAlert: PropTypes.bool.isRequired,
raiseHandPushAlert: PropTypes.bool.isRequired,
};