2017-08-09 13:12:00 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-12-20 20:14:07 +00:00
|
|
|
import { View, TextInput, SafeAreaView, Platform, FlatList, Text, TouchableOpacity } from 'react-native';
|
2017-12-08 19:36:03 +00:00
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
2017-08-10 20:09:54 +00:00
|
|
|
import ImagePicker from 'react-native-image-picker';
|
2017-11-21 16:55:32 +00:00
|
|
|
import { connect } from 'react-redux';
|
2017-12-08 19:36:03 +00:00
|
|
|
import { userTyping } from '../../actions/room';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import { editRequest, editCancel, clearInput } from '../../actions/messages';
|
|
|
|
import styles from './style';
|
|
|
|
import MyIcon from '../icons';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../../lib/realm';
|
2017-12-20 20:14:07 +00:00
|
|
|
import Avatar from '../Avatar';
|
|
|
|
import AnimatedContainer from './AnimatedContainer';
|
|
|
|
|
|
|
|
const MENTIONS_TRACKING_TYPE_USERS = '@';
|
|
|
|
|
|
|
|
const onlyUnique = function onlyUnique(value, index, self) {
|
|
|
|
return self.indexOf(({ _id }) => value._id === _id) === index;
|
|
|
|
};
|
|
|
|
|
2017-11-21 14:55:50 +00:00
|
|
|
@connect(state => ({
|
2017-12-08 19:36:03 +00:00
|
|
|
room: state.room,
|
2017-11-21 14:55:50 +00:00
|
|
|
message: state.messages.message,
|
2017-12-20 20:14:07 +00:00
|
|
|
editing: state.messages.editing,
|
2017-12-27 15:22:06 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : ''
|
2017-11-21 14:55:50 +00:00
|
|
|
}), dispatch => ({
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel: () => dispatch(editCancel()),
|
2017-11-21 17:05:14 +00:00
|
|
|
editRequest: message => dispatch(editRequest(message)),
|
2017-11-24 20:44:52 +00:00
|
|
|
typing: status => dispatch(userTyping(status)),
|
|
|
|
clearInput: () => dispatch(clearInput())
|
2017-11-21 14:55:50 +00:00
|
|
|
}))
|
|
|
|
export default class MessageBox extends React.Component {
|
2017-08-09 13:12:00 +00:00
|
|
|
static propTypes = {
|
2017-08-10 20:09:54 +00:00
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2017-11-21 14:55:50 +00:00
|
|
|
rid: PropTypes.string.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel: PropTypes.func.isRequired,
|
2017-11-21 14:55:50 +00:00
|
|
|
editRequest: PropTypes.func.isRequired,
|
2017-12-20 20:14:07 +00:00
|
|
|
baseUrl: PropTypes.string.isRequired,
|
2017-11-21 14:55:50 +00:00
|
|
|
message: PropTypes.object,
|
2017-11-21 17:09:22 +00:00
|
|
|
editing: PropTypes.bool,
|
2017-11-24 20:44:52 +00:00
|
|
|
typing: PropTypes.func,
|
|
|
|
clearInput: PropTypes.func
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:27:06 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2017-12-08 19:36:03 +00:00
|
|
|
height: 20,
|
2017-12-20 20:14:07 +00:00
|
|
|
messageboxHeight: 0,
|
|
|
|
text: '',
|
|
|
|
mentions: [],
|
|
|
|
showAnimatedContainer: false
|
2017-11-28 16:27:06 +00:00
|
|
|
};
|
2017-12-20 20:14:07 +00:00
|
|
|
this.users = [];
|
|
|
|
this.rooms = [];
|
2017-11-28 16:27:06 +00:00
|
|
|
}
|
2017-11-21 14:55:50 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2017-12-20 20:14:07 +00:00
|
|
|
if (this.props.message !== nextProps.message && nextProps.message.msg) {
|
|
|
|
this.setState({ text: nextProps.message.msg });
|
2017-11-21 14:55:50 +00:00
|
|
|
this.component.focus();
|
2017-11-24 20:44:52 +00:00
|
|
|
} else if (!nextProps.message) {
|
2017-12-20 20:14:07 +00:00
|
|
|
this.setState({ text: '' });
|
2017-11-21 14:55:50 +00:00
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
}
|
2017-12-20 20:14:07 +00:00
|
|
|
|
|
|
|
onChange() {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
const { start, end } = this.component._lastNativeSelection;
|
|
|
|
|
|
|
|
const cursor = Math.max(start, end);
|
|
|
|
|
|
|
|
const text = this.component._lastNativeText;
|
|
|
|
|
|
|
|
const regexp = /(#|@)([a-z._-]+)$/im;
|
|
|
|
|
|
|
|
const result = text.substr(0, cursor).match(regexp);
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
return this.stopTrackingMention();
|
|
|
|
}
|
|
|
|
const [, lastChar, name] = result;
|
|
|
|
|
|
|
|
this.identifyMentionKeyword(name, lastChar);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-08 19:36:03 +00:00
|
|
|
onChangeText(text) {
|
|
|
|
this.setState({ text });
|
|
|
|
}
|
2017-12-20 20:14:07 +00:00
|
|
|
|
2017-12-08 19:36:03 +00:00
|
|
|
get leftButtons() {
|
2017-11-24 17:21:21 +00:00
|
|
|
const { editing } = this.props;
|
2017-11-21 14:55:50 +00:00
|
|
|
if (editing) {
|
2017-12-11 20:37:33 +00:00
|
|
|
return (<Icon
|
|
|
|
style={styles.actionButtons}
|
|
|
|
name='ios-close'
|
|
|
|
accessibilityLabel='Cancel editing'
|
|
|
|
accessibilityTraits='button'
|
|
|
|
onPress={() => this.editCancel()}
|
|
|
|
/>);
|
2017-08-22 01:24:41 +00:00
|
|
|
}
|
2017-12-11 20:37:33 +00:00
|
|
|
return !this.state.emoji ? (<Icon
|
|
|
|
style={styles.actionButtons}
|
|
|
|
onPress={() => this.openEmoji()}
|
|
|
|
accessibilityLabel='Open emoji selector'
|
|
|
|
accessibilityTraits='button'
|
|
|
|
name='md-happy'
|
|
|
|
/>) : (<Icon
|
|
|
|
onPress={() => this.openEmoji()}
|
|
|
|
style={styles.actionButtons}
|
|
|
|
accessibilityLabel='Close emoji selector'
|
|
|
|
accessibilityTraits='button'
|
|
|
|
name='md-sad'
|
|
|
|
/>);
|
2017-12-08 19:36:03 +00:00
|
|
|
}
|
|
|
|
get rightButtons() {
|
|
|
|
const icons = [];
|
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
if (this.state.text) {
|
2017-12-08 19:36:03 +00:00
|
|
|
icons.push(<MyIcon
|
|
|
|
style={[styles.actionButtons, { color: '#1D74F5' }]}
|
|
|
|
name='send'
|
|
|
|
key='sendIcon'
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel='Send message'
|
|
|
|
accessibilityTraits='button'
|
2017-12-20 20:14:07 +00:00
|
|
|
onPress={() => this.submit(this.state.text)}
|
2017-12-08 19:36:03 +00:00
|
|
|
/>);
|
|
|
|
}
|
|
|
|
icons.push(<MyIcon
|
|
|
|
style={[styles.actionButtons, { color: '#2F343D', fontSize: 16 }]}
|
|
|
|
name='plus'
|
|
|
|
key='fileIcon'
|
2017-12-11 20:37:33 +00:00
|
|
|
accessibilityLabel='Message actions'
|
|
|
|
accessibilityTraits='button'
|
2017-12-08 19:36:03 +00:00
|
|
|
onPress={() => this.addFile()}
|
|
|
|
/>);
|
|
|
|
return icons;
|
2017-08-11 18:18:09 +00:00
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
|
2017-12-08 19:36:03 +00:00
|
|
|
updateSize = (height) => {
|
|
|
|
this.setState({ height: height + (Platform.OS === 'ios' ? 0 : 0) });
|
|
|
|
}
|
2017-12-20 20:14:07 +00:00
|
|
|
|
2017-08-10 20:09:54 +00:00
|
|
|
addFile = () => {
|
|
|
|
const options = {
|
2017-12-26 14:46:14 +00:00
|
|
|
maxHeight: 1960,
|
|
|
|
maxWidth: 1960,
|
|
|
|
quality: 0.8,
|
2017-08-10 20:09:54 +00:00
|
|
|
customButtons: [{
|
|
|
|
name: 'import', title: 'Import File From'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
ImagePicker.showImagePicker(options, (response) => {
|
|
|
|
if (response.didCancel) {
|
|
|
|
console.log('User cancelled image picker');
|
|
|
|
} else if (response.error) {
|
|
|
|
console.log('ImagePicker Error: ', response.error);
|
|
|
|
} else if (response.customButton) {
|
|
|
|
console.log('User tapped custom button: ', response.customButton);
|
|
|
|
} else {
|
|
|
|
const fileInfo = {
|
|
|
|
name: response.fileName,
|
|
|
|
size: response.fileSize,
|
|
|
|
type: response.type || 'image/jpeg',
|
|
|
|
// description: '',
|
|
|
|
store: 'Uploads'
|
|
|
|
};
|
2017-08-15 19:28:46 +00:00
|
|
|
RocketChat.sendFileMessage(this.props.rid, fileInfo, response.data);
|
2017-08-10 20:09:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-24 20:44:52 +00:00
|
|
|
editCancel() {
|
|
|
|
this.props.editCancel();
|
2017-12-20 20:14:07 +00:00
|
|
|
this.setState({ text: '' });
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
2017-12-08 19:36:03 +00:00
|
|
|
openEmoji() {
|
|
|
|
this.setState({ emoji: !this.state.emoji });
|
|
|
|
}
|
|
|
|
submit(message) {
|
|
|
|
this.setState({ text: '' });
|
2017-12-20 20:14:07 +00:00
|
|
|
this.stopTrackingMention();
|
2017-12-08 19:36:03 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.props.typing(false);
|
|
|
|
if (message.trim() === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if is editing a message
|
|
|
|
const { editing } = this.props;
|
|
|
|
if (editing) {
|
|
|
|
const { _id, rid } = this.props.message;
|
|
|
|
this.props.editRequest({ _id, msg: message, rid });
|
|
|
|
} else {
|
|
|
|
// if is submiting a new message
|
|
|
|
this.props.onSubmit(message);
|
|
|
|
}
|
2017-12-13 15:00:26 +00:00
|
|
|
this.props.clearInput();
|
2017-12-08 19:36:03 +00:00
|
|
|
});
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:14:07 +00:00
|
|
|
async _getUsers(keyword) {
|
2017-12-27 15:22:06 +00:00
|
|
|
this.users = database.objects('users');
|
2017-12-20 20:14:07 +00:00
|
|
|
if (keyword) {
|
|
|
|
this.users = this.users.filtered('username CONTAINS[c] $0', keyword);
|
|
|
|
}
|
|
|
|
this.setState({ mentions: this.users.slice() });
|
|
|
|
|
|
|
|
const usernames = [];
|
|
|
|
|
|
|
|
if (keyword && this.users.length > 7) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.users.forEach(user => usernames.push(user.username));
|
|
|
|
|
|
|
|
if (this.oldPromise) {
|
|
|
|
this.oldPromise();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const results = await Promise.race([
|
|
|
|
RocketChat.spotlight(keyword, usernames, { users: true }),
|
|
|
|
new Promise((resolve, reject) => (this.oldPromise = reject))
|
|
|
|
]);
|
2017-12-27 15:22:06 +00:00
|
|
|
database.write(() => {
|
2017-12-20 20:14:07 +00:00
|
|
|
results.users.forEach((user) => {
|
2017-12-27 15:22:06 +00:00
|
|
|
database.create('users', user, true);
|
2017-12-20 20:14:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log('spotlight canceled');
|
|
|
|
} finally {
|
|
|
|
delete this.oldPromise;
|
2017-12-27 15:22:06 +00:00
|
|
|
this.users = database.objects('users').filtered('username CONTAINS[c] $0', keyword);
|
2017-12-20 20:14:07 +00:00
|
|
|
this.setState({ mentions: this.users.slice() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _getRooms(keyword = '') {
|
|
|
|
this.roomsCache = this.roomsCache || [];
|
2017-12-27 15:22:06 +00:00
|
|
|
this.rooms = database.objects('subscriptions')
|
|
|
|
.filtered('t != $0', 'd');
|
2017-12-20 20:14:07 +00:00
|
|
|
if (keyword) {
|
|
|
|
this.rooms = this.rooms.filtered('name CONTAINS[c] $0', keyword);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rooms = [];
|
|
|
|
this.rooms.forEach(room => rooms.push(room));
|
|
|
|
|
|
|
|
this.roomsCache.forEach((room) => {
|
|
|
|
if (room.name && room.name.toUpperCase().indexOf(keyword.toUpperCase()) !== -1) {
|
|
|
|
rooms.push(room);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (rooms.length > 3) {
|
|
|
|
this.setState({ mentions: rooms });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.oldPromise) {
|
|
|
|
this.oldPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const results = await Promise.race([
|
|
|
|
RocketChat.spotlight(keyword, [...rooms, ...this.roomsCache].map(r => r.name), { rooms: true }),
|
|
|
|
new Promise((resolve, reject) => (this.oldPromise = reject))
|
|
|
|
]);
|
|
|
|
this.roomsCache = [...this.roomsCache, ...results.rooms].filter(onlyUnique);
|
|
|
|
this.setState({ mentions: [...rooms.slice(), ...results.rooms] });
|
|
|
|
} catch (e) {
|
|
|
|
console.log('spotlight canceled');
|
|
|
|
} finally {
|
|
|
|
delete this.oldPromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stopTrackingMention() {
|
|
|
|
this.setState({
|
|
|
|
showAnimatedContainer: false,
|
|
|
|
mentions: []
|
|
|
|
});
|
|
|
|
this.users = [];
|
|
|
|
this.rooms = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
identifyMentionKeyword(keyword, type) {
|
|
|
|
this.updateMentions(keyword, type);
|
|
|
|
this.setState({
|
|
|
|
showAnimatedContainer: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateMentions = (keyword, type) => {
|
|
|
|
if (type === MENTIONS_TRACKING_TYPE_USERS) {
|
|
|
|
this._getUsers(keyword);
|
|
|
|
} else {
|
|
|
|
this._getRooms(keyword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onPressMention(item) {
|
|
|
|
const msg = this.component._lastNativeText;
|
|
|
|
|
|
|
|
const { start, end } = this.component._lastNativeSelection;
|
|
|
|
|
|
|
|
const cursor = Math.max(start, end);
|
|
|
|
|
|
|
|
const regexp = /([a-z._-]+)$/im;
|
|
|
|
|
|
|
|
const result = msg.substr(0, cursor).replace(regexp, '');
|
|
|
|
const text = `${ result }${ item.username || item.name } ${ msg.slice(cursor) }`;
|
|
|
|
this.component.setNativeProps({ text });
|
|
|
|
this.setState({ text });
|
|
|
|
this.component.focus();
|
|
|
|
requestAnimationFrame(() => this.stopTrackingMention());
|
|
|
|
}
|
|
|
|
renderMentionItem = item => (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={styles.mentionItem}
|
|
|
|
onPress={() => this._onPressMention(item)}
|
|
|
|
>
|
|
|
|
<Avatar
|
|
|
|
style={{ margin: 8 }}
|
|
|
|
text={item.username || item.name}
|
|
|
|
size={30}
|
|
|
|
baseUrl={this.props.baseUrl}
|
|
|
|
/>
|
|
|
|
<Text>{item.username || item.name }</Text>
|
|
|
|
</TouchableOpacity>
|
|
|
|
)
|
|
|
|
renderMentions() {
|
|
|
|
const usersList = (
|
|
|
|
<FlatList
|
|
|
|
style={styles.mentionList}
|
|
|
|
data={this.state.mentions}
|
|
|
|
renderItem={({ item }) => this.renderMentionItem(item)}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
keyboardDismissMode='interactive'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const { showAnimatedContainer, messageboxHeight } = this.state;
|
|
|
|
return <AnimatedContainer visible={showAnimatedContainer} subview={usersList} messageboxHeight={messageboxHeight} />;
|
|
|
|
}
|
2017-08-09 13:12:00 +00:00
|
|
|
render() {
|
2017-11-28 16:27:06 +00:00
|
|
|
const { height } = this.state;
|
2017-08-09 13:12:00 +00:00
|
|
|
return (
|
2017-12-20 20:14:07 +00:00
|
|
|
<View>
|
|
|
|
<SafeAreaView
|
|
|
|
style={[styles.textBox, (this.props.editing ? styles.editing : null)]}
|
|
|
|
onLayout={event => this.setState({ messageboxHeight: event.nativeEvent.layout.height })}
|
|
|
|
>
|
|
|
|
<View style={styles.textArea}>
|
|
|
|
{this.leftButtons}
|
|
|
|
<TextInput
|
|
|
|
ref={component => this.component = component}
|
|
|
|
style={[styles.textBoxInput, { height }]}
|
|
|
|
returnKeyType='default'
|
|
|
|
blurOnSubmit={false}
|
|
|
|
placeholder='New Message'
|
|
|
|
onChangeText={text => this.onChangeText(text)}
|
|
|
|
onChange={event => this.onChange(event)}
|
|
|
|
value={this.state.text}
|
|
|
|
underlineColorAndroid='transparent'
|
|
|
|
defaultValue=''
|
|
|
|
multiline
|
|
|
|
placeholderTextColor='#9EA2A8'
|
|
|
|
onContentSizeChange={e => this.updateSize(e.nativeEvent.contentSize.height)}
|
|
|
|
/>
|
|
|
|
{this.rightButtons}
|
|
|
|
</View>
|
|
|
|
</SafeAreaView>
|
|
|
|
{this.renderMentions()}
|
|
|
|
</View>
|
2017-08-09 13:12:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|