blob: 24cd48d39e6fbafbb8ebd1095d8a0a165a96191a (
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import TeamButton from './components/team_button.jsx';
import TeamStore from 'stores/team_store.jsx';
import UserStore from 'stores/user_store.jsx';
import * as AsyncClient from 'utils/async_client.jsx';
import * as Utils from 'utils/utils.jsx';
import $ from 'jquery';
import React from 'react';
import {FormattedMessage} from 'react-intl';
export default class TeamSidebar extends React.Component {
constructor(props) {
super(props);
this.getStateFromStores = this.getStateFromStores.bind(this);
this.onChange = this.onChange.bind(this);
this.handleResize = this.handleResize.bind(this);
this.setStyles = this.setStyles.bind(this);
this.state = this.getStateFromStores();
}
getStateFromStores() {
const teamMembers = TeamStore.getMyTeamMembers();
const currentTeamId = TeamStore.getCurrentId();
return {
teams: TeamStore.getAll(),
teamListings: TeamStore.getTeamListings(),
teamMembers,
currentTeamId,
show: teamMembers && teamMembers.length > 1
};
}
componentDidMount() {
window.addEventListener('resize', this.handleResize);
TeamStore.addChangeListener(this.onChange);
TeamStore.addUnreadChangeListener(this.onChange);
AsyncClient.getAllTeamListings();
this.setStyles();
}
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
TeamStore.removeChangeListener(this.onChange);
TeamStore.removeUnreadChangeListener(this.onChange);
}
componentDidUpdate(prevProps, prevState) {
$('.team-wrapper').perfectScrollbar();
// reset the scrollbar upon switching teams
if (this.state.currentTeam !== prevState.currentTeam) {
this.refs.container.scrollTop = 0;
$('.team-wrapper').perfectScrollbar('update');
}
}
onChange() {
this.setState(this.getStateFromStores());
this.setStyles();
}
handleResize() {
const teamMembers = this.state.teamMembers;
this.setState({show: teamMembers && teamMembers.length > 1});
this.setStyles();
}
setStyles() {
const root = document.querySelector('#root');
if (this.state.show) {
root.classList.add('multi-teams');
} else {
root.classList.remove('multi-teams');
}
}
render() {
if (!this.state.show) {
return null;
}
const myTeams = [];
const isSystemAdmin = Utils.isSystemAdmin(UserStore.getCurrentUser().roles);
const isAlreadyMember = new Map();
let moreTeams = false;
for (const index in this.state.teamMembers) {
if (this.state.teamMembers.hasOwnProperty(index)) {
const teamMember = this.state.teamMembers[index];
const teamId = teamMember.team_id;
myTeams.push(Object.assign({
unread: teamMember.msg_count > 0,
mentions: teamMember.mention_count
}, this.state.teams[teamId]));
isAlreadyMember[teamId] = true;
}
}
for (const id in this.state.teamListings) {
if (this.state.teamListings.hasOwnProperty(id) && !isAlreadyMember[id]) {
moreTeams = true;
break;
}
}
const teams = myTeams.
sort((a, b) => a.display_name.localeCompare(b.display_name)).
map((team) => {
return (
<TeamButton
key={'switch_team_' + team.name}
url={`/${team.name}`}
tip={team.display_name}
active={team.id === this.state.currentTeamId}
displayName={team.display_name}
unread={team.unread}
mentions={team.mentions}
/>
);
});
if (moreTeams) {
teams.push(
<TeamButton
btnClass='team-btn__add'
key='more_teams'
url='/select_team'
tip={
<FormattedMessage
id='team_sidebar.join'
defaultMessage='Other teams you can join.'
/>
}
content={<i className='fa fa-plus'/>}
/>
);
} else if (global.window.mm_config.EnableTeamCreation === 'true' || isSystemAdmin) {
teams.push(
<TeamButton
btnClass='team-btn__add'
key='more_teams'
url='/create_team'
tip={
<FormattedMessage
id='navbar_dropdown.create'
defaultMessage='Create a New Team'
/>
}
content={<i className='fa fa-plus'/>}
/>
);
}
return (
<div className='team-sidebar'>
<div className='team-wrapper'>
{teams}
</div>
</div>
);
}
}
|