summaryrefslogtreecommitdiffstats
path: root/webapp/components/suggestion/switch_team_provider.jsx
blob: ff2a8f24b64610001a6ef0a543c8ad0596e62051 (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) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import Suggestion from './suggestion.jsx';
import Provider from './provider.jsx';

import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import {ActionTypes} from 'utils/constants.jsx';
import LocalizationStore from 'stores/localization_store.jsx';

import React from 'react';

// Redux actions
import store from 'stores/redux_store.jsx';
const getState = store.getState;

import * as Selectors from 'mattermost-redux/selectors/entities/teams';

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

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

        return (
            <div
                onClick={this.handleClick}
                className={className}
            >
                <div className='status'><i className='fa fa-group'/></div>
                {item.display_name}
            </div>
        );
    }
}

let prefix = '';

function quickSwitchSorter(a, b) {
    const aDisplayName = a.display_name.toLowerCase();
    const bDisplayName = b.display_name.toLowerCase();
    const aStartsWith = aDisplayName.startsWith(prefix);
    const bStartsWith = bDisplayName.startsWith(prefix);

    if (aStartsWith && bStartsWith) {
        const locale = LocalizationStore.getLocale();

        if (aDisplayName !== bDisplayName) {
            return aDisplayName.localeCompare(bDisplayName, locale, {numeric: true});
        }

        return a.name.localeCompare(b.name, locale, {numeric: true});
    } else if (aStartsWith) {
        return -1;
    }

    return 1;
}

export default class SwitchTeamProvider extends Provider {
    handlePretextChanged(suggestionId, teamPrefix) {
        if (teamPrefix) {
            prefix = teamPrefix;
            this.startNewRequest(suggestionId, teamPrefix);

            const allTeams = Selectors.getMyTeams(getState());

            const teams = allTeams.filter((team) => {
                return team.display_name.toLowerCase().indexOf(teamPrefix) !== -1 ||
                    team.name.indexOf(teamPrefix) !== -1;
            });

            const teamNames = teams.
                sort(quickSwitchSorter).
                map((team) => team.name);

            setTimeout(() => {
                AppDispatcher.handleServerAction({
                    type: ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
                    id: suggestionId,
                    matchedPretext: teamPrefix,
                    terms: teamNames,
                    items: teams,
                    component: SwitchTeamSuggestion
                });
            }, 0);

            return true;
        }

        return false;
    }
}