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

import Suggestion from './suggestion.jsx';

import {autocompleteChannels} from 'actions/channel_actions.jsx';

import ChannelStore from 'stores/channel_store.jsx';

import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import {Constants, ActionTypes} from 'utils/constants.jsx';

import React from 'react';

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

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

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

export default class SearchChannelProvider {
    constructor() {
        this.timeoutId = '';
    }

    componentWillUnmount() {
        clearTimeout(this.timeoutId);
    }

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

            function autocomplete() {
                autocompleteChannels(
                    channelPrefix,
                    (data) => {
                        const publicChannels = data;

                        const localChannels = ChannelStore.getAll();
                        const privateChannels = [];

                        for (const id of Object.keys(localChannels)) {
                            const channel = localChannels[id];
                            if (channel.name.startsWith(channelPrefix) && channel.type === Constants.PRIVATE_CHANNEL) {
                                privateChannels.push(channel);
                            }
                        }

                        const filteredPublicChannels = [];
                        publicChannels.forEach((item) => {
                            if (item.name.startsWith(channelPrefix)) {
                                filteredPublicChannels.push(item);
                            }
                        });

                        privateChannels.sort((a, b) => a.name.localeCompare(b.name));
                        filteredPublicChannels.sort((a, b) => a.name.localeCompare(b.name));

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

                        AppDispatcher.handleServerAction({
                            type: ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
                            id: suggestionId,
                            matchedPretext: channelPrefix,
                            terms: channelNames,
                            items: channels,
                            component: SearchChannelSuggestion
                        });
                    }
                );
            }

            this.timeoutId = setTimeout(
                autocomplete.bind(this),
                Constants.AUTOCOMPLETE_TIMEOUT
            );
        }
    }
}