2018-04-24 19:34:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-12-12 15:15:10 +00:00
|
|
|
import { View, FlatList, Text } from 'react-native';
|
2020-09-11 14:31:38 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-02-26 16:01:45 +00:00
|
|
|
import { dequal } from 'dequal';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
import RCTextInput from '../../containers/TextInput';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
2018-04-24 19:34:03 +00:00
|
|
|
import styles from './styles';
|
2019-08-27 12:25:38 +00:00
|
|
|
import Markdown from '../../containers/markdown';
|
2018-04-24 19:34:03 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2020-05-08 17:07:58 +00:00
|
|
|
import Message from '../../containers/message';
|
2018-04-24 19:34:03 +00:00
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-05-28 16:18:46 +00:00
|
|
|
import log from '../../utils/log';
|
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 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
2020-09-11 14:31:38 +00:00
|
|
|
import database from '../../lib/database';
|
2020-09-15 13:01:43 +00:00
|
|
|
import { sanitizeLikeString } from '../../lib/database/utils';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class SearchMessagesView extends React.Component {
|
2020-06-15 14:00:46 +00:00
|
|
|
static navigationOptions = ({ navigation, route }) => {
|
|
|
|
const options = {
|
|
|
|
title: I18n.t('Search')
|
|
|
|
};
|
|
|
|
const showCloseModal = route.params?.showCloseModal;
|
|
|
|
if (showCloseModal) {
|
2020-10-30 16:15:58 +00:00
|
|
|
options.headerLeft = () => <HeaderButton.CloseModal navigation={navigation} />;
|
2020-06-15 14:00:46 +00:00
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
static propTypes = {
|
2019-03-12 16:23:06 +00:00
|
|
|
navigation: PropTypes.object,
|
2020-06-15 14:00:46 +00:00
|
|
|
route: PropTypes.object,
|
2018-07-10 13:40:32 +00:00
|
|
|
user: PropTypes.object,
|
2019-09-16 20:26:32 +00:00
|
|
|
baseUrl: PropTypes.string,
|
2019-12-04 16:39:53 +00:00
|
|
|
customEmojis: PropTypes.object,
|
2021-02-11 21:43:57 +00:00
|
|
|
theme: PropTypes.string,
|
|
|
|
useRealName: PropTypes.bool
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2018-04-24 19:34:03 +00:00
|
|
|
this.state = {
|
2018-12-12 15:15:10 +00:00
|
|
|
loading: false,
|
2018-04-24 19:34:03 +00:00
|
|
|
messages: [],
|
2018-12-12 15:15:10 +00:00
|
|
|
searchText: ''
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
this.rid = props.route.params?.rid;
|
2020-09-11 14:31:38 +00:00
|
|
|
this.encrypted = props.route.params?.encrypted;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { loading, searchText, messages } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
if (nextState.loading !== loading) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (nextState.searchText !== searchText) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-26 16:01:45 +00:00
|
|
|
if (!dequal(nextState.messages, messages)) {
|
2018-12-21 10:55:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
componentWillUnmount() {
|
2020-09-11 14:31:38 +00:00
|
|
|
this.search?.stop?.();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle encrypted rooms search messages
|
|
|
|
searchMessages = async(searchText) => {
|
|
|
|
// If it's a encrypted, room we'll search only on the local stored messages
|
|
|
|
if (this.encrypted) {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const messagesCollection = db.get('messages');
|
2020-09-15 13:01:43 +00:00
|
|
|
const likeString = sanitizeLikeString(searchText);
|
2020-09-11 14:31:38 +00:00
|
|
|
return messagesCollection
|
|
|
|
.query(
|
|
|
|
// Messages of this room
|
|
|
|
Q.where('rid', this.rid),
|
|
|
|
// Message content is like the search text
|
2020-09-15 13:01:43 +00:00
|
|
|
Q.where('msg', Q.like(`%${ likeString }%`))
|
2020-09-11 14:31:38 +00:00
|
|
|
)
|
|
|
|
.fetch();
|
|
|
|
}
|
|
|
|
// If it's not a encrypted room, search messages on the server
|
|
|
|
const result = await RocketChat.searchMessages(this.rid, searchText);
|
|
|
|
if (result.success) {
|
|
|
|
return result.messages;
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 15:15:10 +00:00
|
|
|
search = debounce(async(searchText) => {
|
|
|
|
this.setState({ searchText, loading: true, messages: [] });
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
2020-09-11 14:31:38 +00:00
|
|
|
const messages = await this.searchMessages(searchText);
|
|
|
|
this.setState({
|
|
|
|
messages: messages || [],
|
|
|
|
loading: false
|
|
|
|
});
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
2018-12-12 15:15:10 +00:00
|
|
|
this.setState({ loading: false });
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2018-12-12 15:15:10 +00:00
|
|
|
}, 1000)
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
getCustomEmoji = (name) => {
|
|
|
|
const { customEmojis } = this.props;
|
|
|
|
const emoji = customEmojis[name];
|
|
|
|
if (emoji) {
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderEmpty = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={[styles.listEmptyContainer, { backgroundColor: themes[theme].backgroundColor }]}>
|
|
|
|
<Text style={[styles.noDataFound, { color: themes[theme].titleText }]}>{I18n.t('No_results_found')}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderItem = ({ item }) => {
|
2021-02-11 21:43:57 +00:00
|
|
|
const {
|
|
|
|
user, baseUrl, theme, useRealName
|
|
|
|
} = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<Message
|
2020-05-08 17:07:58 +00:00
|
|
|
item={item}
|
2018-12-12 15:15:10 +00:00
|
|
|
baseUrl={baseUrl}
|
2018-09-25 19:28:42 +00:00
|
|
|
user={user}
|
2020-10-30 17:35:07 +00:00
|
|
|
timeFormat='LLL'
|
2019-05-20 20:43:50 +00:00
|
|
|
isHeader
|
2019-12-18 21:13:11 +00:00
|
|
|
showAttachment={() => {}}
|
2019-09-16 20:26:32 +00:00
|
|
|
getCustomEmoji={this.getCustomEmoji}
|
2020-02-17 16:06:46 +00:00
|
|
|
navToRoomInfo={this.navToRoomInfo}
|
2021-02-11 21:43:57 +00:00
|
|
|
useRealName={useRealName}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-12-12 15:15:10 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderList = () => {
|
|
|
|
const { messages, loading, searchText } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-12-12 15:15:10 +00:00
|
|
|
|
|
|
|
if (!loading && messages.length === 0 && searchText.length) {
|
|
|
|
return this.renderEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FlatList
|
|
|
|
data={messages}
|
|
|
|
renderItem={this.renderItem}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
|
2018-12-12 15:15:10 +00:00
|
|
|
keyExtractor={item => item._id}
|
|
|
|
onEndReached={this.load}
|
2019-12-04 16:39:53 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null}
|
2018-12-12 15:15:10 +00:00
|
|
|
{...scrollPersistTaps}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
render() {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2018-04-24 19:34:03 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView style={{ backgroundColor: themes[theme].backgroundColor }} testID='search-messages-view'>
|
|
|
|
<StatusBar />
|
2018-04-24 19:34:03 +00:00
|
|
|
<View style={styles.searchContainer}>
|
|
|
|
<RCTextInput
|
2019-08-07 19:20:16 +00:00
|
|
|
autoFocus
|
2018-06-01 17:38:13 +00:00
|
|
|
label={I18n.t('Search')}
|
2018-12-12 15:15:10 +00:00
|
|
|
onChangeText={this.search}
|
2018-06-01 17:38:13 +00:00
|
|
|
placeholder={I18n.t('Search_Messages')}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='search-message-view-input'
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-04-24 19:34:03 +00:00
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Markdown msg={I18n.t('You_can_search_using_RegExp_eg')} username='' baseUrl='' theme={theme} />
|
|
|
|
<View style={[styles.divider, { backgroundColor: themes[theme].separatorColor }]} />
|
2018-04-24 19:34:03 +00:00
|
|
|
</View>
|
2018-12-12 15:15:10 +00:00
|
|
|
{this.renderList()}
|
2018-08-01 19:35:06 +00:00
|
|
|
</SafeAreaView>
|
2018-04-24 19:34:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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),
|
2021-02-11 21:43:57 +00:00
|
|
|
useRealName: state.settings.UI_Use_Real_Name,
|
2019-09-16 20:26:32 +00:00
|
|
|
customEmojis: state.customEmojis
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(SearchMessagesView));
|