2018-08-31 18:13:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
2019-03-01 16:49:11 +00:00
|
|
|
View, StyleSheet, FlatList, Text
|
2018-09-25 19:28:42 +00:00
|
|
|
} from 'react-native';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2021-03-15 20:16:34 +00:00
|
|
|
import * as List from '../containers/List';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
import Touch from '../utils/touch';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2018-08-31 18:13:30 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import UserItem from '../presentation/UserItem';
|
|
|
|
import sharedStyles from './Styles';
|
|
|
|
import I18n from '../i18n';
|
2020-07-30 13:26:17 +00:00
|
|
|
import log, { logEvent, events } from '../utils/log';
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
2019-03-12 16:23:06 +00:00
|
|
|
import { CustomIcon } from '../lib/Icons';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import { withTheme } from '../theme';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../selectors/login';
|
2020-03-30 19:50:27 +00:00
|
|
|
import Navigation from '../lib/Navigation';
|
2020-04-01 12:28:54 +00:00
|
|
|
import { createChannelRequest } from '../actions/createChannel';
|
2020-05-13 19:02:57 +00:00
|
|
|
import { goRoom } from '../utils/goRoom';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2021-03-15 20:16:34 +00:00
|
|
|
const QUERY_SIZE = 50;
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
const styles = StyleSheet.create({
|
2020-04-01 12:28:54 +00:00
|
|
|
button: {
|
2019-12-04 16:39:53 +00:00
|
|
|
height: 46,
|
2018-08-31 18:13:30 +00:00
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonIcon: {
|
2019-06-10 16:22:35 +00:00
|
|
|
marginLeft: 18,
|
2020-03-30 19:50:27 +00:00
|
|
|
marginRight: 16
|
2018-08-31 18:13:30 +00:00
|
|
|
},
|
2020-04-01 12:28:54 +00:00
|
|
|
buttonText: {
|
2019-03-29 19:36:07 +00:00
|
|
|
fontSize: 17,
|
|
|
|
...sharedStyles.textRegular
|
2020-04-01 12:28:54 +00:00
|
|
|
},
|
|
|
|
buttonContainer: {
|
|
|
|
paddingVertical: 25
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class NewMessageView extends React.Component {
|
2020-06-15 14:00:46 +00:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
2020-10-30 16:15:58 +00:00
|
|
|
headerLeft: () => <HeaderButton.CloseModal navigation={navigation} testID='new-message-view-close' />,
|
2019-03-12 16:23:06 +00:00
|
|
|
title: I18n.t('New_Message')
|
|
|
|
})
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2018-09-11 16:32:52 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2019-02-07 19:58:20 +00:00
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
2019-12-04 16:39:53 +00:00
|
|
|
}),
|
2020-04-01 12:28:54 +00:00
|
|
|
createChannel: PropTypes.func,
|
|
|
|
maxUsers: PropTypes.number,
|
2020-06-15 14:00:46 +00:00
|
|
|
theme: PropTypes.string,
|
|
|
|
isMasterDetail: PropTypes.bool
|
2018-08-31 18:13:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.init();
|
2018-08-31 18:13:30 +00:00
|
|
|
this.state = {
|
2019-09-16 20:26:32 +00:00
|
|
|
search: [],
|
|
|
|
chats: []
|
2018-08-31 18:13:30 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
init = async() => {
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-03-15 20:16:34 +00:00
|
|
|
const chats = await db.collections
|
2019-09-16 20:26:32 +00:00
|
|
|
.get('subscriptions')
|
2021-03-15 20:16:34 +00:00
|
|
|
.query(
|
|
|
|
Q.where('t', 'd'),
|
|
|
|
Q.experimentalTake(QUERY_SIZE),
|
|
|
|
Q.experimentalSortBy('room_updated_at', Q.desc)
|
|
|
|
)
|
|
|
|
.fetch();
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-03-15 20:16:34 +00:00
|
|
|
this.setState({ chats });
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChangeText(text) {
|
|
|
|
this.search(text);
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
dismiss = () => {
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
return navigation.pop();
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
search = async(text) => {
|
|
|
|
const result = await RocketChat.search({ text, filterRooms: false });
|
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createChannel = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_CHANNEL);
|
2019-03-12 16:23:06 +00:00
|
|
|
const { navigation } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', { nextAction: () => navigation.navigate('CreateChannelView') });
|
|
|
|
}
|
|
|
|
|
|
|
|
createGroupChat = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_GROUP_CHAT);
|
2020-04-01 12:28:54 +00:00
|
|
|
const { createChannel, maxUsers, navigation } = this.props;
|
|
|
|
navigation.navigate('SelectedUsersViewCreateChannel', {
|
|
|
|
nextAction: () => createChannel({ group: true }),
|
|
|
|
buttonText: I18n.t('Create'),
|
|
|
|
maxUsers
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
goRoom = (item) => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CHAT_WITH_USER);
|
2020-06-15 14:00:46 +00:00
|
|
|
const { isMasterDetail, navigation } = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.pop();
|
|
|
|
}
|
|
|
|
goRoom({ item, isMasterDetail });
|
|
|
|
}
|
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
renderButton = ({
|
|
|
|
onPress, testID, title, icon, first
|
|
|
|
}) => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<Touch
|
|
|
|
onPress={onPress}
|
|
|
|
style={{ backgroundColor: themes[theme].backgroundColor }}
|
|
|
|
testID={testID}
|
|
|
|
theme={theme}
|
|
|
|
>
|
|
|
|
<View style={[first ? sharedStyles.separatorVertical : sharedStyles.separatorBottom, styles.button, { borderColor: themes[theme].separatorColor }]}>
|
|
|
|
<CustomIcon style={[styles.buttonIcon, { color: themes[theme].tintColor }]} size={24} name={icon} />
|
|
|
|
<Text style={[styles.buttonText, { color: themes[theme].tintColor }]}>{title}</Text>
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
);
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
createDiscussion = () => {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.NEW_MSG_CREATE_DISCUSSION);
|
2020-03-30 19:50:27 +00:00
|
|
|
Navigation.navigate('CreateDiscussionView');
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderHeader = () => {
|
2020-04-01 12:28:54 +00:00
|
|
|
const { maxUsers, theme } = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
return (
|
|
|
|
<View style={{ backgroundColor: themes[theme].auxiliaryBackground }}>
|
|
|
|
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='new-message-view-search' />
|
2020-04-01 12:28:54 +00:00
|
|
|
<View style={styles.buttonContainer}>
|
|
|
|
{this.renderButton({
|
|
|
|
onPress: this.createChannel,
|
|
|
|
title: I18n.t('Create_Channel'),
|
2020-07-27 19:53:33 +00:00
|
|
|
icon: 'channel-public',
|
2020-04-01 12:28:54 +00:00
|
|
|
testID: 'new-message-view-create-channel',
|
|
|
|
first: true
|
|
|
|
})}
|
|
|
|
{maxUsers > 2 ? this.renderButton({
|
|
|
|
onPress: this.createGroupChat,
|
|
|
|
title: I18n.t('Create_Direct_Messages'),
|
|
|
|
icon: 'team',
|
|
|
|
testID: 'new-message-view-create-direct-message'
|
|
|
|
}) : null}
|
|
|
|
{this.renderButton({
|
|
|
|
onPress: this.createDiscussion,
|
|
|
|
title: I18n.t('Create_Discussion'),
|
2020-07-27 19:53:33 +00:00
|
|
|
icon: 'discussions',
|
2020-04-01 12:28:54 +00:00
|
|
|
testID: 'new-message-view-create-discussion'
|
|
|
|
})}
|
|
|
|
</View>
|
2019-12-04 16:39:53 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
renderItem = ({ item, index }) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { baseUrl, user, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
let style = { borderColor: themes[theme].separatorColor };
|
2018-08-31 18:13:30 +00:00
|
|
|
if (index === 0) {
|
2019-12-04 16:39:53 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorTop };
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (search.length > 0 && index === search.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (search.length === 0 && index === chats.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={item.search ? item.name : item.fname}
|
|
|
|
username={item.search ? item.username : item.name}
|
2020-06-15 14:00:46 +00:00
|
|
|
onPress={() => this.goRoom(item)}
|
2018-09-25 19:28:42 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-08-31 18:13:30 +00:00
|
|
|
testID={`new-message-view-item-${ item.name }`}
|
|
|
|
style={style}
|
2019-02-07 19:58:20 +00:00
|
|
|
user={user}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-08-31 18:13:30 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderList = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
2019-09-16 20:26:32 +00:00
|
|
|
data={search.length > 0 ? search : chats}
|
2018-09-25 19:28:42 +00:00
|
|
|
extraData={this.state}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
renderItem={this.renderItem}
|
2021-03-15 20:16:34 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={{ backgroundColor: themes[theme].backgroundColor }}
|
2018-09-25 19:28:42 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2020-10-30 13:59:44 +00:00
|
|
|
render() {
|
2019-12-04 16:39:53 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='new-message-view'>
|
|
|
|
<StatusBar />
|
2019-12-04 16:39:53 +00:00
|
|
|
{this.renderList()}
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
2020-04-01 12:28:54 +00:00
|
|
|
maxUsers: state.settings.DirectMesssage_maxUsers || 1,
|
2020-02-11 14:09:14 +00:00
|
|
|
user: getUserSelector(state)
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
createChannel: params => dispatch(createChannelRequest(params))
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(NewMessageView));
|