2020-08-29 01:23:27 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import PropTypes from 'prop-types';
|
2023-03-20 21:08:35 +08:00
|
|
|
import ModalSimple from '/imports/ui/components/common/modal/simple/component';
|
2021-04-15 23:12:10 +08:00
|
|
|
import AudioService from '/imports/ui/components/audio/service';
|
2021-11-11 03:10:35 +08:00
|
|
|
import Styled from './styles';
|
2020-08-29 01:23:27 +08:00
|
|
|
|
2021-04-28 23:44:08 +08:00
|
|
|
const SELECT_RANDOM_USER_COUNTDOWN = Meteor.settings.public.selectRandomUser.countdown;
|
|
|
|
|
2020-08-29 01:23:27 +08:00
|
|
|
const messages = defineMessages({
|
|
|
|
noViewers: {
|
|
|
|
id: 'app.modal.randomUser.noViewers.description',
|
|
|
|
description: 'Label displayed when no viewers are avaiable',
|
|
|
|
},
|
|
|
|
selected: {
|
|
|
|
id: 'app.modal.randomUser.selected.description',
|
|
|
|
description: 'Label shown to the selected user',
|
|
|
|
},
|
|
|
|
randUserTitle: {
|
|
|
|
id: 'app.modal.randomUser.title',
|
|
|
|
description: 'Modal title label',
|
|
|
|
},
|
2021-04-15 23:12:10 +08:00
|
|
|
whollbeSelected: {
|
|
|
|
id: 'app.modal.randomUser.who',
|
|
|
|
description: 'Label shown during the selection',
|
|
|
|
},
|
|
|
|
onlyOneViewerTobeSelected: {
|
|
|
|
id: 'app.modal.randomUser.alone',
|
|
|
|
description: 'Label shown when only one viewer to be selected',
|
|
|
|
},
|
2020-08-29 01:23:27 +08:00
|
|
|
reselect: {
|
|
|
|
id: 'app.modal.randomUser.reselect.label',
|
|
|
|
description: 'select new random user button label',
|
|
|
|
},
|
|
|
|
ariaModalTitle: {
|
|
|
|
id: 'app.modal.randomUser.ariaLabel.title',
|
|
|
|
description: 'modal title displayed to screen reader',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
numAvailableViewers: PropTypes.number.isRequired,
|
2020-09-26 03:41:30 +08:00
|
|
|
randomUserReq: PropTypes.func.isRequired,
|
2020-08-29 01:23:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class RandomUserSelect extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2020-09-26 03:41:30 +08:00
|
|
|
if (props.currentUser.presenter) {
|
|
|
|
props.randomUserReq();
|
|
|
|
}
|
2021-04-16 02:33:40 +08:00
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
if (SELECT_RANDOM_USER_COUNTDOWN) {
|
2021-04-28 23:44:08 +08:00
|
|
|
this.state = {
|
|
|
|
count: 0,
|
|
|
|
};
|
|
|
|
this.play = this.play.bind(this);
|
|
|
|
}
|
2021-04-15 23:12:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
iterateSelection() {
|
2021-04-16 02:33:40 +08:00
|
|
|
if (this.props.mappedRandomlySelectedUsers.length > 1) {
|
|
|
|
const that = this;
|
2021-04-15 23:12:10 +08:00
|
|
|
setTimeout(delay(that.props.mappedRandomlySelectedUsers, 1), that.props.mappedRandomlySelectedUsers[1][1]);
|
|
|
|
function delay(arr, num) {
|
|
|
|
that.setState({
|
|
|
|
count: num,
|
|
|
|
});
|
2021-04-16 02:33:40 +08:00
|
|
|
if (num < that.props.mappedRandomlySelectedUsers.length - 1) {
|
|
|
|
setTimeout(() => { delay(arr, num + 1); }, arr[num + 1][1]);
|
2021-04-15 23:12:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2022-03-01 22:01:21 +08:00
|
|
|
const { keepModalOpen, toggleKeepModalOpen, currentUser } = this.props;
|
|
|
|
|
|
|
|
if (currentUser.presenter && !keepModalOpen) {
|
|
|
|
toggleKeepModalOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SELECT_RANDOM_USER_COUNTDOWN && !currentUser.presenter) {
|
2021-04-15 23:12:10 +08:00
|
|
|
this.iterateSelection();
|
|
|
|
}
|
2020-08-29 01:23:27 +08:00
|
|
|
}
|
|
|
|
|
2021-04-26 20:07:59 +08:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2021-11-04 23:16:47 +08:00
|
|
|
if (SELECT_RANDOM_USER_COUNTDOWN) {
|
|
|
|
if (this.props.currentUser.presenter && this.state.count === 0) {
|
2021-04-28 23:44:08 +08:00
|
|
|
this.iterateSelection();
|
|
|
|
}
|
2021-04-26 20:07:59 +08:00
|
|
|
|
2021-10-28 02:47:48 +08:00
|
|
|
if ((prevState.count !== this.state.count) && this.props.keepModalOpen) {
|
2021-04-28 23:44:08 +08:00
|
|
|
this.play();
|
|
|
|
}
|
2021-04-26 20:07:59 +08:00
|
|
|
}
|
2020-08-29 01:23:27 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 23:12:10 +08:00
|
|
|
play() {
|
|
|
|
AudioService.playAlertSound(`${Meteor.settings.public.app.cdn
|
|
|
|
+ Meteor.settings.public.app.basename
|
|
|
|
+ Meteor.settings.public.app.instanceId}`
|
|
|
|
+ '/resources/sounds/Poll.mp3');
|
|
|
|
}
|
|
|
|
|
|
|
|
reselect() {
|
2021-11-04 23:16:47 +08:00
|
|
|
if (SELECT_RANDOM_USER_COUNTDOWN) {
|
2021-04-28 23:44:08 +08:00
|
|
|
this.setState({
|
|
|
|
count: 0,
|
|
|
|
});
|
|
|
|
}
|
2021-04-15 23:12:10 +08:00
|
|
|
this.props.randomUserReq();
|
|
|
|
}
|
|
|
|
|
2020-08-29 01:23:27 +08:00
|
|
|
render() {
|
|
|
|
const {
|
2021-10-28 02:47:48 +08:00
|
|
|
keepModalOpen,
|
|
|
|
toggleKeepModalOpen,
|
2020-08-29 01:23:27 +08:00
|
|
|
intl,
|
2023-03-22 01:55:43 +08:00
|
|
|
setIsOpen,
|
2020-08-29 01:23:27 +08:00
|
|
|
numAvailableViewers,
|
2020-09-26 03:41:30 +08:00
|
|
|
currentUser,
|
2020-10-19 22:46:41 +08:00
|
|
|
clearRandomlySelectedUser,
|
2021-04-15 23:12:10 +08:00
|
|
|
mappedRandomlySelectedUsers,
|
2023-04-12 23:51:23 +08:00
|
|
|
isOpen,
|
|
|
|
priority,
|
2020-08-29 01:23:27 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2021-04-28 23:44:08 +08:00
|
|
|
const counter = SELECT_RANDOM_USER_COUNTDOWN ? this.state.count : 0;
|
|
|
|
if (mappedRandomlySelectedUsers.length < counter + 1) return null;
|
2021-04-15 23:12:10 +08:00
|
|
|
|
2021-12-08 01:36:34 +08:00
|
|
|
const selectedUser = SELECT_RANDOM_USER_COUNTDOWN ? mappedRandomlySelectedUsers[counter][0] :
|
|
|
|
mappedRandomlySelectedUsers[mappedRandomlySelectedUsers.length - 1][0];
|
2021-04-28 23:44:08 +08:00
|
|
|
const countDown = SELECT_RANDOM_USER_COUNTDOWN ?
|
|
|
|
mappedRandomlySelectedUsers.length - this.state.count - 1 : 0;
|
2021-04-16 02:33:40 +08:00
|
|
|
|
2021-02-04 14:18:40 +08:00
|
|
|
let viewElement;
|
2022-11-11 02:09:21 +08:00
|
|
|
let title;
|
2020-08-29 01:23:27 +08:00
|
|
|
|
2021-04-15 23:12:10 +08:00
|
|
|
const amISelectedUser = currentUser.userId === selectedUser.userId;
|
|
|
|
if (numAvailableViewers < 1 || (currentUser.presenter && amISelectedUser)) { // there's no viewers to select from,
|
2021-04-16 02:33:40 +08:00
|
|
|
// or when you are the presenter but selected, which happens when the presenter ability is passed to somebody
|
|
|
|
// and people are entering and leaving the meeting
|
2021-02-04 14:18:40 +08:00
|
|
|
// display modal informing presenter that there's no viewers to select from
|
|
|
|
viewElement = (
|
2021-11-11 03:10:35 +08:00
|
|
|
<Styled.ModalViewContainer>
|
2022-02-04 22:13:42 +08:00
|
|
|
<div data-test="noViewersSelectedMessage">
|
|
|
|
{intl.formatMessage(messages.noViewers)}
|
|
|
|
</div>
|
2021-11-11 03:10:35 +08:00
|
|
|
</Styled.ModalViewContainer>
|
2021-02-04 14:18:40 +08:00
|
|
|
);
|
2022-11-11 02:09:21 +08:00
|
|
|
title = intl.formatMessage(messages.randUserTitle);
|
2021-02-04 14:18:40 +08:00
|
|
|
} else { // viewers are available
|
|
|
|
if (!selectedUser) return null; // rendering triggered before selectedUser is available
|
|
|
|
|
|
|
|
// display modal with random user selection
|
|
|
|
viewElement = (
|
2021-11-11 03:10:35 +08:00
|
|
|
<Styled.ModalViewContainer>
|
|
|
|
<Styled.ModalAvatar aria-hidden style={{ backgroundColor: `${selectedUser.color}` }}>
|
2021-02-04 14:18:40 +08:00
|
|
|
{selectedUser.name.slice(0, 2)}
|
2021-11-11 03:10:35 +08:00
|
|
|
</Styled.ModalAvatar>
|
2022-02-24 22:49:56 +08:00
|
|
|
<Styled.SelectedUserName data-test="selectedUserName">
|
2021-02-04 14:18:40 +08:00
|
|
|
{selectedUser.name}
|
2021-11-11 03:10:35 +08:00
|
|
|
</Styled.SelectedUserName>
|
2021-04-15 23:12:10 +08:00
|
|
|
{currentUser.presenter
|
2021-11-04 23:16:47 +08:00
|
|
|
&& countDown === 0
|
2021-02-04 14:18:40 +08:00
|
|
|
&& (
|
2021-11-11 03:10:35 +08:00
|
|
|
<Styled.SelectButton
|
2021-02-04 14:18:40 +08:00
|
|
|
label={intl.formatMessage(messages.reselect)}
|
|
|
|
color="primary"
|
|
|
|
size="md"
|
2021-04-15 23:12:10 +08:00
|
|
|
onClick={() => this.reselect()}
|
2022-02-04 22:13:42 +08:00
|
|
|
data-test="selectAgainRadomUser"
|
2021-02-04 14:18:40 +08:00
|
|
|
/>
|
2021-04-16 02:33:40 +08:00
|
|
|
)}
|
2021-11-11 03:10:35 +08:00
|
|
|
</Styled.ModalViewContainer>
|
2021-02-04 14:18:40 +08:00
|
|
|
);
|
2022-11-11 02:09:21 +08:00
|
|
|
title = countDown == 0
|
|
|
|
? amISelectedUser
|
|
|
|
? `${intl.formatMessage(messages.selected)}`
|
|
|
|
: numAvailableViewers == 1 && currentUser.presenter
|
|
|
|
? `${intl.formatMessage(messages.onlyOneViewerTobeSelected)}`
|
|
|
|
: `${intl.formatMessage(messages.randUserTitle)}`
|
|
|
|
: `${intl.formatMessage(messages.whollbeSelected)} ${countDown}`;
|
2021-02-04 14:18:40 +08:00
|
|
|
}
|
2021-11-04 23:16:47 +08:00
|
|
|
if (keepModalOpen) {
|
2021-10-28 02:47:48 +08:00
|
|
|
return (
|
2023-03-20 21:08:35 +08:00
|
|
|
<ModalSimple
|
2021-10-28 02:47:48 +08:00
|
|
|
onRequestClose={() => {
|
2023-04-13 03:05:54 +08:00
|
|
|
if (currentUser.presenter) clearRandomlySelectedUser();
|
2021-10-28 02:47:48 +08:00
|
|
|
toggleKeepModalOpen();
|
2023-03-22 01:55:43 +08:00
|
|
|
setIsOpen(false);
|
2021-10-28 02:47:48 +08:00
|
|
|
}}
|
|
|
|
contentLabel={intl.formatMessage(messages.ariaModalTitle)}
|
2022-11-11 02:09:21 +08:00
|
|
|
title={title}
|
2023-04-12 23:51:23 +08:00
|
|
|
{...{
|
|
|
|
isOpen,
|
|
|
|
priority,
|
|
|
|
}}
|
2021-10-28 02:47:48 +08:00
|
|
|
>
|
|
|
|
{viewElement}
|
2023-03-20 21:08:35 +08:00
|
|
|
</ModalSimple>
|
2021-10-28 02:47:48 +08:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2020-08-29 01:23:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RandomUserSelect.propTypes = propTypes;
|
|
|
|
export default injectIntl(RandomUserSelect);
|