summaryrefslogtreecommitdiffstats
path: root/webapp/components/at_mention/at_mention.jsx
blob: 668222cc2fa01ab97e74e306168bf3101dfb1b3a (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
98
99
100
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import ProfilePopover from 'components/profile_popover.jsx';
import Pluggable from 'plugins/pluggable';
import {Client4} from 'mattermost-redux/client';

import React from 'react';
import PropTypes from 'prop-types';
import {OverlayTrigger} from 'react-bootstrap';

export default class AtMention extends React.PureComponent {
    static propTypes = {
        mentionName: PropTypes.string.isRequired,
        usersByUsername: PropTypes.object.isRequired,
        isRHS: PropTypes.bool,
        hasMention: PropTypes.bool
    };

    static defaultProps = {
        isRHS: false,
        hasMention: false
    }

    constructor(props) {
        super(props);

        this.hideProfilePopover = this.hideProfilePopover.bind(this);

        this.state = {
            user: this.getUserFromMentionName(props)
        };
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.mentionName !== this.props.mentionName || nextProps.usersByUsername !== this.props.usersByUsername) {
            this.setState({
                user: this.getUserFromMentionName(nextProps)
            });
        }
    }

    hideProfilePopover() {
        this.refs.overlay.hide();
    }

    getUserFromMentionName(props) {
        const usersByUsername = props.usersByUsername;
        let mentionName = props.mentionName;

        while (mentionName.length > 0) {
            if (usersByUsername[mentionName]) {
                return usersByUsername[mentionName];
            }

            // Repeatedly trim off trailing punctuation in case this is at the end of a sentence
            if ((/[._-]$/).test(mentionName)) {
                mentionName = mentionName.substring(0, mentionName.length - 1);
            } else {
                break;
            }
        }

        return '';
    }

    render() {
        if (!this.state.user) {
            return <span>{'@' + this.props.mentionName}</span>;
        }

        const user = this.state.user;
        const suffix = this.props.mentionName.substring(user.username.length);

        return (
            <span>
                <OverlayTrigger
                    ref='overlay'
                    trigger='click'
                    placement='right'
                    rootClose={true}
                    overlay={
                        <Pluggable>
                            <ProfilePopover
                                user={user}
                                src={Client4.getProfilePictureUrl(user.id, user.last_picture_update)}
                                hide={this.hideProfilePopover}
                                isRHS={this.props.isRHS}
                                hasMention={this.props.hasMention}
                            />
                        </Pluggable>
                    }
                >
                    <a className='mention-link'>{'@' + user.username}</a>
                </OverlayTrigger>
                {suffix}
            </span>
        );
    }
}