summaryrefslogtreecommitdiffstats
path: root/webapp/components/msg_typing.jsx
blob: 503af1dc7ed6c7021492ca24bdf9e9aa8ce72eee (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import UserTypingStore from 'stores/user_typing_store.jsx';

import {FormattedMessage} from 'react-intl';

import React from 'react';

class MsgTyping extends React.Component {
    constructor(props) {
        super(props);

        this.onTypingChange = this.onTypingChange.bind(this);
        this.updateTypingText = this.updateTypingText.bind(this);
        this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this);

        this.state = {
            text: ''
        };
    }

    componentWillMount() {
        UserTypingStore.addChangeListener(this.onTypingChange);
        this.onTypingChange();
    }

    componentWillUnmount() {
        UserTypingStore.removeChangeListener(this.onTypingChange);
    }

    componentWillReceiveProps(nextProps) {
        if (this.props.channelId !== nextProps.channelId) {
            this.updateTypingText(UserTypingStore.getUsersTyping(nextProps.channelId, nextProps.parentId));
        }
    }

    onTypingChange() {
        this.updateTypingText(UserTypingStore.getUsersTyping(this.props.channelId, this.props.parentId));
    }

    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(', ')),
                        last
                    }}
                />
            );
            break;
        }
        }

        this.setState({text});
    }

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

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

export default MsgTyping;