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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import Suggestion from './suggestion.jsx';
import Provider from './provider.jsx';
import ChannelStore from 'stores/channel_store.jsx';
import UserStore from 'stores/user_store.jsx';
import {autocompleteUsers} from 'actions/user_actions.jsx';
import Client from 'client/web_client.jsx';
import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import {Constants, ActionTypes} from 'utils/constants.jsx';
import * as Utils from 'utils/utils.jsx';
import React from 'react';
class SwitchChannelSuggestion extends Suggestion {
render() {
const {item, isSelection} = this.props;
let className = 'mentions__name';
if (isSelection) {
className += ' suggestion--selected';
}
const displayName = item.display_name;
let icon = null;
if (item.type === Constants.OPEN_CHANNEL) {
icon = <div className='status'><i className='fa fa-globe'/></div>;
} else if (item.type === Constants.PRIVATE_CHANNEL) {
icon = <div className='status'><i className='fa fa-lock'/></div>;
} else {
icon = (
<div className='pull-left'>
<img
className='mention__image'
src={Client.getUsersRoute() + '/' + item.id + '/image?time=' + item.update_at}
/>
</div>
);
}
return (
<div
onClick={this.handleClick}
className={className}
>
{icon}
{displayName}
</div>
);
}
}
export default class SwitchChannelProvider extends Provider {
handlePretextChanged(suggestionId, channelPrefix) {
if (channelPrefix) {
this.startNewRequest(channelPrefix);
const allChannels = ChannelStore.getAll();
const channels = [];
autocompleteUsers(
channelPrefix,
(users) => {
if (this.shouldCancelDispatch(channelPrefix)) {
return;
}
const currentId = UserStore.getCurrentId();
for (const id of Object.keys(allChannels)) {
const channel = allChannels[id];
if (channel.display_name.toLowerCase().indexOf(channelPrefix.toLowerCase()) !== -1) {
channels.push(channel);
}
}
const userMap = {};
for (let i = 0; i < users.length; i++) {
const user = users[i];
let displayName = `@${user.username} `;
if (user.id === currentId) {
continue;
}
if ((user.first_name || user.last_name) && user.nickname) {
displayName += `- ${Utils.getFullName(user)} (${user.nickname})`;
} else if (user.nickname) {
displayName += `- (${user.nickname})`;
} else if (user.first_name || user.last_name) {
displayName += `- ${Utils.getFullName(user)}`;
}
const newChannel = {
display_name: displayName,
name: user.username,
id: user.id,
update_at: user.update_at,
type: Constants.DM_CHANNEL
};
channels.push(newChannel);
userMap[user.id] = user;
}
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);
AppDispatcher.handleServerAction({
type: ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
id: suggestionId,
matchedPretext: channelPrefix,
terms: channelNames,
items: channels,
component: SwitchChannelSuggestion
});
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_PROFILES,
profiles: userMap
});
}
);
}
}
}
|