2018-08-31 16:46:33 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2019-06-26 19:50:03 +00:00
|
|
|
View, Text, Animated, Easing, TouchableWithoutFeedback, TouchableOpacity, FlatList, Image
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2018-08-31 16:46:33 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-10-23 21:39:48 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-12-21 10:55:35 +00:00
|
|
|
import equal from 'deep-equal';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { withNavigation } from 'react-navigation';
|
2019-06-26 19:50:03 +00:00
|
|
|
import RNUserDefaults from 'rn-user-defaults';
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
import { toggleServerDropdown as toggleServerDropdownAction } from '../../actions/rooms';
|
|
|
|
import { selectServerRequest as selectServerRequestAction } from '../../actions/server';
|
|
|
|
import { appStart as appStartAction } from '../../actions';
|
2018-08-31 16:46:33 +00:00
|
|
|
import styles from './styles';
|
|
|
|
import Touch from '../../utils/touch';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import I18n from '../../i18n';
|
2018-10-23 21:39:48 +00:00
|
|
|
import EventEmitter from '../../utils/events';
|
2019-06-10 16:22:35 +00:00
|
|
|
import Check from '../../containers/Check';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../../lib/database';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
2019-11-25 20:01:17 +00:00
|
|
|
import { KEY_COMMAND, handleCommandSelectServer } from '../../commands';
|
|
|
|
import { isTablet } from '../../utils/deviceInfo';
|
|
|
|
import { withSplit } from '../../split';
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
const ROW_HEIGHT = 68;
|
|
|
|
const ANIMATION_DURATION = 200;
|
|
|
|
|
2019-03-12 16:23:06 +00:00
|
|
|
class ServerDropdown extends Component {
|
2018-08-31 16:46:33 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2018-08-31 16:46:33 +00:00
|
|
|
closeServerDropdown: PropTypes.bool,
|
2019-11-25 20:01:17 +00:00
|
|
|
split: PropTypes.bool,
|
2018-08-31 16:46:33 +00:00
|
|
|
server: PropTypes.string,
|
2019-12-04 16:39:53 +00:00
|
|
|
theme: PropTypes.string,
|
2018-08-31 16:46:33 +00:00
|
|
|
toggleServerDropdown: PropTypes.func,
|
|
|
|
selectServerRequest: PropTypes.func,
|
|
|
|
appStart: PropTypes.func
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.state = { servers: [] };
|
2018-08-31 16:46:33 +00:00
|
|
|
this.animatedValue = new Animated.Value(0);
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
const serversDB = database.servers;
|
|
|
|
const observable = await serversDB.collections
|
|
|
|
.get('servers')
|
|
|
|
.query()
|
|
|
|
.observeWithColumns(['name']);
|
|
|
|
|
|
|
|
this.subscription = observable.subscribe((data) => {
|
|
|
|
this.setState({ servers: data });
|
|
|
|
});
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
Animated.timing(
|
|
|
|
this.animatedValue,
|
|
|
|
{
|
|
|
|
toValue: 1,
|
|
|
|
duration: ANIMATION_DURATION,
|
2018-12-21 10:55:35 +00:00
|
|
|
easing: Easing.inOut(Easing.quad),
|
2018-08-31 16:46:33 +00:00
|
|
|
useNativeDriver: true
|
2019-10-31 16:21:59 +00:00
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
).start();
|
2019-11-25 20:01:17 +00:00
|
|
|
if (isTablet) {
|
|
|
|
EventEmitter.addEventListener(KEY_COMMAND, this.handleCommands);
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { servers } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { closeServerDropdown, server, theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextProps.closeServerDropdown !== closeServerDropdown) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextProps.server !== server) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!equal(nextState.servers, servers)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { closeServerDropdown } = this.props;
|
|
|
|
if (prevProps.closeServerDropdown !== closeServerDropdown) {
|
2018-08-31 16:46:33 +00:00
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 14:26:40 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.newServerTimeout) {
|
|
|
|
clearTimeout(this.newServerTimeout);
|
|
|
|
this.newServerTimeout = false;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.subscription && this.subscription.unsubscribe) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2019-11-25 20:01:17 +00:00
|
|
|
if (isTablet) {
|
|
|
|
EventEmitter.removeListener(KEY_COMMAND, this.handleCommands);
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close = () => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { toggleServerDropdown } = this.props;
|
2018-08-31 16:46:33 +00:00
|
|
|
Animated.timing(
|
|
|
|
this.animatedValue,
|
|
|
|
{
|
|
|
|
toValue: 0,
|
|
|
|
duration: ANIMATION_DURATION,
|
2018-12-21 10:55:35 +00:00
|
|
|
easing: Easing.inOut(Easing.quad),
|
2018-08-31 16:46:33 +00:00
|
|
|
useNativeDriver: true
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
).start(() => toggleServerDropdown());
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addServer = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { server, navigation } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
this.close();
|
|
|
|
setTimeout(() => {
|
2020-03-30 19:20:50 +00:00
|
|
|
navigation.navigate('NewServerView', { previousServer: server });
|
2018-08-31 16:46:33 +00:00
|
|
|
}, ANIMATION_DURATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
select = async(server) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const {
|
2019-11-25 20:01:17 +00:00
|
|
|
server: currentServer, selectServerRequest, appStart, navigation, split
|
2018-09-25 19:28:42 +00:00
|
|
|
} = this.props;
|
|
|
|
|
2018-08-31 16:46:33 +00:00
|
|
|
this.close();
|
2018-10-15 20:22:42 +00:00
|
|
|
if (currentServer !== server) {
|
2019-06-26 19:50:03 +00:00
|
|
|
const userId = await RNUserDefaults.get(`${ RocketChat.TOKEN_KEY }-${ server }`);
|
2019-11-25 20:01:17 +00:00
|
|
|
if (split) {
|
|
|
|
navigation.navigate('RoomView');
|
|
|
|
}
|
2019-06-26 19:50:03 +00:00
|
|
|
if (!userId) {
|
2018-09-25 19:28:42 +00:00
|
|
|
appStart();
|
2019-02-27 14:26:40 +00:00
|
|
|
this.newServerTimeout = setTimeout(() => {
|
2018-10-23 21:39:48 +00:00
|
|
|
EventEmitter.emit('NewServer', { server });
|
2018-08-31 16:46:33 +00:00
|
|
|
}, 1000);
|
2018-10-15 20:22:42 +00:00
|
|
|
} else {
|
|
|
|
selectServerRequest(server);
|
2018-08-31 16:46:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 20:01:17 +00:00
|
|
|
handleCommands = ({ event }) => {
|
|
|
|
const { servers } = this.state;
|
|
|
|
const { navigation } = this.props;
|
|
|
|
const { input } = event;
|
|
|
|
if (handleCommandSelectServer(event)) {
|
|
|
|
if (servers[input - 1]) {
|
|
|
|
this.select(servers[input - 1].id);
|
|
|
|
navigation.navigate('RoomView');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderSeparator = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <View style={[styles.serverSeparator, { backgroundColor: themes[theme].separatorColor }]} />;
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderServer = ({ item }) => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { server, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
|
|
|
return (
|
2019-12-04 16:39:53 +00:00
|
|
|
<Touch
|
|
|
|
onPress={() => this.select(item.id)}
|
|
|
|
testID={`rooms-list-header-server-${ item.id }`}
|
|
|
|
theme={theme}
|
|
|
|
>
|
2018-09-25 19:28:42 +00:00
|
|
|
<View style={styles.serverItemContainer}>
|
|
|
|
{item.iconURL
|
|
|
|
? (
|
|
|
|
<Image
|
|
|
|
source={{ uri: item.iconURL }}
|
|
|
|
defaultSource={{ uri: 'logo' }}
|
|
|
|
style={styles.serverIcon}
|
2018-10-15 20:22:42 +00:00
|
|
|
onError={() => console.warn('error loading serverIcon')}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<Image
|
|
|
|
source={{ uri: 'logo' }}
|
|
|
|
style={styles.serverIcon}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
<View style={styles.serverTextContainer}>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text style={[styles.serverName, { color: themes[theme].titleText }]}>{item.name || item.id}</Text>
|
|
|
|
<Text style={[styles.serverUrl, { color: themes[theme].auxiliaryText }]}>{item.id}</Text>
|
2018-09-25 19:28:42 +00:00
|
|
|
</View>
|
2019-12-04 16:39:53 +00:00
|
|
|
{item.id === server ? <Check theme={theme} /> : null}
|
2018-08-31 16:46:33 +00:00
|
|
|
</View>
|
2018-09-25 19:28:42 +00:00
|
|
|
</Touch>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 16:46:33 +00:00
|
|
|
|
|
|
|
render() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { servers } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-08-31 16:46:33 +00:00
|
|
|
const maxRows = 4;
|
2018-09-25 19:28:42 +00:00
|
|
|
const initialTop = 41 + (Math.min(servers.length, maxRows) * ROW_HEIGHT);
|
2018-08-31 16:46:33 +00:00
|
|
|
const translateY = this.animatedValue.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [-initialTop, 0]
|
|
|
|
});
|
|
|
|
const backdropOpacity = this.animatedValue.interpolate({
|
|
|
|
inputRange: [0, 1],
|
2019-12-04 16:39:53 +00:00
|
|
|
outputRange: [0, 0.6]
|
2018-08-31 16:46:33 +00:00
|
|
|
});
|
|
|
|
return (
|
2019-12-04 16:39:53 +00:00
|
|
|
<>
|
|
|
|
<TouchableWithoutFeedback onPress={this.close}>
|
|
|
|
<Animated.View style={[styles.backdrop, { backgroundColor: themes[theme].backdropColor, opacity: backdropOpacity }]} />
|
|
|
|
</TouchableWithoutFeedback>
|
2018-08-31 16:46:33 +00:00
|
|
|
<Animated.View
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[
|
|
|
|
styles.dropdownContainer,
|
|
|
|
{
|
|
|
|
transform: [{ translateY }],
|
|
|
|
backgroundColor: themes[theme].backgroundColor,
|
|
|
|
borderColor: themes[theme].separatorColor
|
|
|
|
}
|
|
|
|
]}
|
2018-08-31 16:46:33 +00:00
|
|
|
testID='rooms-list-header-server-dropdown'
|
|
|
|
>
|
2019-12-04 16:39:53 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.dropdownContainerHeader,
|
|
|
|
styles.serverHeader,
|
|
|
|
{ borderColor: themes[theme].separatorColor }
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Text style={[styles.serverHeaderText, { color: themes[theme].auxiliaryText }]}>{I18n.t('Server')}</Text>
|
2018-08-31 16:46:33 +00:00
|
|
|
<TouchableOpacity onPress={this.addServer} testID='rooms-list-header-server-add'>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text style={[styles.serverHeaderAdd, { color: themes[theme].tintColor }]}>{I18n.t('Add_Server')}</Text>
|
2018-08-31 16:46:33 +00:00
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
<FlatList
|
|
|
|
style={{ maxHeight: maxRows * ROW_HEIGHT }}
|
2018-09-25 19:28:42 +00:00
|
|
|
data={servers}
|
2018-08-31 16:46:33 +00:00
|
|
|
keyExtractor={item => item.id}
|
|
|
|
renderItem={this.renderServer}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
2019-11-25 20:01:17 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
2018-08-31 16:46:33 +00:00
|
|
|
/>
|
|
|
|
</Animated.View>
|
2019-12-04 16:39:53 +00:00
|
|
|
</>
|
2018-08-31 16:46:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
closeServerDropdown: state.rooms.closeServerDropdown,
|
|
|
|
server: state.server.server
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
toggleServerDropdown: () => dispatch(toggleServerDropdownAction()),
|
|
|
|
selectServerRequest: server => dispatch(selectServerRequestAction(server)),
|
|
|
|
appStart: () => dispatch(appStartAction('outside'))
|
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default withNavigation(connect(mapStateToProps, mapDispatchToProps)(withTheme(withSplit(ServerDropdown))));
|