2019-05-10 17:09:07 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { FlatList, View, Text } from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
2021-03-15 20:16:34 +00:00
|
|
|
import { dequal } from 'dequal';
|
2019-05-10 17:09:07 +00:00
|
|
|
|
|
|
|
import styles from './styles';
|
2020-05-08 17:07:58 +00:00
|
|
|
import Message from '../../containers/message';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
2019-05-10 17:09:07 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
|
|
|
import getFileUrlFromMessage from '../../lib/methods/helpers/getFileUrlFromMessage';
|
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-06-15 19:35:45 +00:00
|
|
|
import { withActionSheet } from '../../containers/ActionSheet';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2021-06-15 20:13:42 +00:00
|
|
|
import getThreadName from '../../lib/methods/getThreadName';
|
2019-05-10 17:09:07 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class MessagesView extends React.Component {
|
2019-05-10 17:09:07 +00:00
|
|
|
static propTypes = {
|
|
|
|
user: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string,
|
2019-09-16 20:26:32 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2019-12-04 16:39:53 +00:00
|
|
|
customEmojis: PropTypes.object,
|
2020-06-15 19:35:45 +00:00
|
|
|
theme: PropTypes.string,
|
2020-08-26 14:06:14 +00:00
|
|
|
showActionSheet: PropTypes.func,
|
2021-06-15 20:13:42 +00:00
|
|
|
useRealName: PropTypes.bool,
|
|
|
|
isMasterDetail: PropTypes.bool
|
2019-05-10 17:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-05-10 17:09:07 +00:00
|
|
|
this.state = {
|
|
|
|
loading: false,
|
2019-05-20 20:43:50 +00:00
|
|
|
messages: [],
|
2019-08-26 16:56:39 +00:00
|
|
|
fileLoading: true
|
2019-05-10 17:09:07 +00:00
|
|
|
};
|
2020-07-24 19:58:14 +00:00
|
|
|
this.setHeader();
|
2020-06-15 14:00:46 +00:00
|
|
|
this.rid = props.route.params?.rid;
|
|
|
|
this.t = props.route.params?.t;
|
|
|
|
this.content = this.defineMessagesViewContent(props.route.params?.name);
|
2019-05-10 17:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-08-26 16:56:39 +00:00
|
|
|
const {
|
2019-12-18 21:13:11 +00:00
|
|
|
loading, messages, fileLoading
|
2019-08-26 16:56:39 +00:00
|
|
|
} = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-10 17:09:07 +00:00
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-03-15 20:16:34 +00:00
|
|
|
if (!dequal(nextState.messages, messages)) {
|
2019-05-10 17:09:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-08-26 16:56:39 +00:00
|
|
|
if (fileLoading !== nextState.fileLoading) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-10 17:09:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-24 19:58:14 +00:00
|
|
|
|
|
|
|
setHeader = () => {
|
|
|
|
const { route, navigation } = this.props;
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t(route.params?.name)
|
|
|
|
});
|
|
|
|
}
|
2019-05-10 17:09:07 +00:00
|
|
|
|
2020-02-17 16:06:46 +00:00
|
|
|
navToRoomInfo = (navParam) => {
|
|
|
|
const { navigation, user } = this.props;
|
|
|
|
if (navParam.rid === user.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
navigation.navigate('RoomInfoView', navParam);
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:13:42 +00:00
|
|
|
jumpToMessage = async({ item }) => {
|
|
|
|
const { navigation, isMasterDetail } = this.props;
|
|
|
|
let params = {
|
|
|
|
rid: this.rid,
|
|
|
|
jumpToMessageId: item._id,
|
|
|
|
t: this.t,
|
|
|
|
room: this.room
|
|
|
|
};
|
|
|
|
if (item.tmid) {
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
navigation.pop(2);
|
|
|
|
}
|
|
|
|
params = {
|
|
|
|
...params,
|
|
|
|
tmid: item.tmid,
|
|
|
|
name: await getThreadName(this.rid, item.tmid, item._id),
|
|
|
|
t: 'thread'
|
|
|
|
};
|
|
|
|
navigation.push('RoomView', params);
|
|
|
|
} else {
|
|
|
|
navigation.navigate('RoomView', params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:09:07 +00:00
|
|
|
defineMessagesViewContent = (name) => {
|
2020-08-26 14:06:14 +00:00
|
|
|
const {
|
|
|
|
user, baseUrl, theme, useRealName
|
|
|
|
} = this.props;
|
2019-05-10 17:09:07 +00:00
|
|
|
const renderItemCommonProps = item => ({
|
2020-05-08 17:07:58 +00:00
|
|
|
item,
|
2019-05-10 17:09:07 +00:00
|
|
|
baseUrl,
|
|
|
|
user,
|
|
|
|
author: item.u || item.user,
|
|
|
|
timeFormat: 'MMM Do YYYY, h:mm:ss a',
|
2019-05-20 20:43:50 +00:00
|
|
|
isEdited: !!item.editedAt,
|
|
|
|
isHeader: true,
|
2021-06-15 20:13:42 +00:00
|
|
|
isThreadRoom: true,
|
2019-05-20 20:43:50 +00:00
|
|
|
attachments: item.attachments || [],
|
2020-08-26 14:06:14 +00:00
|
|
|
useRealName,
|
2019-12-18 21:13:11 +00:00
|
|
|
showAttachment: this.showAttachment,
|
2020-02-17 16:06:46 +00:00
|
|
|
getCustomEmoji: this.getCustomEmoji,
|
2021-06-15 20:13:42 +00:00
|
|
|
navToRoomInfo: this.navToRoomInfo,
|
|
|
|
onPress: () => this.jumpToMessage({ item })
|
2019-05-10 17:09:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return ({
|
|
|
|
// Files Messages Screen
|
|
|
|
Files: {
|
|
|
|
name: I18n.t('Files'),
|
|
|
|
fetchFunc: async() => {
|
2020-07-17 18:59:18 +00:00
|
|
|
const { messages } = this.state;
|
2019-05-10 17:09:07 +00:00
|
|
|
const result = await RocketChat.getFiles(this.rid, this.t, messages.length);
|
|
|
|
return { ...result, messages: result.files };
|
|
|
|
},
|
|
|
|
noDataMsg: I18n.t('No_files'),
|
|
|
|
testID: 'room-files-view',
|
2020-05-08 17:07:58 +00:00
|
|
|
renderItem: item => (
|
|
|
|
<Message
|
|
|
|
{...renderItemCommonProps(item)}
|
|
|
|
item={{
|
|
|
|
...item,
|
|
|
|
u: item.user,
|
2020-08-25 20:09:52 +00:00
|
|
|
ts: item.ts || item.uploadedAt,
|
2020-05-08 17:07:58 +00:00
|
|
|
attachments: [{
|
2019-05-10 17:09:07 +00:00
|
|
|
title: item.name,
|
|
|
|
description: item.description,
|
2020-05-08 17:07:58 +00:00
|
|
|
...getFileUrlFromMessage(item)
|
|
|
|
}]
|
|
|
|
}}
|
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
)
|
2019-05-10 17:09:07 +00:00
|
|
|
},
|
|
|
|
// Mentions Messages Screen
|
|
|
|
Mentions: {
|
|
|
|
name: I18n.t('Mentions'),
|
2020-07-17 18:59:18 +00:00
|
|
|
fetchFunc: () => {
|
|
|
|
const { messages } = this.state;
|
|
|
|
return RocketChat.getMessages(
|
|
|
|
this.rid,
|
|
|
|
this.t,
|
|
|
|
{ 'mentions._id': { $in: [user.id] } },
|
|
|
|
messages.length
|
|
|
|
);
|
|
|
|
},
|
2019-05-10 17:09:07 +00:00
|
|
|
noDataMsg: I18n.t('No_mentioned_messages'),
|
|
|
|
testID: 'mentioned-messages-view',
|
|
|
|
renderItem: item => (
|
|
|
|
<Message
|
|
|
|
{...renderItemCommonProps(item)}
|
|
|
|
msg={item.msg}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-05-10 17:09:07 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
// Starred Messages Screen
|
|
|
|
Starred: {
|
|
|
|
name: I18n.t('Starred'),
|
2020-07-17 18:59:18 +00:00
|
|
|
fetchFunc: () => {
|
|
|
|
const { messages } = this.state;
|
|
|
|
return RocketChat.getMessages(
|
|
|
|
this.rid,
|
|
|
|
this.t,
|
|
|
|
{ 'starred._id': { $in: [user.id] } },
|
|
|
|
messages.length
|
|
|
|
);
|
|
|
|
},
|
2019-05-10 17:09:07 +00:00
|
|
|
noDataMsg: I18n.t('No_starred_messages'),
|
|
|
|
testID: 'starred-messages-view',
|
|
|
|
renderItem: item => (
|
|
|
|
<Message
|
|
|
|
{...renderItemCommonProps(item)}
|
|
|
|
msg={item.msg}
|
|
|
|
onLongPress={() => this.onLongPress(item)}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-05-10 17:09:07 +00:00
|
|
|
/>
|
|
|
|
),
|
2020-06-15 19:35:45 +00:00
|
|
|
action: message => ({ title: I18n.t('Unstar'), icon: message.starred ? 'star-filled' : 'star', onPress: this.handleActionPress }),
|
2019-09-16 20:26:32 +00:00
|
|
|
handleActionPress: message => RocketChat.toggleStarMessage(message._id, message.starred)
|
2019-05-10 17:09:07 +00:00
|
|
|
},
|
|
|
|
// Pinned Messages Screen
|
|
|
|
Pinned: {
|
|
|
|
name: I18n.t('Pinned'),
|
2020-07-17 18:59:18 +00:00
|
|
|
fetchFunc: () => {
|
|
|
|
const { messages } = this.state;
|
|
|
|
return RocketChat.getMessages(this.rid, this.t, { pinned: true }, messages.length);
|
|
|
|
},
|
2019-05-10 17:09:07 +00:00
|
|
|
noDataMsg: I18n.t('No_pinned_messages'),
|
|
|
|
testID: 'pinned-messages-view',
|
|
|
|
renderItem: item => (
|
|
|
|
<Message
|
|
|
|
{...renderItemCommonProps(item)}
|
|
|
|
msg={item.msg}
|
|
|
|
onLongPress={() => this.onLongPress(item)}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-05-10 17:09:07 +00:00
|
|
|
/>
|
|
|
|
),
|
2020-06-15 19:35:45 +00:00
|
|
|
action: () => ({ title: I18n.t('Unpin'), icon: 'pin', onPress: this.handleActionPress }),
|
2019-09-16 20:26:32 +00:00
|
|
|
handleActionPress: message => RocketChat.togglePinMessage(message._id, message.pinned)
|
2019-05-10 17:09:07 +00:00
|
|
|
}
|
|
|
|
}[name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
load = async() => {
|
|
|
|
const {
|
|
|
|
messages, total, loading
|
|
|
|
} = this.state;
|
|
|
|
if (messages.length === total || loading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await this.content.fetchFunc();
|
|
|
|
if (result.success) {
|
|
|
|
this.setState({
|
|
|
|
messages: [...messages, ...result.messages],
|
|
|
|
total: result.total,
|
|
|
|
loading: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.setState({ loading: false });
|
|
|
|
console.warn('MessagesView -> catch -> error', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
getCustomEmoji = (name) => {
|
|
|
|
const { customEmojis } = this.props;
|
|
|
|
const emoji = customEmojis[name];
|
|
|
|
if (emoji) {
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-12-18 21:13:11 +00:00
|
|
|
showAttachment = (attachment) => {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.navigate('AttachmentView', { attachment });
|
2019-05-20 20:43:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:09:07 +00:00
|
|
|
onLongPress = (message) => {
|
2020-06-15 19:35:45 +00:00
|
|
|
this.setState({ message }, this.showActionSheet);
|
2019-05-10 17:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
showActionSheet = () => {
|
2020-06-15 19:35:45 +00:00
|
|
|
const { message } = this.state;
|
|
|
|
const { showActionSheet } = this.props;
|
|
|
|
showActionSheet({ options: [this.content.action(message)], hasCancel: true });
|
2019-05-10 17:09:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
handleActionPress = async() => {
|
|
|
|
const { message } = this.state;
|
2019-05-10 17:09:07 +00:00
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
try {
|
|
|
|
const result = await this.content.handleActionPress(message);
|
|
|
|
if (result.success) {
|
|
|
|
this.setState(prevState => ({
|
|
|
|
messages: prevState.messages.filter(item => item._id !== message._id),
|
|
|
|
total: prevState.total - 1
|
|
|
|
}));
|
2019-05-10 17:09:07 +00:00
|
|
|
}
|
2020-06-15 19:35:45 +00:00
|
|
|
} catch {
|
|
|
|
// Do nothing
|
2019-05-10 17:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 16:56:39 +00:00
|
|
|
setFileLoading = (fileLoading) => {
|
|
|
|
this.setState({ fileLoading });
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderEmpty = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.listEmptyContainer,
|
|
|
|
{ backgroundColor: themes[theme].backgroundColor }
|
|
|
|
]}
|
|
|
|
testID={this.content.testID}
|
|
|
|
>
|
|
|
|
<Text style={[styles.noDataFound, { color: themes[theme].titleText }]}>{this.content.noDataMsg}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2019-05-10 17:09:07 +00:00
|
|
|
|
|
|
|
renderItem = ({ item }) => this.content.renderItem(item)
|
|
|
|
|
|
|
|
render() {
|
2019-12-18 21:13:11 +00:00
|
|
|
const { messages, loading } = this.state;
|
|
|
|
const { theme } = this.props;
|
2019-05-10 17:09:07 +00:00
|
|
|
|
|
|
|
if (!loading && messages.length === 0) {
|
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-12-04 16:39:53 +00:00
|
|
|
<SafeAreaView
|
2020-06-15 14:00:46 +00:00
|
|
|
style={{ backgroundColor: themes[theme].backgroundColor }}
|
2019-12-04 16:39:53 +00:00
|
|
|
testID={this.content.testID}
|
|
|
|
>
|
2020-10-30 13:59:44 +00:00
|
|
|
<StatusBar />
|
2019-05-10 17:09:07 +00:00
|
|
|
<FlatList
|
|
|
|
data={messages}
|
|
|
|
renderItem={this.renderItem}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
2019-05-10 17:09:07 +00:00
|
|
|
keyExtractor={item => item._id}
|
|
|
|
onEndReached={this.load}
|
2019-12-04 16:39:53 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null}
|
2019-05-10 17:09:07 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
|
|
|
user: getUserSelector(state),
|
2020-08-26 14:06:14 +00:00
|
|
|
customEmojis: state.customEmojis,
|
2021-06-15 20:13:42 +00:00
|
|
|
useRealName: state.settings.UI_Use_Real_Name,
|
|
|
|
isMasterDetail: state.app.isMasterDetail
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(withActionSheet(MessagesView)));
|