summaryrefslogtreecommitdiffstats
path: root/web/react/components/msg_typing.jsx
blob: d9823c3cf37e2ab67a06b9f6e4c35dab8d4b770e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 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');

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: ''});
        }
    }

    componentWillUnmount() {
        SocketStore.removeChangeListener(this.onChange);
    }

    onChange(msg) {
        if (msg.action === 'typing' &&
            this.props.channelId === msg.channel_id &&
            this.props.parentId === msg.props.parent_id) {
            this.lastTime = new Date().getTime();

            var username = 'Someone';
            if (UserStore.hasProfile(msg.user_id)) {
                username = UserStore.getProfile(msg.user_id).username;
            }

            this.setState({text: username + ' is typing...'});

            if (!this.timer) {
                this.timer = setInterval(function myTimer() {
                    if ((new Date().getTime() - this.lastTime) > 8000) {
                        this.setState({text: ''});
                    }
                }.bind(this), 3000);
            }
        } else if (msg.action === 'posted' && msg.channel_id === this.props.channelId) {
            this.setState({text: ''});
        }
    }

    render() {
        return (
            <span className='msg-typing'>{this.state.text}</span>
        );
    }
}

MsgTyping.propTypes = {
    channelId: React.PropTypes.string,
    parentId: React.PropTypes.string
};