Files
mattermost/web/react/components/sidebar.jsx

633 lines
22 KiB
React
Raw Normal View History

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
const AsyncClient = require('../utils/async_client.jsx');
const ChannelStore = require('../stores/channel_store.jsx');
const Client = require('../utils/client.jsx');
const Constants = require('../utils/constants.jsx');
const PreferenceStore = require('../stores/preference_store.jsx');
const NewChannelFlow = require('./new_channel_flow.jsx');
const MoreDirectChannels = require('./more_direct_channels.jsx');
const SearchBox = require('./search_bar.jsx');
const SidebarHeader = require('./sidebar_header.jsx');
const TeamStore = require('../stores/team_store.jsx');
const UnreadChannelIndicator = require('./unread_channel_indicator.jsx');
const UserStore = require('../stores/user_store.jsx');
const Utils = require('../utils/utils.jsx');
2015-10-14 17:04:13 +05:00
const Tooltip = ReactBootstrap.Tooltip;
const OverlayTrigger = ReactBootstrap.OverlayTrigger;
2015-06-14 23:53:32 -08:00
export default class Sidebar extends React.Component {
constructor(props) {
super(props);
2015-06-14 23:53:32 -08:00
this.badgesActive = false;
this.firstUnreadChannel = null;
this.lastUnreadChannel = null;
2015-06-14 23:53:32 -08:00
this.getStateFromStores = this.getStateFromStores.bind(this);
this.onChange = this.onChange.bind(this);
this.onScroll = this.onScroll.bind(this);
this.updateUnreadIndicators = this.updateUnreadIndicators.bind(this);
this.handleLeaveDirectChannel = this.handleLeaveDirectChannel.bind(this);
this.updateScrollbar = this.updateScrollbar.bind(this);
this.handleResize = this.handleResize.bind(this);
this.showNewChannelModal = this.showNewChannelModal.bind(this);
this.hideNewChannelModal = this.hideNewChannelModal.bind(this);
this.showMoreDirectChannelsModal = this.showMoreDirectChannelsModal.bind(this);
this.hideMoreDirectChannelsModal = this.hideMoreDirectChannelsModal.bind(this);
this.createChannelElement = this.createChannelElement.bind(this);
2015-10-25 15:12:36 +01:00
this.updateTitle = this.updateTitle.bind(this);
this.setUnreadCountPerChannel = this.setUnreadCountPerChannel.bind(this);
2015-10-25 15:12:36 +01:00
this.getUnreadCount = this.getUnreadCount.bind(this);
2015-06-14 23:53:32 -08:00
this.isLeaving = new Map();
const state = this.getStateFromStores();
state.newChannelModalType = '';
2015-10-16 10:15:52 -04:00
state.showDirectChannelsModal = false;
state.loadingDMChannel = -1;
state.windowWidth = Utils.windowWidth();
this.state = state;
this.unreadCountPerChannel = {};
this.setUnreadCountPerChannel();
}
setUnreadCountPerChannel() {
2015-10-25 15:12:36 +01:00
const channels = ChannelStore.getAll();
const members = ChannelStore.getAllMembers();
const channelUnreadCounts = {};
2015-10-25 15:12:36 +01:00
channels.forEach((ch) => {
const chMember = members[ch.id];
let chMentionCount = chMember.mention_count;
let chUnreadCount = ch.total_msg_count - chMember.msg_count - chMentionCount;
if (ch.type === 'D') {
chMentionCount = chUnreadCount;
chUnreadCount = 0;
}
channelUnreadCounts[ch.id] = {msgs: chUnreadCount, mentions: chMentionCount};
2015-10-25 15:12:36 +01:00
});
this.unreadCountPerChannel = channelUnreadCounts;
2015-10-25 15:12:36 +01:00
}
getUnreadCount(channelId) {
let mentions = 0;
let msgs = 0;
if (channelId) {
return this.unreadCountPerChannel[channelId] ? this.unreadCountPerChannel[channelId] : {msgs, mentions};
2015-10-25 15:12:36 +01:00
}
Object.keys(this.unreadCountPerChannel).forEach((chId) => {
msgs += this.unreadCountPerChannel[chId].msgs;
mentions += this.unreadCountPerChannel[chId].mentions;
2015-10-25 15:12:36 +01:00
});
return {msgs, mentions};
}
getStateFromStores() {
const members = ChannelStore.getAllMembers();
var teamMemberMap = UserStore.getActiveOnlyProfiles();
var currentId = ChannelStore.getCurrentId();
const currentUserId = UserStore.getCurrentId();
var teammates = [];
for (var id in teamMemberMap) {
if (id === currentUserId) {
continue;
}
teammates.push(teamMemberMap[id]);
2015-06-14 23:53:32 -08:00
}
2015-10-13 11:52:17 -04:00
const preferences = PreferenceStore.getPreferences(Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW);
var visibleDirectChannels = [];
var hiddenDirectChannelCount = 0;
for (var i = 0; i < teammates.length; i++) {
const teammate = teammates[i];
2015-06-14 23:53:32 -08:00
if (teammate.id === currentUserId) {
continue;
}
2015-06-14 23:53:32 -08:00
const channelName = Utils.getDirectChannelName(currentUserId, teammate.id);
2015-06-14 23:53:32 -08:00
let forceShow = false;
let channel = ChannelStore.getByName(channelName);
2015-10-07 13:07:59 -04:00
if (channel) {
const member = members[channel.id];
const msgCount = channel.total_msg_count - member.msg_count;
// always show a channel if either it is the current one or if it is unread, but it is not currently being left
forceShow = (currentId === channel.id || msgCount > 0) && !this.isLeaving.get(channel.id);
} else {
channel = {};
channel.fake = true;
channel.name = channelName;
channel.last_post_at = 0;
channel.total_msg_count = 0;
channel.type = 'D';
}
channel.display_name = Utils.displayUsername(teammate.id);
channel.teammate_id = teammate.id;
channel.status = UserStore.getStatus(teammate.id);
2015-10-13 11:52:17 -04:00
if (preferences.some((preference) => (preference.name === teammate.id && preference.value !== 'false'))) {
visibleDirectChannels.push(channel);
} else if (forceShow) {
// make sure that unread direct channels are visible
2015-10-13 11:52:17 -04:00
const preference = PreferenceStore.setPreference(Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, teammate.id, 'true');
AsyncClient.savePreferences([preference]);
visibleDirectChannels.push(channel);
} else {
hiddenDirectChannelCount += 1;
2015-06-14 23:53:32 -08:00
}
}
visibleDirectChannels.sort(this.sortChannelsByDisplayName);
2015-06-14 23:53:32 -08:00
return {
activeId: currentId,
channels: ChannelStore.getAll(),
2015-10-07 13:07:59 -04:00
members,
visibleDirectChannels,
hiddenDirectChannelCount
};
}
componentDidMount() {
ChannelStore.addChangeListener(this.onChange);
UserStore.addChangeListener(this.onChange);
UserStore.addStatusesChangeListener(this.onChange);
TeamStore.addChangeListener(this.onChange);
PreferenceStore.addChangeListener(this.onChange);
2015-06-14 23:53:32 -08:00
this.updateTitle();
this.updateUnreadIndicators();
this.updateScrollbar();
window.addEventListener('resize', this.handleResize);
}
shouldComponentUpdate(nextProps, nextState) {
if (!Utils.areStatesEqual(nextState, this.state)) {
return true;
}
return false;
}
componentDidUpdate() {
2015-06-14 23:53:32 -08:00
this.updateTitle();
this.updateUnreadIndicators();
this.updateScrollbar();
}
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
ChannelStore.removeChangeListener(this.onChange);
UserStore.removeChangeListener(this.onChange);
UserStore.removeStatusesChangeListener(this.onChange);
TeamStore.removeChangeListener(this.onChange);
PreferenceStore.removeChangeListener(this.onChange);
}
handleResize() {
this.setState({
windowWidth: Utils.windowWidth(),
windowHeight: Utils.windowHeight()
});
}
updateScrollbar() {
if (this.state.windowWidth > 768) {
$('.nav-pills__container').perfectScrollbar();
$('.nav-pills__container').perfectScrollbar('update');
}
}
onChange() {
var newState = this.getStateFromStores();
if (!Utils.areStatesEqual(newState, this.state)) {
2015-06-14 23:53:32 -08:00
this.setState(newState);
}
}
updateTitle() {
const channel = ChannelStore.getCurrent();
2015-06-14 23:53:32 -08:00
if (channel) {
let currentSiteName = '';
2015-10-16 09:10:54 -07:00
if (global.window.mm_config.SiteName != null) {
currentSiteName = global.window.mm_config.SiteName;
}
let currentChannelName = channel.display_name;
2015-06-14 23:53:32 -08:00
if (channel.type === 'D') {
currentChannelName = Utils.getDirectTeammate(channel.id).username;
2015-06-14 23:53:32 -08:00
}
2015-10-25 15:12:36 +01:00
const unread = this.getUnreadCount();
const mentionTitle = unread.mentions > 0 ? '(' + unread.mentions + ') ' : '';
const unreadTitle = unread.msgs > 0 ? '* ' : '';
2015-10-27 15:48:51 -04:00
document.title = mentionTitle + unreadTitle + currentChannelName + ' - ' + TeamStore.getCurrent().display_name + ' ' + currentSiteName;
2015-06-14 23:53:32 -08:00
}
}
onScroll() {
this.updateUnreadIndicators();
}
updateUnreadIndicators() {
2015-10-15 12:07:06 -04:00
const container = $(ReactDOM.findDOMNode(this.refs.container));
var showTopUnread = false;
var showBottomUnread = false;
if (this.firstUnreadChannel) {
2015-10-15 12:07:06 -04:00
var firstUnreadElement = $(ReactDOM.findDOMNode(this.refs[this.firstUnreadChannel]));
if (firstUnreadElement.position().top + firstUnreadElement.height() < 0) {
showTopUnread = true;
}
}
if (this.lastUnreadChannel) {
2015-10-15 12:07:06 -04:00
var lastUnreadElement = $(ReactDOM.findDOMNode(this.refs[this.lastUnreadChannel]));
if (lastUnreadElement.position().top > container.height()) {
showBottomUnread = true;
}
}
this.setState({
showTopUnread,
showBottomUnread
});
}
handleLeaveDirectChannel(channel) {
if (!this.isLeaving.get(channel.id)) {
this.isLeaving.set(channel.id, true);
2015-10-13 11:52:17 -04:00
const preference = PreferenceStore.setPreference(Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, channel.teammate_id, 'false');
2015-10-13 15:18:01 -04:00
// bypass AsyncClient since we've already saved the updated preferences
Client.savePreferences(
[preference],
() => {
this.isLeaving.set(channel.id, false);
},
() => {
this.isLeaving.set(channel.id, false);
}
);
this.setState(this.getStateFromStores());
}
if (channel.id === this.state.activeId) {
Utils.switchChannel(ChannelStore.getByName(Constants.DEFAULT_CHANNEL));
}
}
sortChannelsByDisplayName(a, b) {
return a.display_name.localeCompare(b.display_name);
}
showNewChannelModal(type) {
this.setState({newChannelModalType: type});
}
hideNewChannelModal() {
this.setState({newChannelModalType: ''});
}
showMoreDirectChannelsModal() {
this.setState({showDirectChannelsModal: true});
}
hideMoreDirectChannelsModal() {
this.setState({showDirectChannelsModal: false});
}
createChannelElement(channel, index, arr, handleClose) {
2015-06-14 23:53:32 -08:00
var members = this.state.members;
2015-08-11 12:20:49 -04:00
var activeId = this.state.activeId;
var channelMember = members[channel.id];
2015-10-25 15:12:36 +01:00
var unreadCount = this.getUnreadCount(channel.id);
var msgCount;
2015-06-14 23:53:32 -08:00
var linkClass = '';
if (channel.id === activeId) {
linkClass = 'active';
}
2015-06-14 23:53:32 -08:00
let rowClass = 'sidebar-channel';
var unread = false;
if (channelMember) {
2015-10-25 15:12:36 +01:00
msgCount = unreadCount.msgs + unreadCount.mentions;
unread = (msgCount > 0 && channelMember.notify_props.mark_unread !== 'mention') || channelMember.mention_count > 0;
}
2015-06-14 23:53:32 -08:00
if (unread) {
rowClass += ' unread-title';
if (channel.id !== activeId) {
if (!this.firstUnreadChannel) {
this.firstUnreadChannel = channel.name;
}
this.lastUnreadChannel = channel.name;
2015-06-14 23:53:32 -08:00
}
}
2015-06-14 23:53:32 -08:00
var badge = null;
if (channelMember) {
2015-10-25 15:12:36 +01:00
if (unreadCount.mentions) {
badge = <span className='badge pull-right small'>{unreadCount.mentions}</span>;
this.badgesActive = true;
2015-06-14 23:53:32 -08:00
}
} else if (this.state.loadingDMChannel === index && channel.type === 'D') {
badge = (
<img
className='channel-loading-gif pull-right'
src='/static/images/load.gif'
/>
);
}
2015-06-14 23:53:32 -08:00
2015-09-16 20:32:10 +05:00
if (msgCount > 0) {
rowClass += ' has-badge';
2015-09-16 20:32:10 +05:00
}
// set up status icon for direct message channels
var status = null;
if (channel.type === 'D') {
var statusIcon = '';
if (channel.status === 'online') {
statusIcon = Constants.ONLINE_ICON_SVG;
} else if (channel.status === 'away') {
statusIcon = Constants.ONLINE_ICON_SVG;
} else {
statusIcon = Constants.OFFLINE_ICON_SVG;
}
status = (
<span
className='status'
dangerouslySetInnerHTML={{__html: statusIcon}}
/>
);
}
// set up click handler to switch channels (or create a new channel for non-existant ones)
var handleClick = null;
var href = '#';
var teamURL = TeamStore.getCurrentTeamUrl();
if (!channel.fake) {
handleClick = function clickHandler(e) {
2015-10-07 13:07:59 -04:00
if (e.target.attributes.getNamedItem('data-close')) {
handleClose(channel);
2015-10-07 13:07:59 -04:00
} else {
Utils.switchChannel(channel);
}
e.preventDefault();
};
} else if (channel.fake && teamURL) {
// It's a direct message channel that doesn't exist yet so let's create it now
var otherUserId = Utils.getUserIdFromChannelName(channel);
if (this.state.loadingDMChannel === -1) {
2015-08-14 17:39:50 -07:00
handleClick = function clickHandler(e) {
e.preventDefault();
2015-10-07 13:07:59 -04:00
if (e.target.attributes.getNamedItem('data-close')) {
handleClose(channel);
} else {
this.setState({loadingDMChannel: index});
Client.createDirectChannel(channel, otherUserId,
2015-10-07 13:07:59 -04:00
(data) => {
this.setState({loadingDMChannel: -1});
AsyncClient.getChannel(data.id);
Utils.switchChannel(data);
2015-10-07 13:07:59 -04:00
},
() => {
this.setState({loadingDMChannel: -1});
window.location.href = TeamStore.getCurrentTeamUrl() + '/channels/' + channel.name;
2015-10-07 13:07:59 -04:00
}
);
}
2015-09-02 16:27:55 -04:00
}.bind(this);
2015-06-14 23:53:32 -08:00
}
2015-08-14 17:39:50 -07:00
}
2015-06-14 23:53:32 -08:00
let closeButton = null;
2015-10-16 10:15:52 -04:00
const removeTooltip = (
<Tooltip id='remove-dm-tooltip'>{'Remove from list'}</Tooltip>
);
if (handleClose && !badge) {
closeButton = (
2015-10-14 17:04:13 +05:00
<OverlayTrigger
2015-10-16 10:15:52 -04:00
delayShow={1000}
2015-10-14 17:04:13 +05:00
placement='top'
overlay={removeTooltip}
>
<span
2015-10-14 17:04:13 +05:00
className='btn-close'
data-close='true'
>
{'×'}
</span>
2015-10-14 17:04:13 +05:00
</OverlayTrigger>
);
rowClass += ' has-close';
}
return (
<li
key={channel.name}
ref={channel.name}
className={linkClass}
>
<a
className={rowClass}
href={href}
onClick={handleClick}
>
{status}
{channel.display_name}
{badge}
{closeButton}
</a>
</li>
);
}
render() {
this.badgesActive = false;
this.setUnreadCountPerChannel();
2015-10-25 15:12:36 +01:00
// keep track of the first and last unread channels so we can use them to set the unread indicators
this.firstUnreadChannel = null;
this.lastUnreadChannel = null;
// create elements for all 3 types of channels
const publicChannels = this.state.channels.filter((channel) => channel.type === 'O');
const publicChannelItems = publicChannels.map(this.createChannelElement);
2015-06-14 23:53:32 -08:00
const privateChannels = this.state.channels.filter((channel) => channel.type === 'P');
const privateChannelItems = privateChannels.map(this.createChannelElement);
const directMessageItems = this.state.visibleDirectChannels.map((channel, index, arr) => {
return this.createChannelElement(channel, index, arr, this.handleLeaveDirectChannel);
});
2015-06-14 23:53:32 -08:00
// update the favicon to show if there are any notifications
2015-06-14 23:53:32 -08:00
var link = document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
link.id = 'favicon';
if (this.badgesActive) {
2015-06-14 23:53:32 -08:00
link.href = '/static/images/redfavicon.ico';
} else {
link.href = '/static/images/favicon.ico';
}
var head = document.getElementsByTagName('head')[0];
var oldLink = document.getElementById('favicon');
if (oldLink) {
head.removeChild(oldLink);
}
head.appendChild(link);
var directMessageMore = null;
if (this.state.hiddenDirectChannelCount > 0) {
directMessageMore = (
<li key='more'>
<a
href='#'
onClick={this.showMoreDirectChannelsModal}
>
{'More (' + this.state.hiddenDirectChannelCount + ')'}
</a>
</li>
);
}
let showChannelModal = false;
if (this.state.newChannelModalType !== '') {
showChannelModal = true;
}
2015-10-16 10:15:52 -04:00
const createChannelTootlip = (
<Tooltip id='new-channel-tooltip' >{'Create new channel'}</Tooltip>
);
const createGroupTootlip = (
<Tooltip id='new-group-tooltip'>{'Create new group'}</Tooltip>
);
2015-10-14 17:04:13 +05:00
2015-06-14 23:53:32 -08:00
return (
<div>
<NewChannelFlow
show={showChannelModal}
channelType={this.state.newChannelModalType}
onModalDismissed={this.hideNewChannelModal}
/>
<MoreDirectChannels
show={this.state.showDirectChannelsModal}
onModalDismissed={this.hideMoreDirectChannelsModal}
/>
<SidebarHeader
2015-10-27 15:48:51 -04:00
teamDisplayName={TeamStore.getCurrent().display_name}
teamName={TeamStore.getCurrent().name}
teamType={TeamStore.getCurrent().type}
/>
2015-06-14 23:53:32 -08:00
<SearchBox />
<UnreadChannelIndicator
show={this.state.showTopUnread}
extraClass='nav-pills__unread-indicator-top'
text={'Unread post(s) above'}
/>
<UnreadChannelIndicator
show={this.state.showBottomUnread}
extraClass='nav-pills__unread-indicator-bottom'
text={'Unread post(s) below'}
/>
<div
ref='container'
className='nav-pills__container'
onScroll={this.onScroll}
>
<ul className='nav nav-pills nav-stacked'>
<li>
<h4>
2015-10-07 13:07:59 -04:00
{'Channels'}
2015-10-14 17:04:13 +05:00
<OverlayTrigger
2015-10-16 10:15:52 -04:00
delayShow={500}
2015-10-14 17:04:13 +05:00
placement='top'
overlay={createChannelTootlip}
>
<a
className='add-channel-btn'
href='#'
onClick={this.showNewChannelModal.bind(this, 'O')}
>
{'+'}
</a>
2015-10-14 17:04:13 +05:00
</OverlayTrigger>
</h4>
</li>
{publicChannelItems}
<li>
<a
href='#'
data-toggle='modal'
className='nav-more'
data-target='#more_channels'
data-channeltype='O'
>
2015-10-07 13:07:59 -04:00
{'More...'}
</a>
</li>
2015-06-14 23:53:32 -08:00
</ul>
<ul className='nav nav-pills nav-stacked'>
<li>
<h4>
2015-10-07 13:07:59 -04:00
{'Private Groups'}
2015-10-14 17:04:13 +05:00
<OverlayTrigger
2015-10-16 10:15:52 -04:00
delayShow={500}
2015-10-14 17:04:13 +05:00
placement='top'
overlay={createGroupTootlip}
>
<a
className='add-channel-btn'
href='#'
onClick={this.showNewChannelModal.bind(this, 'P')}
>
{'+'}
</a>
2015-10-14 17:04:13 +05:00
</OverlayTrigger>
</h4>
</li>
2015-06-14 23:53:32 -08:00
{privateChannelItems}
</ul>
<ul className='nav nav-pills nav-stacked'>
2015-10-07 13:07:59 -04:00
<li><h4>{'Direct Messages'}</h4></li>
2015-06-14 23:53:32 -08:00
{directMessageItems}
{directMessageMore}
2015-06-14 23:53:32 -08:00
</ul>
</div>
</div>
);
}
}
Sidebar.defaultProps = {
};
Sidebar.propTypes = {
};