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

import ChannelStore from '../../stores/channel_store.jsx';
import Constants from '../../utils/constants.jsx';
import SuggestionStore from '../../stores/suggestion_store.jsx';

class SearchChannelSuggestion extends React.Component {
    render() {
        const {item, isSelection, onClick} = this.props;

        let className = 'search-autocomplete__item';
        if (isSelection) {
            className += ' selected';
        }

        return (
            <div
                onClick={onClick}
                className={className}
            >
                <i className='fa fa fa-plus-square'></i>{item.name}
            </div>
        );
    }
}

SearchChannelSuggestion.propTypes = {
    item: React.PropTypes.object.isRequired,
    isSelection: React.PropTypes.bool,
    onClick: React.PropTypes.func
};

export default class SearchChannelProvider {
    handlePretextChanged(suggestionId, pretext) {
        const captured = (/\b(?:in|channel):\s*(\S*)$/i).exec(pretext);
        if (captured) {
            const channelPrefix = captured[1];

            const channels = ChannelStore.getAll();
            const publicChannels = [];
            const privateChannels = [];

            for (const id of Object.keys(channels)) {
                const channel = channels[id];

                // don't show direct channels
                if (channel.type !== Constants.DM_CHANNEL && channel.name.startsWith(channelPrefix)) {
                    if (channel.type === Constants.OPEN_CHANNEL) {
                        publicChannels.push(channel);
                    } else {
                        privateChannels.push(channel);
                    }
                }
            }

            publicChannels.sort((a, b) => a.name.localeCompare(b.name));
            const publicChannelNames = publicChannels.map((channel) => channel.name);

            privateChannels.sort((a, b) => a.name.localeCompare(b.name));
            const privateChannelNames = privateChannels.map((channel) => channel.name);

            SuggestionStore.setMatchedPretext(suggestionId, channelPrefix);

            SuggestionStore.addSuggestions(suggestionId, publicChannelNames, publicChannels, SearchChannelSuggestion);
            SuggestionStore.addSuggestions(suggestionId, privateChannelNames, privateChannels, SearchChannelSuggestion);
        }
    }
}