2019-06-10 16:22:35 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import {
|
|
|
|
View, FlatList, Text
|
|
|
|
} from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
import Touch from '../../utils/touch';
|
2019-06-10 16:22:35 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2019-07-18 17:44:02 +00:00
|
|
|
import DirectoryItem from '../../presentation/DirectoryItem';
|
2019-06-10 16:22:35 +00:00
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import SearchBox from '../../containers/SearchBox';
|
|
|
|
import { CustomIcon } from '../../lib/Icons';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2019-12-04 16:39:53 +00:00
|
|
|
import ActivityIndicator from '../../containers/ActivityIndicator';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
2019-06-10 16:22:35 +00:00
|
|
|
import debounce from '../../utils/debounce';
|
2020-07-30 13:26:17 +00:00
|
|
|
import log, { logEvent, events } from '../../utils/log';
|
2019-06-10 16:22:35 +00:00
|
|
|
import Options from './Options';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { themes } from '../../constants/colors';
|
2019-06-10 16:22:35 +00:00
|
|
|
import styles from './styles';
|
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';
|
|
|
|
import { goRoom } from '../../utils/goRoom';
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class DirectoryView extends React.Component {
|
2020-06-15 14:00:46 +00:00
|
|
|
static navigationOptions = ({ navigation, isMasterDetail }) => {
|
2019-11-25 20:01:17 +00:00
|
|
|
const options = {
|
|
|
|
title: I18n.t('Directory')
|
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
if (isMasterDetail) {
|
2020-10-30 16:15:58 +00:00
|
|
|
options.headerLeft = () => <HeaderButton.CloseModal navigation={navigation} testID='directory-view-close' />;
|
2019-11-25 20:01:17 +00:00
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
2019-06-10 16:22:35 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
baseUrl: PropTypes.string,
|
|
|
|
isFederationEnabled: PropTypes.bool,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
token: PropTypes.string
|
2019-12-04 16:39:53 +00:00
|
|
|
}),
|
2020-03-30 14:20:55 +00:00
|
|
|
theme: PropTypes.string,
|
2020-06-15 14:00:46 +00:00
|
|
|
directoryDefaultView: PropTypes.string,
|
|
|
|
isMasterDetail: PropTypes.bool
|
2019-06-10 16:22:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
data: [],
|
|
|
|
loading: false,
|
|
|
|
text: '',
|
|
|
|
total: -1,
|
|
|
|
showOptionsDropdown: false,
|
|
|
|
globalUsers: true,
|
2020-03-30 14:20:55 +00:00
|
|
|
type: props.directoryDefaultView
|
2019-06-10 16:22:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.load({});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChangeText = (text) => {
|
2020-11-03 12:40:33 +00:00
|
|
|
this.setState({ text }, this.search);
|
2019-06-10 16:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line react/sort-comp
|
|
|
|
load = debounce(async({ newSearch = false }) => {
|
|
|
|
if (newSearch) {
|
|
|
|
this.setState({ data: [], total: -1, loading: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
loading, text, total, data: { length }
|
|
|
|
} = this.state;
|
|
|
|
if (loading || length === total) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { data, type, globalUsers } = this.state;
|
|
|
|
const query = { text, type, workspace: globalUsers ? 'all' : 'local' };
|
|
|
|
const directories = await RocketChat.getDirectory({
|
|
|
|
query,
|
|
|
|
offset: data.length,
|
|
|
|
count: 50,
|
|
|
|
sort: (type === 'users') ? { username: 1 } : { usersCount: -1 }
|
|
|
|
});
|
|
|
|
if (directories.success) {
|
|
|
|
this.setState({
|
|
|
|
data: [...data, ...directories.result],
|
|
|
|
loading: false,
|
|
|
|
total: directories.total
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({ loading: false });
|
|
|
|
}
|
2019-08-23 13:18:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2019-06-10 16:22:35 +00:00
|
|
|
this.setState({ loading: false });
|
|
|
|
}
|
|
|
|
}, 200)
|
|
|
|
|
|
|
|
search = () => {
|
|
|
|
this.load({ newSearch: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
changeType = (type) => {
|
|
|
|
this.setState({ type, data: [] }, () => this.search());
|
2020-07-30 13:26:17 +00:00
|
|
|
|
|
|
|
if (type === 'users') {
|
|
|
|
logEvent(events.DIRECTORY_SEARCH_USERS);
|
|
|
|
} else if (type === 'channels') {
|
|
|
|
logEvent(events.DIRECTORY_SEARCH_CHANNELS);
|
|
|
|
}
|
2019-06-10 16:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleWorkspace = () => {
|
|
|
|
this.setState(({ globalUsers }) => ({ globalUsers: !globalUsers, data: [] }), () => this.search());
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleDropdown = () => {
|
|
|
|
this.setState(({ showOptionsDropdown }) => ({ showOptionsDropdown: !showOptionsDropdown }));
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
goRoom = (item) => {
|
|
|
|
const { navigation, isMasterDetail } = this.props;
|
|
|
|
if (isMasterDetail) {
|
|
|
|
navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
navigation.navigate('RoomsListView');
|
|
|
|
}
|
|
|
|
goRoom({ item, isMasterDetail });
|
2019-06-10 16:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onPressItem = async(item) => {
|
|
|
|
const { type } = this.state;
|
|
|
|
if (type === 'users') {
|
|
|
|
const result = await RocketChat.createDirectMessage(item.username);
|
|
|
|
if (result.success) {
|
|
|
|
this.goRoom({ rid: result.room._id, name: item.username, t: 'd' });
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-13 12:51:16 +00:00
|
|
|
this.goRoom({
|
|
|
|
rid: item._id, name: item.name, t: 'c', search: true
|
|
|
|
});
|
2019-06-10 16:22:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderHeader = () => {
|
|
|
|
const { type } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2019-06-10 16:22:35 +00:00
|
|
|
return (
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2019-06-10 16:22:35 +00:00
|
|
|
<SearchBox
|
|
|
|
onChangeText={this.onSearchChangeText}
|
|
|
|
onSubmitEditing={this.search}
|
|
|
|
testID='federation-view-search'
|
|
|
|
/>
|
2019-12-04 16:39:53 +00:00
|
|
|
<Touch
|
|
|
|
onPress={this.toggleDropdown}
|
|
|
|
style={styles.dropdownItemButton}
|
|
|
|
testID='federation-view-create-channel'
|
|
|
|
theme={theme}
|
|
|
|
>
|
|
|
|
<View style={[sharedStyles.separatorVertical, styles.toggleDropdownContainer, { borderColor: themes[theme].separatorColor }]}>
|
2020-07-27 19:53:33 +00:00
|
|
|
<CustomIcon style={[styles.toggleDropdownIcon, { color: themes[theme].tintColor }]} size={20} name={type === 'users' ? 'user' : 'channel-public'} />
|
2019-12-04 16:39:53 +00:00
|
|
|
<Text style={[styles.toggleDropdownText, { color: themes[theme].tintColor }]}>{type === 'users' ? I18n.t('Users') : I18n.t('Channels')}</Text>
|
2020-06-05 13:28:58 +00:00
|
|
|
<CustomIcon name='chevron-down' size={20} style={[styles.toggleDropdownArrow, { color: themes[theme].auxiliaryTintColor }]} />
|
2019-06-10 16:22:35 +00:00
|
|
|
</View>
|
|
|
|
</Touch>
|
2019-09-24 20:26:56 +00:00
|
|
|
</>
|
2019-06-10 16:22:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderSeparator = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return <View style={[sharedStyles.separator, styles.separator, { backgroundColor: themes[theme].separatorColor }]} />;
|
|
|
|
}
|
2019-06-10 16:22:35 +00:00
|
|
|
|
|
|
|
renderItem = ({ item, index }) => {
|
|
|
|
const { data, type } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { baseUrl, user, theme } = this.props;
|
2019-06-10 16:22:35 +00:00
|
|
|
|
|
|
|
let style;
|
|
|
|
if (index === data.length - 1) {
|
2020-04-01 12:28:54 +00:00
|
|
|
style = {
|
|
|
|
...sharedStyles.separatorBottom,
|
|
|
|
borderColor: themes[theme].separatorColor
|
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const commonProps = {
|
|
|
|
title: item.name,
|
|
|
|
onPress: () => this.onPressItem(item),
|
|
|
|
baseUrl,
|
|
|
|
testID: `federation-view-item-${ item.name }`,
|
|
|
|
style,
|
2019-12-04 16:39:53 +00:00
|
|
|
user,
|
2020-10-30 13:51:04 +00:00
|
|
|
theme,
|
|
|
|
rid: item._id
|
2019-06-10 16:22:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (type === 'users') {
|
|
|
|
return (
|
|
|
|
<DirectoryItem
|
|
|
|
avatar={item.username}
|
|
|
|
description={item.username}
|
|
|
|
rightLabel={item.federation && item.federation.peer}
|
|
|
|
type='d'
|
|
|
|
{...commonProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<DirectoryItem
|
|
|
|
avatar={item.name}
|
|
|
|
description={item.topic}
|
|
|
|
rightLabel={I18n.t('N_users', { n: item.usersCount })}
|
|
|
|
type='c'
|
|
|
|
{...commonProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render = () => {
|
|
|
|
const {
|
|
|
|
data, loading, showOptionsDropdown, type, globalUsers
|
|
|
|
} = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { isFederationEnabled, theme } = this.props;
|
2019-06-10 16:22:35 +00:00
|
|
|
return (
|
2020-06-15 14:00:46 +00:00
|
|
|
<SafeAreaView
|
|
|
|
style={{ backgroundColor: themes[theme].backgroundColor }}
|
|
|
|
testID='directory-view'
|
|
|
|
>
|
2020-10-30 13:59:44 +00:00
|
|
|
<StatusBar />
|
2019-06-10 16:22:35 +00:00
|
|
|
<FlatList
|
|
|
|
data={data}
|
|
|
|
style={styles.list}
|
|
|
|
contentContainerStyle={styles.listContainer}
|
|
|
|
extraData={this.state}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ItemSeparatorComponent={this.renderSeparator}
|
|
|
|
keyboardShouldPersistTaps='always'
|
2019-12-04 16:39:53 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator theme={theme} /> : null}
|
2019-06-10 16:22:35 +00:00
|
|
|
onEndReached={() => this.load({})}
|
|
|
|
/>
|
|
|
|
{showOptionsDropdown
|
|
|
|
? (
|
|
|
|
<Options
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2019-06-10 16:22:35 +00:00
|
|
|
type={type}
|
|
|
|
globalUsers={globalUsers}
|
|
|
|
close={this.toggleDropdown}
|
|
|
|
changeType={this.changeType}
|
|
|
|
toggleWorkspace={this.toggleWorkspace}
|
|
|
|
isFederationEnabled={isFederationEnabled}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
: null}
|
|
|
|
</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-03-30 14:20:55 +00:00
|
|
|
isFederationEnabled: state.settings.FEDERATION_Enabled,
|
2020-06-15 14:00:46 +00:00
|
|
|
directoryDefaultView: state.settings.Accounts_Directory_DefaultView,
|
|
|
|
isMasterDetail: state.app.isMasterDetail
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(DirectoryView));
|