2019-03-12 16:23:06 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
2019-10-02 12:18:08 +00:00
|
|
|
ScrollView, Text, View, FlatList, SafeAreaView
|
2019-03-12 16:23:06 +00:00
|
|
|
} from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import equal from 'deep-equal';
|
|
|
|
import { RectButton } from 'react-native-gesture-handler';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2019-03-12 16:23:06 +00:00
|
|
|
|
|
|
|
import { logout as logoutAction } from '../../actions/login';
|
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import Status from '../../containers/Status/Status';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import styles from './styles';
|
|
|
|
import SidebarItem from './SidebarItem';
|
2019-03-29 19:36:07 +00:00
|
|
|
import { COLOR_TEXT } from '../../constants/colors';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../lib/database';
|
2019-09-19 13:32:24 +00:00
|
|
|
import { animateNextTransition } from '../../utils/layoutAnimation';
|
2019-11-25 20:01:17 +00:00
|
|
|
import { withSplit } from '../../split';
|
2019-03-12 16:23:06 +00:00
|
|
|
|
|
|
|
const keyExtractor = item => item.id;
|
|
|
|
|
|
|
|
const Separator = React.memo(() => <View style={styles.separator} />);
|
|
|
|
|
2019-05-18 19:31:33 +00:00
|
|
|
const permissions = [
|
|
|
|
'view-statistics',
|
|
|
|
'view-room-administration',
|
|
|
|
'view-user-administration',
|
|
|
|
'view-privileged-setting'
|
|
|
|
];
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class Sidebar extends Component {
|
2019-03-12 16:23:06 +00:00
|
|
|
static propTypes = {
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
Site_Name: PropTypes.string.isRequired,
|
|
|
|
user: PropTypes.object,
|
|
|
|
logout: PropTypes.func.isRequired,
|
2019-09-24 20:10:50 +00:00
|
|
|
activeItemKey: PropTypes.string,
|
2019-11-25 20:01:17 +00:00
|
|
|
loadingServer: PropTypes.bool,
|
|
|
|
split: PropTypes.bool
|
2019-03-12 16:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
showStatus: false,
|
2019-09-16 20:26:32 +00:00
|
|
|
isAdmin: false,
|
2019-03-12 16:23:06 +00:00
|
|
|
status: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.setStatus();
|
2019-09-16 20:26:32 +00:00
|
|
|
this.setIsAdmin();
|
2019-03-12 16:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
2019-09-24 20:10:50 +00:00
|
|
|
const { user, loadingServer } = this.props;
|
2019-03-12 16:23:06 +00:00
|
|
|
if (nextProps.user && user && user.language !== nextProps.user.language) {
|
|
|
|
this.setStatus();
|
|
|
|
}
|
2019-09-24 20:10:50 +00:00
|
|
|
if (loadingServer && nextProps.loadingServer !== loadingServer) {
|
|
|
|
this.setIsAdmin();
|
|
|
|
}
|
2019-03-12 16:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-09-24 20:10:50 +00:00
|
|
|
const { status, showStatus, isAdmin } = this.state;
|
2019-03-12 16:23:06 +00:00
|
|
|
const {
|
2019-11-25 20:01:17 +00:00
|
|
|
Site_Name, user, baseUrl, activeItemKey, split
|
2019-03-12 16:23:06 +00:00
|
|
|
} = this.props;
|
|
|
|
if (nextState.showStatus !== showStatus) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.Site_Name !== Site_Name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.Site_Name !== Site_Name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.baseUrl !== baseUrl) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.activeItemKey !== activeItemKey) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.user && user) {
|
|
|
|
if (nextProps.user.language !== user.language) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.user.status !== user.status) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.user.username !== user.username) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
if (nextProps.split !== split) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-12 16:23:06 +00:00
|
|
|
if (!equal(nextState.status, status)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-24 20:10:50 +00:00
|
|
|
if (nextState.isAdmin !== isAdmin) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-12 16:23:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus = () => {
|
|
|
|
this.setState({
|
|
|
|
status: [{
|
|
|
|
id: 'online',
|
|
|
|
name: I18n.t('Online')
|
|
|
|
}, {
|
|
|
|
id: 'busy',
|
|
|
|
name: I18n.t('Busy')
|
|
|
|
}, {
|
|
|
|
id: 'away',
|
|
|
|
name: I18n.t('Away')
|
|
|
|
}, {
|
|
|
|
id: 'offline',
|
|
|
|
name: I18n.t('Invisible')
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
async setIsAdmin() {
|
|
|
|
const db = database.active;
|
|
|
|
const { user } = this.props;
|
|
|
|
const { roles } = user;
|
|
|
|
try {
|
|
|
|
if (roles) {
|
|
|
|
const permissionsCollection = db.collections.get('permissions');
|
|
|
|
const permissionsFiltered = await permissionsCollection.query(Q.where('id', Q.oneOf(permissions))).fetch();
|
|
|
|
const isAdmin = permissionsFiltered.reduce((result, permission) => (
|
|
|
|
result || permission.roles.some(r => roles.indexOf(r) !== -1)),
|
|
|
|
false);
|
|
|
|
this.setState({ isAdmin });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-03-12 16:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logout = () => {
|
|
|
|
const { logout } = this.props;
|
|
|
|
logout();
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
sidebarNavigate = (route) => {
|
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.navigate(route);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleStatus = () => {
|
2019-09-19 13:32:24 +00:00
|
|
|
animateNextTransition();
|
2019-09-16 20:26:32 +00:00
|
|
|
this.setState(prevState => ({ showStatus: !prevState.showStatus }));
|
2019-05-18 19:31:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
renderStatusItem = ({ item }) => {
|
|
|
|
const { user } = this.props;
|
|
|
|
return (
|
|
|
|
<SidebarItem
|
|
|
|
text={item.name}
|
|
|
|
left={<Status style={styles.status} size={12} status={item.id} />}
|
|
|
|
current={user.status === item.id}
|
|
|
|
onPress={() => {
|
|
|
|
this.toggleStatus();
|
|
|
|
if (user.status !== item.id) {
|
|
|
|
try {
|
|
|
|
RocketChat.setUserPresenceDefaultStatus(item.id);
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-03-12 16:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderNavigation = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { isAdmin } = this.state;
|
2019-03-12 16:23:06 +00:00
|
|
|
const { activeItemKey } = this.props;
|
|
|
|
return (
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2019-03-12 16:23:06 +00:00
|
|
|
<SidebarItem
|
|
|
|
text={I18n.t('Chats')}
|
2019-04-08 12:35:28 +00:00
|
|
|
left={<CustomIcon name='message' size={20} color={COLOR_TEXT} />}
|
2019-03-12 16:23:06 +00:00
|
|
|
onPress={() => this.sidebarNavigate('RoomsListView')}
|
|
|
|
testID='sidebar-chats'
|
|
|
|
current={activeItemKey === 'ChatsStack'}
|
|
|
|
/>
|
|
|
|
<SidebarItem
|
|
|
|
text={I18n.t('Profile')}
|
2019-03-29 19:36:07 +00:00
|
|
|
left={<CustomIcon name='user' size={20} color={COLOR_TEXT} />}
|
2019-03-12 16:23:06 +00:00
|
|
|
onPress={() => this.sidebarNavigate('ProfileView')}
|
|
|
|
testID='sidebar-profile'
|
|
|
|
current={activeItemKey === 'ProfileStack'}
|
|
|
|
/>
|
|
|
|
<SidebarItem
|
|
|
|
text={I18n.t('Settings')}
|
2019-03-29 19:36:07 +00:00
|
|
|
left={<CustomIcon name='cog' size={20} color={COLOR_TEXT} />}
|
2019-03-12 16:23:06 +00:00
|
|
|
onPress={() => this.sidebarNavigate('SettingsView')}
|
|
|
|
testID='sidebar-settings'
|
|
|
|
current={activeItemKey === 'SettingsStack'}
|
|
|
|
/>
|
2019-09-16 20:26:32 +00:00
|
|
|
{isAdmin ? (
|
2019-05-18 19:31:33 +00:00
|
|
|
<SidebarItem
|
|
|
|
text={I18n.t('Admin_Panel')}
|
|
|
|
left={<CustomIcon name='shield-alt' size={20} color={COLOR_TEXT} />}
|
|
|
|
onPress={() => this.sidebarNavigate('AdminPanelView')}
|
|
|
|
testID='sidebar-settings'
|
|
|
|
current={activeItemKey === 'AdminPanelStack'}
|
|
|
|
/>
|
|
|
|
) : null}
|
2019-03-12 16:23:06 +00:00
|
|
|
<Separator key='separator-logout' />
|
|
|
|
<SidebarItem
|
|
|
|
text={I18n.t('Logout')}
|
2019-03-29 19:36:07 +00:00
|
|
|
left={<CustomIcon name='sign-out' size={20} color={COLOR_TEXT} />}
|
2019-03-12 16:23:06 +00:00
|
|
|
onPress={this.logout}
|
|
|
|
testID='sidebar-logout'
|
|
|
|
/>
|
2019-09-24 20:26:56 +00:00
|
|
|
</>
|
2019-03-12 16:23:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderStatus = () => {
|
|
|
|
const { status } = this.state;
|
|
|
|
const { user } = this.props;
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
key='status-list'
|
|
|
|
data={status}
|
|
|
|
extraData={user}
|
|
|
|
renderItem={this.renderStatusItem}
|
|
|
|
keyExtractor={keyExtractor}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { showStatus } = this.state;
|
2019-11-25 20:01:17 +00:00
|
|
|
const {
|
|
|
|
user, Site_Name, baseUrl, split
|
|
|
|
} = this.props;
|
2019-03-12 16:23:06 +00:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<SafeAreaView testID='sidebar-view' style={styles.container}>
|
|
|
|
<ScrollView style={styles.container} {...scrollPersistTaps}>
|
|
|
|
<RectButton
|
|
|
|
onPress={this.toggleStatus}
|
2019-03-29 19:36:07 +00:00
|
|
|
underlayColor={COLOR_TEXT}
|
2019-03-12 16:23:06 +00:00
|
|
|
activeOpacity={0.1}
|
|
|
|
testID='sidebar-toggle-status'
|
|
|
|
style={styles.header}
|
|
|
|
>
|
|
|
|
<Avatar
|
|
|
|
text={user.username}
|
|
|
|
size={30}
|
|
|
|
style={styles.avatar}
|
|
|
|
baseUrl={baseUrl}
|
2019-04-18 20:57:35 +00:00
|
|
|
userId={user.id}
|
|
|
|
token={user.token}
|
2019-03-12 16:23:06 +00:00
|
|
|
/>
|
|
|
|
<View style={styles.headerTextContainer}>
|
|
|
|
<View style={styles.headerUsername}>
|
2019-09-16 20:26:32 +00:00
|
|
|
<Status style={styles.status} size={12} status={user && user.status} />
|
2019-03-29 19:36:07 +00:00
|
|
|
<Text numberOfLines={1} style={styles.username}>{user.username}</Text>
|
2019-03-12 16:23:06 +00:00
|
|
|
</View>
|
|
|
|
<Text style={styles.currentServerText} numberOfLines={1}>{Site_Name}</Text>
|
|
|
|
</View>
|
|
|
|
<CustomIcon name='arrow-down' size={20} style={[styles.headerIcon, showStatus && styles.inverted]} />
|
|
|
|
</RectButton>
|
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
{!split || showStatus ? <Separator key='separator-header' /> : null}
|
2019-03-12 16:23:06 +00:00
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
{!showStatus && !split ? this.renderNavigation() : null}
|
2019-03-12 16:23:06 +00:00
|
|
|
{showStatus ? this.renderStatus() : null}
|
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
Site_Name: state.settings.Site_Name,
|
|
|
|
user: {
|
|
|
|
id: state.login.user && state.login.user.id,
|
|
|
|
language: state.login.user && state.login.user.language,
|
|
|
|
status: state.login.user && state.login.user.status,
|
|
|
|
username: state.login.user && state.login.user.username,
|
|
|
|
token: state.login.user && state.login.user.token,
|
|
|
|
roles: state.login.user && state.login.user.roles
|
|
|
|
},
|
2019-09-24 20:10:50 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
|
|
|
loadingServer: state.server.loading
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
logout: () => dispatch(logoutAction())
|
|
|
|
});
|
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withSplit(Sidebar));
|