Files
mattermost/web/react/components/msg_typing.jsx

71 lines
1.9 KiB
React
Raw Normal View History

2015-06-14 23:53:32 -08:00
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var SocketStore = require('../stores/socket_store.jsx');
var UserStore = require('../stores/user_store.jsx');
2015-09-01 17:06:31 -07:00
export default class MsgTyping extends React.Component {
constructor(props) {
super(props);
this.timer = null;
this.lastTime = 0;
this.onChange = this.onChange.bind(this);
this.state = {
text: ''
};
}
componentDidMount() {
SocketStore.addChangeListener(this.onChange);
}
componentWillReceiveProps(newProps) {
if (this.props.channelId !== newProps.channelId) {
this.setState({text: ''});
}
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) {
if (msg.action === 'typing' &&
this.props.channelId === msg.channel_id &&
this.props.parentId === msg.props.parent_id) {
2015-06-14 23:53:32 -08:00
this.lastTime = new Date().getTime();
2015-09-01 17:06:31 -07:00
var username = 'Someone';
2015-06-14 23:53:32 -08:00
if (UserStore.hasProfile(msg.user_id)) {
username = UserStore.getProfile(msg.user_id).username;
}
2015-09-01 17:06:31 -07:00
this.setState({text: username + ' is typing...'});
2015-06-14 23:53:32 -08:00
if (!this.timer) {
2015-09-01 17:06:31 -07:00
this.timer = setInterval(function myTimer() {
if ((new Date().getTime() - this.lastTime) > 8000) {
this.setState({text: ''});
}
}.bind(this), 3000);
2015-06-14 23:53:32 -08:00
}
2015-09-01 17:06:31 -07:00
} else if (msg.action === 'posted' && msg.channel_id === this.props.channelId) {
this.setState({text: ''});
2015-06-14 23:53:32 -08:00
}
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
};