2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2015-11-19 21:12:56 -05:00
|
|
|
import SocketStore from '../stores/socket_store.jsx';
|
|
|
|
|
import UserStore from '../stores/user_store.jsx';
|
2015-10-15 10:44:04 -04:00
|
|
|
|
2015-11-19 21:12:56 -05:00
|
|
|
import Constants from '../utils/constants.jsx';
|
2015-10-15 10:44:04 -04:00
|
|
|
const SocketEvents = Constants.SocketEvents;
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-09-01 17:06:31 -07:00
|
|
|
export default class MsgTyping extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.onChange = this.onChange.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
|
|
|
|
2015-10-25 02:00:55 +01:00
|
|
|
this.typingUsers = {};
|
2015-09-01 17:06:31 -07:00
|
|
|
this.state = {
|
|
|
|
|
text: ''
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
SocketStore.addChangeListener(this.onChange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
|
if (this.props.channelId !== newProps.channelId) {
|
2015-10-26 17:44:29 +01:00
|
|
|
this.updateTypingText();
|
2015-07-06 12:37:42 -07:00
|
|
|
}
|
2015-09-01 17:06:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
SocketStore.removeChangeListener(this.onChange);
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-09-01 17:06:31 -07:00
|
|
|
onChange(msg) {
|
2015-10-25 02:00:55 +01:00
|
|
|
let username = 'Someone';
|
2015-10-15 10:44:04 -04:00
|
|
|
if (msg.action === SocketEvents.TYPING &&
|
2015-10-15 10:57:09 -04:00
|
|
|
this.props.channelId === msg.channel_id &&
|
|
|
|
|
this.props.parentId === msg.props.parent_id) {
|
2015-06-14 23:53:32 -08:00
|
|
|
if (UserStore.hasProfile(msg.user_id)) {
|
|
|
|
|
username = UserStore.getProfile(msg.user_id).username;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-25 02:00:55 +01:00
|
|
|
if (this.typingUsers[username]) {
|
|
|
|
|
clearTimeout(this.typingUsers[username]);
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-10-25 02:00:55 +01:00
|
|
|
|
|
|
|
|
this.typingUsers[username] = setTimeout(function myTimer(user) {
|
|
|
|
|
delete this.typingUsers[user];
|
2015-10-26 17:44:29 +01:00
|
|
|
this.updateTypingText();
|
2015-10-25 02:00:55 +01:00
|
|
|
}.bind(this, username), Constants.UPDATE_TYPING_MS);
|
|
|
|
|
|
2015-10-26 17:44:29 +01:00
|
|
|
this.updateTypingText();
|
2015-10-15 10:44:04 -04:00
|
|
|
} else if (msg.action === SocketEvents.POSTED && msg.channel_id === this.props.channelId) {
|
2015-10-25 02:00:55 +01:00
|
|
|
if (UserStore.hasProfile(msg.user_id)) {
|
|
|
|
|
username = UserStore.getProfile(msg.user_id).username;
|
|
|
|
|
}
|
|
|
|
|
clearTimeout(this.typingUsers[username]);
|
|
|
|
|
delete this.typingUsers[username];
|
2015-10-26 17:44:29 +01:00
|
|
|
this.updateTypingText();
|
2015-10-25 02:00:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-26 17:44:29 +01:00
|
|
|
updateTypingText() {
|
|
|
|
|
const users = Object.keys(this.typingUsers);
|
|
|
|
|
let text = '';
|
2015-10-25 02:00:55 +01:00
|
|
|
switch (users.length) {
|
|
|
|
|
case 0:
|
2015-10-26 17:44:29 +01:00
|
|
|
text = '';
|
|
|
|
|
break;
|
2015-10-25 02:00:55 +01:00
|
|
|
case 1:
|
2015-10-26 17:44:29 +01:00
|
|
|
text = users[0] + ' is typing...';
|
|
|
|
|
break;
|
2015-11-19 18:10:08 -05:00
|
|
|
default: {
|
2015-10-25 02:00:55 +01:00
|
|
|
const last = users.pop();
|
2015-10-26 17:44:29 +01:00
|
|
|
text = users.join(', ') + ' and ' + last + ' are typing...';
|
|
|
|
|
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
|
|
|
|
|
};
|