// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
const Modal = ReactBootstrap.Modal;
import UserStore from '../stores/user_store.jsx';
import * as Utils from '../utils/utils.jsx';
import {intlShape, injectIntl, defineMessages, FormattedMessage} from 'mm-intl';
const holders = defineMessages({
member: {
id: 'more_direct_channels.member',
defaultMessage: 'Member'
},
search: {
id: 'more_direct_channels.search',
defaultMessage: 'Search members'
}
});
class MoreDirectChannels extends React.Component {
constructor(props) {
super(props);
this.handleFilterChange = this.handleFilterChange.bind(this);
this.handleHide = this.handleHide.bind(this);
this.handleShowDirectChannel = this.handleShowDirectChannel.bind(this);
this.handleUserChange = this.handleUserChange.bind(this);
this.createRowForUser = this.createRowForUser.bind(this);
this.state = {
users: this.getUsersFromStore(),
filter: '',
loadingDMChannel: -1
};
}
getUsersFromStore() {
const currentId = UserStore.getCurrentId();
const profiles = UserStore.getActiveOnlyProfiles();
const users = [];
for (const id in profiles) {
if (id !== currentId) {
users.push(profiles[id]);
}
}
users.sort((a, b) => a.username.localeCompare(b.username));
return users;
}
componentDidMount() {
UserStore.addChangeListener(this.handleUserChange);
}
componentWillUnmount() {
UserStore.addChangeListener(this.handleUserChange);
}
componentDidUpdate(prevProps) {
if (!prevProps.show && this.props.show) {
this.onShow();
}
}
onShow() {
if (Utils.isMobile()) {
$(ReactDOM.findDOMNode(this.refs.userList)).css('max-height', $(window).height() - 250);
} else {
$(ReactDOM.findDOMNode(this.refs.userList)).perfectScrollbar();
$(ReactDOM.findDOMNode(this.refs.userList)).css('max-height', $(window).height() - 300);
}
}
handleFilterChange() {
const filter = ReactDOM.findDOMNode(this.refs.filter).value;
if ($(window).width() > 768) {
$(ReactDOM.findDOMNode(this.refs.userList)).scrollTop(0);
}
if (filter !== this.state.filter) {
this.setState({filter});
}
}
handleHide() {
if (this.props.onModalDismissed) {
this.props.onModalDismissed();
}
this.setState({filter: ''});
}
handleShowDirectChannel(teammate, e) {
e.preventDefault();
if (this.state.loadingDMChannel !== -1) {
return;
}
this.setState({loadingDMChannel: teammate.id});
Utils.openDirectChannelToUser(
teammate,
(channel) => {
Utils.switchChannel(channel);
this.setState({loadingDMChannel: -1});
this.handleHide();
},
() => {
this.setState({loadingDMChannel: -1});
}
);
}
handleUserChange() {
this.setState({users: this.getUsersFromStore()});
}
createRowForUser(user) {
const details = [];
const fullName = Utils.getFullName(user);
if (fullName) {
details.push(
{fullName}
);
}
if (user.nickname) {
const separator = fullName ? ' - ' : '';
details.push(
{separator + user.nickname}
);
}
let joinButton;
if (this.state.loadingDMChannel === user.id) {
joinButton = (
);
} else {
joinButton = (
);
}
return (