2017-09-21 17:08:00 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
ScrollView, Text, View, StyleSheet, FlatList, LayoutAnimation, SafeAreaView
|
|
|
|
} from 'react-native';
|
2017-09-21 17:08:00 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-06-05 01:17:02 +00:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialIcons';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2019-01-31 16:08:38 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2018-12-05 20:52:08 +00:00
|
|
|
import { setStackRoot as setStackRootAction } from '../actions';
|
2018-09-25 19:28:42 +00:00
|
|
|
import { logout as logoutAction } from '../actions/login';
|
2019-01-31 16:08:38 +00:00
|
|
|
import Avatar from '../containers/Avatar';
|
|
|
|
import Status from '../containers/status';
|
2018-06-05 01:17:02 +00:00
|
|
|
import Touch from '../utils/touch';
|
|
|
|
import { STATUS_COLORS } from '../constants/colors';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import log from '../utils/log';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2018-08-10 17:26:36 +00:00
|
|
|
import scrollPersistTaps from '../utils/scrollPersistTaps';
|
2019-01-29 19:52:56 +00:00
|
|
|
import { getReadableVersion } from '../utils/deviceInfo';
|
2017-09-21 17:08:00 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2018-08-01 19:35:06 +00:00
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
backgroundColor: '#fff'
|
2017-09-21 17:08:00 +00:00
|
|
|
},
|
2018-06-05 01:17:02 +00:00
|
|
|
item: {
|
|
|
|
flexDirection: 'row',
|
2017-09-21 17:08:00 +00:00
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2018-06-05 01:17:02 +00:00
|
|
|
itemLeft: {
|
|
|
|
marginHorizontal: 10,
|
|
|
|
width: 30,
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
itemText: {
|
|
|
|
marginVertical: 16,
|
|
|
|
fontWeight: 'bold',
|
|
|
|
color: '#292E35'
|
2017-09-21 17:08:00 +00:00
|
|
|
},
|
2018-10-23 21:39:48 +00:00
|
|
|
itemSelected: {
|
|
|
|
backgroundColor: '#F7F8FA'
|
|
|
|
},
|
2018-06-05 01:17:02 +00:00
|
|
|
separator: {
|
|
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
|
|
borderColor: '#ddd',
|
|
|
|
marginVertical: 4
|
2017-09-21 17:08:00 +00:00
|
|
|
},
|
2018-06-05 01:17:02 +00:00
|
|
|
header: {
|
|
|
|
paddingVertical: 16,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
headerTextContainer: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'column',
|
|
|
|
alignItems: 'flex-start'
|
|
|
|
},
|
|
|
|
headerUsername: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
avatar: {
|
|
|
|
marginHorizontal: 10
|
|
|
|
},
|
|
|
|
status: {
|
|
|
|
borderRadius: 12,
|
|
|
|
width: 12,
|
|
|
|
height: 12,
|
|
|
|
marginRight: 5
|
|
|
|
},
|
|
|
|
currentServerText: {
|
|
|
|
fontWeight: 'bold'
|
2018-09-21 17:24:15 +00:00
|
|
|
},
|
|
|
|
version: {
|
|
|
|
marginHorizontal: 5,
|
|
|
|
marginBottom: 5,
|
|
|
|
fontWeight: '600',
|
|
|
|
color: '#292E35',
|
|
|
|
fontSize: 13
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
});
|
2018-03-02 21:31:44 +00:00
|
|
|
const keyExtractor = item => item.id;
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2017-09-21 17:08:00 +00:00
|
|
|
@connect(state => ({
|
2018-11-14 21:42:03 +00:00
|
|
|
Site_Name: state.settings.Site_Name,
|
2018-11-22 18:54:38 +00:00
|
|
|
stackRoot: state.app.stackRoot,
|
2018-07-10 13:40:32 +00:00
|
|
|
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,
|
2019-02-07 19:58:20 +00:00
|
|
|
username: state.login.user && state.login.user.username,
|
|
|
|
token: state.login.user && state.login.user.token
|
2018-09-11 16:32:52 +00:00
|
|
|
},
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
2017-09-21 17:08:00 +00:00
|
|
|
}), dispatch => ({
|
2018-09-25 19:28:42 +00:00
|
|
|
logout: () => dispatch(logoutAction()),
|
2018-11-22 18:54:38 +00:00
|
|
|
setStackRoot: stackRoot => dispatch(setStackRootAction(stackRoot))
|
2017-09-21 17:08:00 +00:00
|
|
|
}))
|
|
|
|
export default class Sidebar extends Component {
|
|
|
|
static propTypes = {
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2018-10-23 21:39:48 +00:00
|
|
|
componentId: PropTypes.string,
|
2018-11-14 21:42:03 +00:00
|
|
|
Site_Name: PropTypes.string.isRequired,
|
2018-11-22 18:54:38 +00:00
|
|
|
stackRoot: PropTypes.string.isRequired,
|
2018-07-10 13:40:32 +00:00
|
|
|
user: PropTypes.object,
|
2018-08-10 17:26:36 +00:00
|
|
|
logout: PropTypes.func.isRequired,
|
2018-11-22 18:54:38 +00:00
|
|
|
setStackRoot: PropTypes.func
|
2017-09-21 17:08:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:40:44 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-06-05 01:17:02 +00:00
|
|
|
this.state = {
|
2018-12-21 10:55:35 +00:00
|
|
|
showStatus: false,
|
|
|
|
status: []
|
2018-06-05 01:17:02 +00:00
|
|
|
};
|
2018-10-23 21:39:48 +00:00
|
|
|
Navigation.events().bindComponent(this);
|
2018-03-06 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-06-13 01:33:00 +00:00
|
|
|
this.setStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { user } = this.props;
|
|
|
|
if (nextProps.user && user && user.language !== nextProps.user.language) {
|
2018-06-13 01:33:00 +00:00
|
|
|
this.setStatus();
|
|
|
|
}
|
2018-03-06 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { status, showStatus } = this.state;
|
|
|
|
const {
|
|
|
|
Site_Name, stackRoot, user, baseUrl
|
|
|
|
} = this.props;
|
|
|
|
if (nextState.showStatus !== showStatus) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.Site_Name !== Site_Name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.stackRoot !== stackRoot) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.Site_Name !== Site_Name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.baseUrl !== baseUrl) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!equal(nextState.status, status)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
handleChangeStack = (event) => {
|
|
|
|
const { stack } = event;
|
|
|
|
this.setStack(stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
navigationButtonPressed = ({ buttonId }) => {
|
|
|
|
if (buttonId === 'cancel') {
|
|
|
|
const { componentId } = this.props;
|
|
|
|
Navigation.dismissModal(componentId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 01:33:00 +00:00
|
|
|
setStatus = () => {
|
2018-12-21 10:55:35 +00:00
|
|
|
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')
|
|
|
|
}]
|
2018-06-13 01:33:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-22 18:54:38 +00:00
|
|
|
setStack = async(stack) => {
|
|
|
|
const { stackRoot, setStackRoot } = this.props;
|
|
|
|
if (stackRoot !== stack) {
|
|
|
|
await Navigation.setStackRoot('AppRoot', {
|
2018-10-23 21:39:48 +00:00
|
|
|
component: {
|
|
|
|
id: stack,
|
|
|
|
name: stack
|
|
|
|
}
|
|
|
|
});
|
2018-11-22 18:54:38 +00:00
|
|
|
setStackRoot(stack);
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 13:39:18 +00:00
|
|
|
closeDrawer = () => {
|
2019-01-31 16:08:38 +00:00
|
|
|
Navigation.toggleDrawer();
|
2018-05-23 13:39:18 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:24:32 +00:00
|
|
|
toggleStatus = () => {
|
2018-06-05 01:17:02 +00:00
|
|
|
LayoutAnimation.easeInEaseOut();
|
2018-09-25 19:28:42 +00:00
|
|
|
this.setState(prevState => ({ showStatus: !prevState.showStatus }));
|
2018-06-05 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
sidebarNavigate = (stack) => {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.closeDrawer();
|
2018-10-23 21:39:48 +00:00
|
|
|
this.setStack(stack);
|
2018-06-05 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderSeparator = key => <View key={key} style={styles.separator} />;
|
2017-09-21 17:08:00 +00:00
|
|
|
|
2018-06-05 01:17:02 +00:00
|
|
|
renderItem = ({
|
2018-10-23 21:39:48 +00:00
|
|
|
text, left, onPress, testID, current
|
2018-06-05 01:17:02 +00:00
|
|
|
}) => (
|
|
|
|
<Touch
|
|
|
|
key={text}
|
|
|
|
onPress={onPress}
|
|
|
|
underlayColor='rgba(255, 255, 255, 0.5)'
|
|
|
|
activeOpacity={0.3}
|
|
|
|
testID={testID}
|
2017-09-21 17:08:00 +00:00
|
|
|
>
|
2018-10-23 21:39:48 +00:00
|
|
|
<View style={[styles.item, current && styles.itemSelected]}>
|
2018-07-10 13:40:32 +00:00
|
|
|
<View style={styles.itemLeft}>
|
2018-06-05 01:17:02 +00:00
|
|
|
{left}
|
|
|
|
</View>
|
|
|
|
<Text style={styles.itemText}>
|
|
|
|
{text}
|
2017-09-21 17:08:00 +00:00
|
|
|
</Text>
|
|
|
|
</View>
|
2018-06-05 01:17:02 +00:00
|
|
|
</Touch>
|
|
|
|
)
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderStatusItem = ({ item }) => {
|
|
|
|
const { user } = this.props;
|
|
|
|
return (
|
|
|
|
this.renderItem({
|
|
|
|
text: item.name,
|
|
|
|
left: <View style={[styles.status, { backgroundColor: STATUS_COLORS[item.id] }]} />,
|
2018-10-23 21:39:48 +00:00
|
|
|
current: user.status === item.id,
|
2018-09-25 19:28:42 +00:00
|
|
|
onPress: () => {
|
|
|
|
this.closeDrawer();
|
|
|
|
this.toggleStatus();
|
|
|
|
if (user.status !== item.id) {
|
|
|
|
try {
|
|
|
|
RocketChat.setUserPresenceDefaultStatus(item.id);
|
|
|
|
} catch (e) {
|
|
|
|
log('setUserPresenceDefaultStatus', e);
|
|
|
|
}
|
2018-06-05 01:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2018-09-25 19:28:42 +00:00
|
|
|
);
|
|
|
|
}
|
2018-06-05 01:17:02 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderNavigation = () => {
|
2018-11-22 18:54:38 +00:00
|
|
|
const { stackRoot } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
const { logout } = this.props;
|
|
|
|
return (
|
|
|
|
[
|
|
|
|
this.renderItem({
|
|
|
|
text: I18n.t('Chats'),
|
|
|
|
left: <Icon name='chat-bubble' size={20} />,
|
2018-10-23 21:39:48 +00:00
|
|
|
onPress: () => this.sidebarNavigate('RoomsListView'),
|
|
|
|
testID: 'sidebar-chats',
|
2018-11-22 18:54:38 +00:00
|
|
|
current: stackRoot === 'RoomsListView'
|
2018-09-25 19:28:42 +00:00
|
|
|
}),
|
|
|
|
this.renderItem({
|
|
|
|
text: I18n.t('Profile'),
|
|
|
|
left: <Icon name='person' size={20} />,
|
2018-10-23 21:39:48 +00:00
|
|
|
onPress: () => this.sidebarNavigate('ProfileView'),
|
|
|
|
testID: 'sidebar-profile',
|
2018-11-22 18:54:38 +00:00
|
|
|
current: stackRoot === 'ProfileView'
|
2018-09-25 19:28:42 +00:00
|
|
|
}),
|
|
|
|
this.renderItem({
|
|
|
|
text: I18n.t('Settings'),
|
|
|
|
left: <Icon name='settings' size={20} />,
|
2018-10-23 21:39:48 +00:00
|
|
|
onPress: () => this.sidebarNavigate('SettingsView'),
|
|
|
|
testID: 'sidebar-settings',
|
2018-11-22 18:54:38 +00:00
|
|
|
current: stackRoot === 'SettingsView'
|
2018-09-25 19:28:42 +00:00
|
|
|
}),
|
|
|
|
this.renderSeparator('separator-logout'),
|
|
|
|
this.renderItem({
|
|
|
|
text: I18n.t('Logout'),
|
|
|
|
left: <Icon name='exit-to-app' size={20} />,
|
|
|
|
onPress: () => logout(),
|
|
|
|
testID: 'sidebar-logout'
|
|
|
|
})
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderStatus = () => {
|
|
|
|
const { status } = this.state;
|
|
|
|
const { user } = this.props;
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
key='status-list'
|
|
|
|
data={status}
|
|
|
|
extraData={user}
|
|
|
|
renderItem={this.renderStatusItem}
|
|
|
|
keyExtractor={keyExtractor}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { showStatus } = this.state;
|
2018-11-14 21:42:03 +00:00
|
|
|
const { user, Site_Name, baseUrl } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-09-21 17:08:00 +00:00
|
|
|
return (
|
2019-01-31 16:08:38 +00:00
|
|
|
<SafeAreaView testID='sidebar-view' style={styles.container}>
|
2018-08-10 17:26:36 +00:00
|
|
|
<ScrollView style={styles.container} {...scrollPersistTaps}>
|
2018-06-05 01:17:02 +00:00
|
|
|
<Touch
|
2018-09-21 17:24:32 +00:00
|
|
|
onPress={() => this.toggleStatus()}
|
2018-06-05 01:17:02 +00:00
|
|
|
underlayColor='rgba(255, 255, 255, 0.5)'
|
|
|
|
activeOpacity={0.3}
|
2018-09-21 17:24:32 +00:00
|
|
|
testID='sidebar-toggle-status'
|
2017-11-13 13:53:45 +00:00
|
|
|
>
|
2018-06-05 01:17:02 +00:00
|
|
|
<View style={styles.header}>
|
|
|
|
<Avatar
|
|
|
|
text={user.username}
|
|
|
|
size={30}
|
|
|
|
style={styles.avatar}
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl={baseUrl}
|
2019-02-07 19:58:20 +00:00
|
|
|
user={user}
|
2018-06-05 01:17:02 +00:00
|
|
|
/>
|
|
|
|
<View style={styles.headerTextContainer}>
|
|
|
|
<View style={styles.headerUsername}>
|
|
|
|
<Status style={styles.status} id={user.id} />
|
2018-08-01 19:35:06 +00:00
|
|
|
<Text numberOfLines={1}>{user.username}</Text>
|
2018-06-05 01:17:02 +00:00
|
|
|
</View>
|
2018-11-14 21:42:03 +00:00
|
|
|
<Text style={styles.currentServerText} numberOfLines={1}>{Site_Name}</Text>
|
2018-06-05 01:17:02 +00:00
|
|
|
</View>
|
|
|
|
<Icon
|
2018-09-25 19:28:42 +00:00
|
|
|
name={showStatus ? 'keyboard-arrow-up' : 'keyboard-arrow-down'}
|
2018-06-05 01:17:02 +00:00
|
|
|
size={30}
|
2018-07-10 13:40:32 +00:00
|
|
|
style={{ paddingHorizontal: 10 }}
|
2018-06-05 01:17:02 +00:00
|
|
|
/>
|
2017-11-13 13:53:45 +00:00
|
|
|
</View>
|
2018-06-05 01:17:02 +00:00
|
|
|
</Touch>
|
|
|
|
|
|
|
|
{this.renderSeparator('separator-header')}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
{!showStatus ? this.renderNavigation() : null}
|
|
|
|
{showStatus ? this.renderStatus() : null}
|
2018-08-10 17:26:36 +00:00
|
|
|
</ScrollView>
|
2018-09-21 17:24:15 +00:00
|
|
|
<Text style={styles.version}>
|
2019-01-29 19:52:56 +00:00
|
|
|
{getReadableVersion}
|
2018-09-21 17:24:15 +00:00
|
|
|
</Text>
|
2018-08-10 17:26:36 +00:00
|
|
|
</SafeAreaView>
|
2017-09-21 17:08:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|