vn-verdnaturachat/app/containers/MessageBox/index.js

694 lines
18 KiB
JavaScript
Raw Normal View History

2017-08-09 13:12:00 +00:00
import React from 'react';
import PropTypes from 'prop-types';
import {
View, TextInput, FlatList, Text, TouchableOpacity, Alert
} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
2017-11-21 16:55:32 +00:00
import { connect } from 'react-redux';
import { emojify } from 'react-emojione';
import { KeyboardAccessoryView } from 'react-native-keyboard-input';
import ImagePicker from 'react-native-image-crop-picker';
import { userTyping as userTypingAction } from '../../actions/room';
import {
editRequest as editRequestAction,
editCancel as editCancelAction,
replyCancel as replyCancelAction
} from '../../actions/messages';
import RocketChat from '../../lib/rocketchat';
import styles from './styles';
import MyIcon from '../icons';
import database from '../../lib/realm';
import Avatar from '../Avatar';
import CustomEmoji from '../EmojiPicker/CustomEmoji';
import { emojis } from '../../emojis';
import Recording from './Recording';
import FilesActions from './FilesActions';
import UploadModal from './UploadModal';
import './EmojiKeyboard';
import log from '../../utils/log';
2018-06-01 17:38:13 +00:00
import I18n from '../../i18n';
import ReplyPreview from './ReplyPreview';
const MENTIONS_TRACKING_TYPE_USERS = '@';
const MENTIONS_TRACKING_TYPE_EMOJIS = ':';
const onlyUnique = function onlyUnique(value, index, self) {
return self.indexOf(({ _id }) => value._id === _id) === index;
};
const imagePickerConfig = {
cropping: true,
compressImageQuality: 0.8,
cropperAvoidEmptySpaceAroundImage: false,
cropperChooseText: I18n.t('Choose'),
cropperCancelText: I18n.t('Cancel')
};
@connect(state => ({
roomType: state.room.t,
message: state.messages.message,
replyMessage: state.messages.replyMessage,
replying: state.messages.replyMessage && !!state.messages.replyMessage.msg,
editing: state.messages.editing,
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
username: state.login.user && state.login.user.username
}), dispatch => ({
editCancel: () => dispatch(editCancelAction()),
editRequest: message => dispatch(editRequestAction(message)),
typing: status => dispatch(userTypingAction(status)),
closeReply: () => dispatch(replyCancelAction())
}))
export default class MessageBox extends React.PureComponent {
2017-08-09 13:12:00 +00:00
static propTypes = {
rid: PropTypes.string.isRequired,
baseUrl: PropTypes.string.isRequired,
message: PropTypes.object,
replyMessage: PropTypes.object,
replying: PropTypes.bool,
2017-11-21 17:09:22 +00:00
editing: PropTypes.bool,
username: PropTypes.string,
roomType: PropTypes.string,
editCancel: PropTypes.func.isRequired,
editRequest: PropTypes.func.isRequired,
onSubmit: PropTypes.func.isRequired,
2017-11-24 20:44:52 +00:00
typing: PropTypes.func,
closeReply: PropTypes.func
}
constructor(props) {
super(props);
this.state = {
text: '',
mentions: [],
showEmojiKeyboard: false,
showFilesAction: false,
recording: false,
file: {
isVisible: false
}
};
this.users = [];
this.rooms = [];
this.emojis = [];
this.customEmojis = [];
this.onEmojiSelected = this.onEmojiSelected.bind(this);
}
componentWillReceiveProps(nextProps) {
const { message, replyMessage } = this.props;
if (message !== nextProps.message && nextProps.message.msg) {
this.setState({ text: nextProps.message.msg });
this.component.focus();
} else if (replyMessage !== nextProps.replyMessage && nextProps.replyMessage.msg) {
this.component.focus();
2017-11-24 20:44:52 +00:00
} else if (!nextProps.message) {
this.setState({ text: '' });
}
2017-08-09 13:12:00 +00:00
}
2018-01-25 14:04:20 +00:00
onChangeText(text) {
const { typing } = this.props;
2018-01-25 14:04:20 +00:00
this.setState({ text });
typing(text.length > 0);
2018-01-25 14:04:20 +00:00
requestAnimationFrame(() => {
const { start, end } = this.component._lastNativeSelection;
const cursor = Math.max(start, end);
2018-01-25 14:04:20 +00:00
const lastNativeText = this.component._lastNativeText;
const regexp = /(#|@|:)([a-z0-9._-]+)$/im;
2018-01-25 14:04:20 +00:00
const result = lastNativeText.substr(0, cursor).match(regexp);
if (!result) {
return this.stopTrackingMention();
}
const [, lastChar, name] = result;
this.identifyMentionKeyword(name, lastChar);
});
}
onKeyboardResigned() {
this.closeEmoji();
}
onPressMention(item) {
const { trackingType } = this.state;
const msg = this.component._lastNativeText;
const { start, end } = this.component._lastNativeSelection;
const cursor = Math.max(start, end);
const regexp = /([a-z0-9._-]+)$/im;
const result = msg.substr(0, cursor).replace(regexp, '');
const mentionName = trackingType === MENTIONS_TRACKING_TYPE_EMOJIS
? `${ item.name || item }:`
: (item.username || item.name);
const text = `${ result }${ mentionName } ${ msg.slice(cursor) }`;
this.component.setNativeProps({ text });
this.setState({ text });
this.component.focus();
requestAnimationFrame(() => this.stopTrackingMention());
}
onEmojiSelected(keyboardId, params) {
const { text } = this.state;
const { emoji } = params;
let newText = '';
// if messagebox has an active cursor
if (this.component._lastNativeSelection) {
const { start, end } = this.component._lastNativeSelection;
const cursor = Math.max(start, end);
newText = `${ text.substr(0, cursor) }${ emoji }${ text.substr(cursor) }`;
} else {
// if messagebox doesn't have a cursor, just append selected emoji
newText = `${ text }${ emoji }`;
}
this.component.setNativeProps({ text: newText });
this.setState({ text: newText });
}
get leftButtons() {
const { showEmojiKeyboard } = this.state;
const { editing } = this.props;
if (editing) {
return (
<Icon
style={styles.actionButtons}
name='close'
accessibilityLabel={I18n.t('Cancel_editing')}
accessibilityTraits='button'
onPress={() => this.editCancel()}
testID='messagebox-cancel-editing'
/>
);
2017-08-22 01:24:41 +00:00
}
return !showEmojiKeyboard
? (
<Icon
style={styles.actionButtons}
onPress={() => this.openEmoji()}
accessibilityLabel={I18n.t('Open_emoji_selector')}
accessibilityTraits='button'
name='mood'
testID='messagebox-open-emoji'
/>)
: (
<Icon
onPress={() => this.closeEmoji()}
style={styles.actionButtons}
accessibilityLabel={I18n.t('Close_emoji_selector')}
accessibilityTraits='button'
name='keyboard'
testID='messagebox-close-emoji'
/>);
}
get rightButtons() {
const { text } = this.state;
const icons = [];
if (text) {
icons.push(<MyIcon
style={[styles.actionButtons, { color: '#1D74F5' }]}
name='send'
key='sendIcon'
2018-06-01 17:38:13 +00:00
accessibilityLabel={I18n.t('Send message')}
2017-12-11 20:37:33 +00:00
accessibilityTraits='button'
onPress={() => this.submit(text)}
2018-05-23 13:39:18 +00:00
testID='messagebox-send-message'
/>);
return icons;
}
icons.push(<Icon
style={[styles.actionButtons, { color: '#1D74F5', paddingHorizontal: 10 }]}
name='mic'
2018-03-23 16:55:40 +00:00
key='micIcon'
2018-06-01 17:38:13 +00:00
accessibilityLabel={I18n.t('Send audio message')}
accessibilityTraits='button'
onPress={() => this.recordAudioMessage()}
2018-05-23 13:39:18 +00:00
testID='messagebox-send-audio'
/>);
icons.push(<MyIcon
style={[styles.actionButtons, { color: '#2F343D', fontSize: 16 }]}
name='plus'
key='fileIcon'
2018-06-01 17:38:13 +00:00
accessibilityLabel={I18n.t('Message actions')}
accessibilityTraits='button'
onPress={this.toggleFilesActions}
2018-05-23 13:39:18 +00:00
testID='messagebox-actions'
/>);
return icons;
2017-08-11 18:18:09 +00:00
}
2017-08-09 13:12:00 +00:00
getPermalink = async(message) => {
try {
return await RocketChat.getPermalink(message);
} catch (error) {
return null;
}
}
getFixedMentions(keyword) {
if ('all'.indexOf(keyword) !== -1) {
this.users = [{ _id: -1, username: 'all' }, ...this.users];
}
if ('here'.indexOf(keyword) !== -1) {
this.users = [{ _id: -2, username: 'here' }, ...this.users];
}
}
async getUsers(keyword) {
this.users = database.objects('users');
if (keyword) {
this.users = this.users.filtered('username CONTAINS[c] $0', keyword);
}
this.getFixedMentions(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))
]);
if (results.users && results.users.length) {
database.write(() => {
results.users.forEach((user) => {
database.create('users', user, true);
});
});
}
} catch (e) {
console.warn('spotlight canceled');
} finally {
delete this.oldPromise;
this.users = database.objects('users').filtered('username CONTAINS[c] $0', keyword).slice();
this.getFixedMentions(keyword);
this.setState({ mentions: this.users });
}
}
async getRooms(keyword = '') {
this.roomsCache = this.roomsCache || [];
this.rooms = database.objects('subscriptions')
.filtered('t != $0', 'd');
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))
]);
if (results.rooms && results.rooms.length) {
this.roomsCache = [...this.roomsCache, ...results.rooms].filter(onlyUnique);
}
this.setState({ mentions: [...rooms.slice(), ...results.rooms] });
} catch (e) {
console.warn('spotlight canceled');
} finally {
delete this.oldPromise;
}
}
getEmojis(keyword) {
if (keyword) {
this.customEmojis = database.objects('customEmojis').filtered('name CONTAINS[c] $0', keyword).slice(0, 4);
this.emojis = emojis.filter(emoji => emoji.indexOf(keyword) !== -1).slice(0, 4);
const mergedEmojis = [...this.customEmojis, ...this.emojis];
this.setState({ mentions: mergedEmojis });
}
}
toggleFilesActions = () => {
this.setState(prevState => ({ showFilesAction: !prevState.showFilesAction }));
}
sendImageMessage = async(file) => {
const { rid } = this.props;
this.setState({ file: { isVisible: false } });
const fileInfo = {
name: file.name,
description: file.description,
size: file.size,
type: file.mime,
store: 'Uploads',
path: file.path
2017-08-10 20:09:54 +00:00
};
try {
await RocketChat.sendFileMessage(rid, fileInfo);
} catch (e) {
log('sendImageMessage', e);
}
2017-08-10 20:09:54 +00:00
}
takePhoto = async() => {
try {
const image = await ImagePicker.openCamera(imagePickerConfig);
this.showUploadModal(image);
} catch (e) {
log('takePhoto', e);
}
}
chooseFromLibrary = async() => {
try {
const image = await ImagePicker.openPicker(imagePickerConfig);
this.showUploadModal(image);
} catch (e) {
log('chooseFromLibrary', e);
}
}
showUploadModal = (file) => {
this.setState({ file: { ...file, isVisible: true } });
}
editCancel = () => {
const { editCancel } = this.props;
editCancel();
this.setState({ text: '' });
2017-11-24 20:44:52 +00:00
}
openEmoji = async() => {
await this.setState({
showEmojiKeyboard: true
});
}
recordAudioMessage = async() => {
const recording = await Recording.permission();
this.setState({ recording });
}
finishAudioMessage = async(fileInfo) => {
const { rid } = this.props;
this.setState({
recording: false
});
2018-05-29 17:10:40 +00:00
if (fileInfo) {
try {
await RocketChat.sendFileMessage(rid, fileInfo);
2018-05-29 17:10:40 +00:00
} catch (e) {
if (e && e.error === 'error-file-too-large') {
return Alert.alert(I18n.t(e.error));
2018-05-29 17:10:40 +00:00
}
log('finishAudioMessage', e);
}
}
}
closeEmoji = () => {
this.setState({ showEmojiKeyboard: false });
}
submit = async(message) => {
const {
typing, message: editingMessage, editRequest, onSubmit
} = this.props;
this.setState({ text: '' });
this.closeEmoji();
this.stopTrackingMention();
typing(false);
2018-03-02 21:31:44 +00:00
if (message.trim() === '') {
return;
}
// if is editing a message
const {
editing, replying
} = this.props;
2018-03-02 21:31:44 +00:00
if (editing) {
const { _id, rid } = editingMessage;
editRequest({ _id, msg: message, rid });
} else if (replying) {
const {
username, replyMessage, roomType, closeReply
} = this.props;
const permalink = await this.getPermalink(replyMessage);
let msg = `[ ](${ permalink }) `;
// if original message wasn't sent by current user and neither from a direct room
if (username !== replyMessage.u.username && roomType !== 'd' && replyMessage.mention) {
msg += `@${ replyMessage.u.username } `;
}
msg = `${ msg } ${ message }`;
onSubmit(msg);
closeReply();
2018-03-02 21:31:44 +00:00
} else {
// if is submiting a new message
onSubmit(message);
2018-03-02 21:31:44 +00:00
}
2017-11-24 20:44:52 +00:00
}
updateMentions = (keyword, type) => {
if (type === MENTIONS_TRACKING_TYPE_USERS) {
this.getUsers(keyword);
} else if (type === MENTIONS_TRACKING_TYPE_EMOJIS) {
this.getEmojis(keyword);
} else {
this.getRooms(keyword);
}
}
identifyMentionKeyword(keyword, type) {
this.setState({
showEmojiKeyboard: false,
trackingType: type
});
this.updateMentions(keyword, type);
}
stopTrackingMention() {
this.setState({
mentions: [],
trackingType: ''
});
this.users = [];
this.rooms = [];
this.customEmojis = [];
this.emojis = [];
}
2018-01-19 12:38:14 +00:00
renderFixedMentionItem = item => (
<TouchableOpacity
style={styles.mentionItem}
onPress={() => this.onPressMention(item)}
>
2018-01-19 12:38:14 +00:00
<Text style={styles.fixedMentionAvatar}>{item.username}</Text>
2018-06-01 17:38:13 +00:00
<Text>{item.username === 'here' ? I18n.t('Notify_active_in_this_room') : I18n.t('Notify_all_in_this_room')}</Text>
</TouchableOpacity>
)
renderMentionEmoji = (item) => {
const { baseUrl } = this.props;
if (item.name) {
return (
<CustomEmoji
key='mention-item-avatar'
style={styles.mentionItemCustomEmoji}
emoji={item}
baseUrl={baseUrl}
/>
);
}
return (
<Text
key='mention-item-avatar'
style={styles.mentionItemEmoji}
>
{emojify(`:${ item }:`, { output: 'unicode' })}
</Text>
);
}
2018-01-19 12:38:14 +00:00
renderMentionItem = (item) => {
const { trackingType } = this.state;
const { baseUrl } = this.props;
2018-01-19 12:38:14 +00:00
if (item.username === 'all' || item.username === 'here') {
return this.renderFixedMentionItem(item);
}
return (
<TouchableOpacity
style={styles.mentionItem}
onPress={() => this.onPressMention(item)}
testID={`mention-item-${ trackingType === MENTIONS_TRACKING_TYPE_EMOJIS ? item.name || item : item.username || item.name }`}
2018-01-19 12:38:14 +00:00
>
{trackingType === MENTIONS_TRACKING_TYPE_EMOJIS
? [
this.renderMentionEmoji(item),
<Text key='mention-item-name'>:{ item.name || item }:</Text>
]
: [
<Avatar
key='mention-item-avatar'
style={{ margin: 8 }}
text={item.username || item.name}
size={30}
type={item.username ? 'd' : 'c'}
baseUrl={baseUrl}
/>,
<Text key='mention-item-name'>{ item.username || item.name }</Text>
]
}
2018-01-19 12:38:14 +00:00
</TouchableOpacity>
);
}
renderMentions = () => {
const { mentions, trackingType } = this.state;
if (!trackingType) {
return null;
}
return (
2018-05-23 13:39:18 +00:00
<View key='messagebox-container' testID='messagebox-container'>
<FlatList
style={styles.mentionList}
data={mentions}
renderItem={({ item }) => this.renderMentionItem(item)}
keyExtractor={item => item._id || item.username || item}
2018-05-23 13:39:18 +00:00
keyboardShouldPersistTaps='always'
/>
</View>
);
};
renderReplyPreview = () => {
const {
replyMessage, replying, closeReply, username
} = this.props;
if (!replying) {
return null;
}
return <ReplyPreview key='reply-preview' message={replyMessage} close={closeReply} username={username} />;
};
renderFilesActions = () => {
const { showFilesAction } = this.state;
if (!showFilesAction) {
return null;
}
return (
<FilesActions
key='files-actions'
hideActions={this.toggleFilesActions}
takePhoto={this.takePhoto}
chooseFromLibrary={this.chooseFromLibrary}
/>
);
}
renderContent() {
const { recording, text } = this.state;
const { editing } = this.props;
if (recording) {
return (<Recording onFinish={this.finishAudioMessage} />);
}
2017-08-09 13:12:00 +00:00
return (
[
this.renderMentions(),
<View style={styles.composer} key='messagebox'>
{this.renderReplyPreview()}
<View
style={[styles.textArea, editing && styles.editing]}
testID='messagebox'
>
{this.leftButtons}
<TextInput
ref={component => this.component = component}
style={styles.textBoxInput}
returnKeyType='default'
keyboardType='twitter'
blurOnSubmit={false}
placeholder={I18n.t('New_Message')}
onChangeText={t => this.onChangeText(t)}
value={text}
underlineColorAndroid='transparent'
defaultValue=''
multiline
placeholderTextColor='#9EA2A8'
testID='messagebox-input'
/>
{this.rightButtons}
</View>
</View>
]
);
}
render() {
const { showEmojiKeyboard, file } = this.state;
return (
[
<KeyboardAccessoryView
key='input'
renderContent={() => this.renderContent()}
kbInputRef={this.component}
kbComponent={showEmojiKeyboard ? 'EmojiKeyboard' : null}
onKeyboardResigned={() => this.onKeyboardResigned()}
onItemSelected={this.onEmojiSelected}
trackInteractive
// revealKeyboardInteractive
requiresSameParentToManageScrollView
addBottomView
/>,
this.renderFilesActions(),
<UploadModal
key='upload-modal'
isVisible={(file && file.isVisible)}
file={file}
close={() => this.setState({ file: {} })}
submit={this.sendImageMessage}
/>
]
2017-08-09 13:12:00 +00:00
);
}
}