summaryrefslogtreecommitdiffstats
path: root/webapp/components/suggestion/switch_channel_provider.jsx
blob: e0c862e7e976b97ea6ffd33265ee8c50e2cdd88b (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
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';

import ChannelStore from 'stores/channel_store.jsx';
import UserStore from 'stores/user_store.jsx';
import SuggestionStore from 'stores/suggestion_store.jsx';
import Suggestion from './suggestion.jsx';
import Constants from 'utils/constants.jsx';
import StatusIcon from 'components/status_icon.jsx';
import * as Utils from 'utils/utils.jsx';

class SwitchChannelSuggestion extends Suggestion {
    render() {
        const {item, isSelection} = this.props;

        let className = 'mentions__name';
        if (isSelection) {
            className += ' suggestion--selected';
        }

        let displayName = '';
        if (item.type === Constants.DM_CHANNEL) {
            displayName = item.display_name + ' ' + Utils.localizeMessage('channel_switch_modal.dm', '(Direct Message)');
        } else {
            displayName = item.display_name + ' (' + item.name + ')';
        }

        let icon = null;
        if (item.type === Constants.OPEN_CHANNEL) {
            icon = <div className='status'><i className='fa fa-globe'></i></div>;
        } else if (item.type === Constants.PRIVATE_CHANNEL) {
            icon = <div className='status'><i className='fa fa-lock'></i></div>;
        } else {
            icon = <StatusIcon status={item.status}/>;
        }

        return (
            <div
                onClick={this.handleClick}
                className={className}
            >
            {icon}
            {displayName}
            </div>
        );
    }
}

export default class SwitchChannelProvider {
    handlePretextChanged(suggestionId, channelPrefix) {
        if (channelPrefix) {
            const allChannels = ChannelStore.getAll();
            const channels = [];

            for (const id of Object.keys(allChannels)) {
                const channel = allChannels[id];
                if (channel.display_name.toLowerCase().startsWith(channelPrefix.toLowerCase())) {
                    channels.push(channel);
                } else if (channel.type === Constants.DM_CHANNEL && Utils.getDirectTeammate(channel.id).username.startsWith(channelPrefix.toLowerCase())) {
                    // New channel to not modify existing channel
                    const otherUser = Utils.getDirectTeammate(channel.id);
                    const newChannel = {
                        display_name: otherUser.username,
                        name: otherUser.username + ' ' + Utils.localizeMessage('channel_switch_modal.dm', '(Direct Message)'),
                        type: Constants.DM_CHANNEL,
                        status: UserStore.getStatus(otherUser.id) || 'offline'
                    };
                    channels.push(newChannel);
                }
            }

            channels.sort((a, b) => {
                if (a.display_name === b.display_name) {
                    if (a.type !== Constants.DM_CHANNEL && b.type === Constants.DM_CHANNEL) {
                        return -1;
                    } else if (a.type === Constants.DM_CHANNEL && b.type !== Constants.DM_CHANNEL) {
                        return 1;
                    }
                    return a.name.localeCompare(b.name);
                }
                return a.display_name.localeCompare(b.display_name);
            });

            const channelNames = channels.map((channel) => channel.name);

            SuggestionStore.addSuggestions(suggestionId, channelNames, channels, SwitchChannelSuggestion, channelPrefix);
        }
    }
}