Files
mattermost/webapp/components/msg_typing.jsx

98 lines
2.5 KiB
React
Raw Normal View History

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
import UserTypingStore from 'stores/user_typing_store.jsx';
import {FormattedMessage} from 'react-intl';
2016-03-14 08:50:46 -04:00
import React from 'react';
class MsgTyping extends React.Component {
2015-09-01 17:06:31 -07:00
constructor(props) {
super(props);
this.onTypingChange = this.onTypingChange.bind(this);
this.updateTypingText = this.updateTypingText.bind(this);
this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this);
2015-09-01 17:06:31 -07:00
this.state = {
text: ''
};
}
componentWillMount() {
UserTypingStore.addChangeListener(this.onTypingChange);
this.onTypingChange();
}
componentWillUnmount() {
UserTypingStore.removeChangeListener(this.onTypingChange);
2015-09-01 17:06:31 -07:00
}
componentWillReceiveProps(nextProps) {
if (this.props.channelId !== nextProps.channelId) {
this.updateTypingText(UserTypingStore.getUsersTyping(nextProps.channelId, nextProps.parentId));
}
2015-09-01 17:06:31 -07: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
updateTypingText(typingUsers) {
let text = '';
let users = {};
let numUsers = 0;
if (typingUsers) {
users = Object.keys(typingUsers);
numUsers = users.length;
}
switch (numUsers) {
case 0:
text = '';
break;
case 1:
text = (
<FormattedMessage
id='msg_typing.isTyping'
defaultMessage='{user} is typing...'
values={{
user: users[0]
}}
/>
);
break;
default: {
const last = users.pop();
text = (
<FormattedMessage
id='msg_typing.areTyping'
defaultMessage='{users} and {last} are typing...'
values={{
users: (users.join(', ')),
2016-07-15 07:49:57 -04:00
last
}}
/>
);
break;
2015-06-14 23:53:32 -08: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
};
export default MsgTyping;