<commit_before>// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

const SocketStore = require('../stores/socket_store.jsx');
const UserStore = require('../stores/user_store.jsx');

const Constants = require('../utils/constants.jsx');
const SocketEvents = Constants.SocketEvents;

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

        this.onChange = this.onChange.bind(this);
        this.getTypingText = this.getTypingText.bind(this);
        this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this);

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

    componentDidMount() {
        SocketStore.addChangeListener(this.onChange);
    }

    componentWillReceiveProps(newProps) {
        if (this.props.channelId !== newProps.channelId) {
            this.setState({text: this.getTypingText()});
        }
    }

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

    onChange(msg) {
        let username = 'Someone';
        if (msg.action === SocketEvents.TYPING &&
                this.props.channelId === msg.channel_id &&
                this.props.parentId === msg.props.parent_id) {
            if (UserStore.hasProfile(msg.user_id)) {
                username = UserStore.getProfile(msg.user_id).username;
            }

            if (this.typingUsers[username]) {
                clearTimeout(this.typingUsers[username]);
            }

            this.typingUsers[username] = setTimeout(function myTimer(user) {
                delete this.typingUsers[user];
                this.setState({text: this.getTypingText()});
            }.bind(this, username), Constants.UPDATE_TYPING_MS);

            this.setState({text: this.getTypingText()});
        } else if (msg.action === SocketEvents.POSTED && msg.channel_id === this.props.channelId) {
            if (UserStore.hasProfile(msg.user_id)) {
                username = UserStore.getProfile(msg.user_id).username;
            }
            clearTimeout(this.typingUsers[username]);
            delete this.typingUsers[username];
            this.setState({text: this.getTypingText()});
        }
    }

    getTypingText() {
        let users = Object.keys(this.typingUsers);
        switch (users.length) {
        case 0:
            return '';
        case 1:
            return users[0] + ' is typing...';
        default:
            const last = users.pop();
            return users.join(', ') + ' and ' + last + ' are typing...';
        }
    }

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

MsgTyping.propTypes = {
    channelId: React.PropTypes.string,
    parentId: React.PropTypes.string
};<commit_msg>rename getTypingText to updateTypingText and set component's state in it<commit_after>// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

const SocketStore = require('../stores/socket_store.jsx');
const UserStore = require('../stores/user_store.jsx');

const Constants = require('../utils/constants.jsx');
const SocketEvents = Constants.SocketEvents;

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

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

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

    componentDidMount() {
        SocketStore.addChangeListener(this.onChange);
    }

    componentWillReceiveProps(newProps) {
        if (this.props.channelId !== newProps.channelId) {
            this.updateTypingText();
        }
    }

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

    onChange(msg) {
        let username = 'Someone';
        if (msg.action === SocketEvents.TYPING &&
                this.props.channelId === msg.channel_id &&
                this.props.parentId === msg.props.parent_id) {
            if (UserStore.hasProfile(msg.user_id)) {
                username = UserStore.getProfile(msg.user_id).username;
            }

            if (this.typingUsers[username]) {
                clearTimeout(this.typingUsers[username]);
            }

            this.typingUsers[username] = setTimeout(function myTimer(user) {
                delete this.typingUsers[user];
                this.updateTypingText();
            }.bind(this, username), Constants.UPDATE_TYPING_MS);

            this.updateTypingText();
        } else if (msg.action === SocketEvents.POSTED && msg.channel_id === this.props.channelId) {
            if (UserStore.hasProfile(msg.user_id)) {
                username = UserStore.getProfile(msg.user_id).username;
            }
            clearTimeout(this.typingUsers[username]);
            delete this.typingUsers[username];
            this.updateTypingText();
        }
    }

    updateTypingText() {
        const users = Object.keys(this.typingUsers);
        let text = '';
        switch (users.length) {
        case 0:
            text = '';
            break;
        case 1:
            text = users[0] + ' is typing...';
            break;
        default:
            const last = users.pop();
            text = users.join(', ') + ' and ' + last + ' are typing...';
            break;
        }

        this.setState({text});
    }

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

MsgTyping.propTypes = {
    channelId: React.PropTypes.string,
    parentId: React.PropTypes.string
};
{'fraction_non_alphanumeric': 0.12713894592744696, 'fraction_numerical': 0.0023956194387405884, 'mean_word_length': 2.0909571655208885, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3935056', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1647, 'n_words': 275}