2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2016-03-23 10:20:52 -04:00
|
|
|
import UserTypingStore from 'stores/user_typing_store.jsx';
|
2015-10-15 10:44:04 -04:00
|
|
|
|
2016-03-23 10:20:52 -04:00
|
|
|
import {FormattedMessage} from 'react-intl';
|
2016-02-01 17:23:45 -03:00
|
|
|
|
2016-03-14 08:50:46 -04:00
|
|
|
import React from 'react';
|
|
|
|
|
|
2016-02-01 17:23:45 -03:00
|
|
|
class MsgTyping extends React.Component {
|
2015-09-01 17:06:31 -07:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
2016-03-23 10:20:52 -04:00
|
|
|
this.onTypingChange = this.onTypingChange.bind(this);
|
2015-10-26 17:44:29 +01:00
|
|
|
this.updateTypingText = this.updateTypingText.bind(this);
|
2015-10-25 02:00:55 +01:00
|
|
|
this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this);
|
2015-09-01 17:06:31 -07:00
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
text: ''
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 10:20:52 -04:00
|
|
|
componentWillMount() {
|
|
|
|
|
UserTypingStore.addChangeListener(this.onTypingChange);
|
|
|
|
|
this.onTypingChange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
UserTypingStore.removeChangeListener(this.onTypingChange);
|
2015-09-01 17:06:31 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-27 11:27:16 -05:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
|
if (this.props.channelId !== nextProps.channelId) {
|
2016-03-23 10:20:52 -04:00
|
|
|
this.updateTypingText(UserTypingStore.getUsersTyping(nextProps.channelId, nextProps.parentId));
|
2015-07-06 12:37:42 -07:00
|
|
|
}
|
2015-09-01 17:06:31 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-23 10:20:52 -04:00
|
|
|
onTypingChange() {
|
|
|
|
|
this.updateTypingText(UserTypingStore.getUsersTyping(this.props.channelId, this.props.parentId));
|
2015-09-01 17:06:31 -07:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2016-03-23 10:20:52 -04:00
|
|
|
updateTypingText(typingUsers) {
|
2016-03-31 12:11:29 -04:00
|
|
|
let text = '';
|
|
|
|
|
let users = {};
|
|
|
|
|
let numUsers = 0;
|
|
|
|
|
if (typingUsers) {
|
|
|
|
|
users = Object.keys(typingUsers);
|
|
|
|
|
numUsers = users.length;
|
2015-10-25 02:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-31 12:11:29 -04:00
|
|
|
switch (numUsers) {
|
2015-10-25 02:00:55 +01:00
|
|
|
case 0:
|
2015-10-26 17:44:29 +01:00
|
|
|
text = '';
|
|
|
|
|
break;
|
2015-10-25 02:00:55 +01:00
|
|
|
case 1:
|
2016-02-01 17:23:45 -03:00
|
|
|
text = (
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
id='msg_typing.isTyping'
|
|
|
|
|
defaultMessage='{user} is typing...'
|
|
|
|
|
values={{
|
|
|
|
|
user: users[0]
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2015-10-26 17:44:29 +01:00
|
|
|
break;
|
2015-11-19 18:10:08 -05:00
|
|
|
default: {
|
2015-10-25 02:00:55 +01:00
|
|
|
const last = users.pop();
|
2016-02-01 17:23:45 -03:00
|
|
|
text = (
|
|
|
|
|
<FormattedMessage
|
|
|
|
|
id='msg_typing.areTyping'
|
|
|
|
|
defaultMessage='{users} and {last} are typing...'
|
2016-02-02 19:39:56 -03:00
|
|
|
values={{
|
|
|
|
|
users: (users.join(', ')),
|
2016-07-15 07:49:57 -04:00
|
|
|
last
|
2016-02-01 17:23:45 -03:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2015-10-26 17:44:29 +01:00
|
|
|
break;
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-11-19 18:10:08 -05:00
|
|
|
}
|
2015-10-26 17:44:29 +01:00
|
|
|
|
|
|
|
|
this.setState({text});
|
2015-09-01 17:06:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2015-06-14 23:53:32 -08:00
|
|
|
return (
|
2015-09-01 17:06:31 -07:00
|
|
|
<span className='msg-typing'>{this.state.text}</span>
|
2015-06-14 23:53:32 -08:00
|
|
|
);
|
|
|
|
|
}
|
2015-09-01 17:06:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MsgTyping.propTypes = {
|
|
|
|
|
channelId: React.PropTypes.string,
|
|
|
|
|
parentId: React.PropTypes.string
|
|
|
|
|
};
|
2016-02-01 17:23:45 -03:00
|
|
|
|
2016-03-23 10:20:52 -04:00
|
|
|
export default MsgTyping;
|