2019-06-10 16:22:35 +00:00
|
|
|
import React from 'react';
|
2022-05-02 11:53:47 +00:00
|
|
|
import { FlatList, ListRenderItem, Text, View } from 'react-native';
|
2019-06-10 16:22:35 +00:00
|
|
|
import { connect } from 'react-redux';
|
2022-01-17 16:10:39 +00:00
|
|
|
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
|
2022-05-02 11:53:47 +00:00
|
|
|
import { CompositeNavigationProp } from '@react-navigation/native';
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2021-12-03 19:27:57 +00:00
|
|
|
import { ChatsStackParamList } from '../../stacks/types';
|
2022-05-02 11:53:47 +00:00
|
|
|
import { MasterDetailInsideStackParamList } from '../../stacks/MasterDetailStack/types';
|
2021-09-13 20:41:05 +00:00
|
|
|
import * as List from '../../containers/List';
|
2022-08-17 13:32:21 +00:00
|
|
|
import Touch from '../../containers/Touch';
|
2022-04-07 20:25:25 +00:00
|
|
|
import DirectoryItem from '../../containers/DirectoryItem';
|
2019-06-10 16:22:35 +00:00
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import SearchBox from '../../containers/SearchBox';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon, TIconsName } from '../../containers/CustomIcon';
|
2019-06-10 16:22:35 +00:00
|
|
|
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';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { debounce } from '../../lib/methods/helpers';
|
|
|
|
import log, { events, logEvent } from '../../lib/methods/helpers/log';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, withTheme } from '../../theme';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
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';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { goRoom, TGoRoomItem } from '../../lib/methods/helpers/goRoom';
|
2022-05-02 11:53:47 +00:00
|
|
|
import { IApplicationState, IServerRoom, IUser, SubscriptionType } from '../../definitions';
|
2021-09-13 20:41:05 +00:00
|
|
|
import styles from './styles';
|
|
|
|
import Options from './Options';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../../lib/services';
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2021-09-15 20:37:21 +00:00
|
|
|
interface IDirectoryViewProps {
|
2022-05-02 11:53:47 +00:00
|
|
|
navigation: CompositeNavigationProp<
|
|
|
|
StackNavigationProp<ChatsStackParamList, 'DirectoryView'>,
|
|
|
|
StackNavigationProp<MasterDetailInsideStackParamList>
|
|
|
|
>;
|
2021-09-15 20:37:21 +00:00
|
|
|
baseUrl: string;
|
|
|
|
isFederationEnabled: boolean;
|
2022-05-02 11:53:47 +00:00
|
|
|
user: IUser;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-09-15 20:37:21 +00:00
|
|
|
directoryDefaultView: string;
|
|
|
|
isMasterDetail: boolean;
|
|
|
|
}
|
|
|
|
|
2022-05-02 11:53:47 +00:00
|
|
|
interface IDirectoryViewState {
|
|
|
|
data: IServerRoom[];
|
|
|
|
loading: boolean;
|
|
|
|
text: string;
|
|
|
|
total: number;
|
|
|
|
showOptionsDropdown: boolean;
|
|
|
|
globalUsers: boolean;
|
|
|
|
type: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DirectoryView extends React.Component<IDirectoryViewProps, IDirectoryViewState> {
|
2022-01-17 16:10:39 +00:00
|
|
|
static navigationOptions = ({ navigation, isMasterDetail }: IDirectoryViewProps) => {
|
|
|
|
const options: StackNavigationOptions = {
|
2019-11-25 20:01:17 +00:00
|
|
|
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;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2021-09-15 20:37:21 +00:00
|
|
|
constructor(props: IDirectoryViewProps) {
|
2019-06-10 16:22:35 +00:00
|
|
|
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({});
|
|
|
|
}
|
|
|
|
|
2021-09-15 20:37:21 +00:00
|
|
|
onSearchChangeText = (text: string) => {
|
2020-11-03 12:40:33 +00:00
|
|
|
this.setState({ text }, this.search);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
load = debounce(async ({ newSearch = false }) => {
|
2019-06-10 16:22:35 +00:00
|
|
|
if (newSearch) {
|
|
|
|
this.setState({ data: [], total: -1, loading: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
2021-09-13 20:41:05 +00:00
|
|
|
loading,
|
|
|
|
text,
|
|
|
|
total,
|
|
|
|
data: { length }
|
2019-06-10 16:22:35 +00:00
|
|
|
} = 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' };
|
2022-04-28 20:37:25 +00:00
|
|
|
const directories = await Services.getDirectory({
|
2019-06-10 16:22:35 +00:00
|
|
|
query,
|
|
|
|
offset: data.length,
|
|
|
|
count: 50,
|
2021-09-13 20:41:05 +00:00
|
|
|
sort: type === 'users' ? { username: 1 } : { usersCount: -1 }
|
2019-06-10 16:22:35 +00:00
|
|
|
});
|
|
|
|
if (directories.success) {
|
|
|
|
this.setState({
|
2022-05-02 11:53:47 +00:00
|
|
|
data: [...data, ...(directories.result as IServerRoom[])],
|
2019-06-10 16:22:35 +00:00
|
|
|
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 });
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
}, 200);
|
2019-06-10 16:22:35 +00:00
|
|
|
|
|
|
|
search = () => {
|
|
|
|
this.load({ newSearch: true });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2021-09-15 20:37:21 +00:00
|
|
|
changeType = (type: string) => {
|
2019-06-10 16:22:35 +00:00
|
|
|
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);
|
2021-06-04 18:07:26 +00:00
|
|
|
} else if (type === 'teams') {
|
|
|
|
logEvent(events.DIRECTORY_SEARCH_TEAMS);
|
2020-07-30 13:26:17 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
|
|
|
toggleWorkspace = () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
this.setState(
|
2022-05-02 11:53:47 +00:00
|
|
|
({ globalUsers }) => ({ globalUsers: !globalUsers, data: [] }),
|
2021-09-13 20:41:05 +00:00
|
|
|
() => this.search()
|
|
|
|
);
|
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
|
|
|
toggleDropdown = () => {
|
2022-05-02 11:53:47 +00:00
|
|
|
this.setState(({ showOptionsDropdown }) => ({ showOptionsDropdown: !showOptionsDropdown }));
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2022-05-02 11:53:47 +00:00
|
|
|
goRoom = (item: TGoRoomItem) => {
|
2022-11-25 13:21:56 +00:00
|
|
|
const { isMasterDetail } = this.props;
|
|
|
|
goRoom({ item, isMasterDetail, popToRoot: true });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2022-05-02 11:53:47 +00:00
|
|
|
onPressItem = async (item: IServerRoom) => {
|
2019-06-10 16:22:35 +00:00
|
|
|
const { type } = this.state;
|
|
|
|
if (type === 'users') {
|
2022-05-02 11:53:47 +00:00
|
|
|
const result = await Services.createDirectMessage(item.username as string);
|
2019-06-10 16:22:35 +00:00
|
|
|
if (result.success) {
|
2022-05-02 11:53:47 +00:00
|
|
|
this.goRoom({ rid: result.room._id, name: item.username, t: SubscriptionType.DIRECT });
|
2019-06-10 16:22:35 +00:00
|
|
|
}
|
2021-06-04 18:07:26 +00:00
|
|
|
} else if (['p', 'c'].includes(item.t) && !item.teamMain) {
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = await Services.getRoomInfo(item._id);
|
2022-03-16 00:04:10 +00:00
|
|
|
if (result.success) {
|
|
|
|
this.goRoom({
|
|
|
|
rid: item._id,
|
|
|
|
name: item.name,
|
|
|
|
joinCodeRequired: result.room.joinCodeRequired,
|
2022-05-02 11:53:47 +00:00
|
|
|
t: item.t as SubscriptionType,
|
2022-03-16 00:04:10 +00:00
|
|
|
search: true
|
|
|
|
});
|
|
|
|
}
|
2022-03-24 21:52:35 +00:00
|
|
|
} else {
|
|
|
|
this.goRoom({
|
|
|
|
rid: item._id,
|
|
|
|
name: item.name,
|
2022-05-02 11:53:47 +00:00
|
|
|
t: item.t as SubscriptionType,
|
2022-03-24 21:52:35 +00:00
|
|
|
search: true,
|
|
|
|
teamMain: item.teamMain,
|
|
|
|
teamId: item.teamId
|
|
|
|
});
|
2019-06-10 16:22:35 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
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;
|
2021-06-04 18:07:26 +00:00
|
|
|
let text = 'Users';
|
2022-05-02 19:21:15 +00:00
|
|
|
let icon: TIconsName = 'user';
|
2021-06-04 18:07:26 +00:00
|
|
|
|
|
|
|
if (type === 'channels') {
|
|
|
|
text = 'Channels';
|
|
|
|
icon = 'channel-public';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'teams') {
|
|
|
|
text = 'Teams';
|
|
|
|
icon = 'teams';
|
|
|
|
}
|
|
|
|
|
2019-06-10 16:22:35 +00:00
|
|
|
return (
|
2019-09-24 20:26:56 +00:00
|
|
|
<>
|
2021-09-13 20:41:05 +00:00
|
|
|
<SearchBox onChangeText={this.onSearchChangeText} onSubmitEditing={this.search} testID='directory-view-search' />
|
2022-08-17 13:32:21 +00:00
|
|
|
<Touch onPress={this.toggleDropdown} style={styles.dropdownItemButton} testID='directory-view-dropdown'>
|
2021-09-13 20:41:05 +00:00
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
sharedStyles.separatorVertical,
|
|
|
|
styles.toggleDropdownContainer,
|
|
|
|
{ borderColor: themes[theme].separatorColor }
|
2022-08-08 21:02:08 +00:00
|
|
|
]}
|
|
|
|
>
|
2022-05-02 19:21:15 +00:00
|
|
|
<CustomIcon name={icon} size={20} color={themes[theme].tintColor} style={styles.toggleDropdownIcon} />
|
2021-06-04 18:07:26 +00:00
|
|
|
<Text style={[styles.toggleDropdownText, { color: themes[theme].tintColor }]}>{I18n.t(text)}</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
<CustomIcon
|
|
|
|
name='chevron-down'
|
|
|
|
size={20}
|
2022-05-02 19:21:15 +00:00
|
|
|
color={themes[theme].auxiliaryTintColor}
|
|
|
|
style={styles.toggleDropdownArrow}
|
2021-09-13 20:41:05 +00:00
|
|
|
/>
|
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
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
2022-05-02 11:53:47 +00:00
|
|
|
renderItem: ListRenderItem<IServerRoom> = ({ item, index }) => {
|
2019-06-10 16:22:35 +00:00
|
|
|
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 = {
|
2022-05-02 11:53:47 +00:00
|
|
|
title: item.name as string,
|
2019-06-10 16:22:35 +00:00
|
|
|
onPress: () => this.onPressItem(item),
|
|
|
|
baseUrl,
|
2023-02-23 13:03:48 +00:00
|
|
|
testID: `directory-view-item-${item.name}`,
|
2019-06-10 16:22:35 +00:00
|
|
|
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}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2021-06-04 18:07:26 +00:00
|
|
|
|
|
|
|
if (type === 'teams') {
|
|
|
|
return (
|
|
|
|
<DirectoryItem
|
|
|
|
avatar={item.name}
|
|
|
|
description={item.name}
|
|
|
|
rightLabel={I18n.t('N_channels', { n: item.roomsCount })}
|
|
|
|
type={item.t}
|
|
|
|
teamMain={item.teamMain}
|
|
|
|
{...commonProps}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2019-06-10 16:22:35 +00:00
|
|
|
return (
|
|
|
|
<DirectoryItem
|
|
|
|
avatar={item.name}
|
|
|
|
description={item.topic}
|
|
|
|
rightLabel={I18n.t('N_users', { n: item.usersCount })}
|
2021-06-04 18:07:26 +00:00
|
|
|
type={item.t}
|
2019-06-10 16:22:35 +00:00
|
|
|
{...commonProps}
|
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
|
|
|
|
render = () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
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 (
|
2021-09-13 20:41:05 +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}
|
2021-02-26 16:27:04 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2019-06-10 16:22:35 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
2022-03-18 02:37:10 +00:00
|
|
|
ListFooterComponent={loading ? <ActivityIndicator /> : null}
|
2019-06-10 16:22:35 +00:00
|
|
|
onEndReached={() => this.load({})}
|
|
|
|
/>
|
2021-09-13 20:41:05 +00:00
|
|
|
{showOptionsDropdown ? (
|
|
|
|
<Options
|
|
|
|
theme={theme}
|
|
|
|
type={type}
|
|
|
|
globalUsers={globalUsers}
|
|
|
|
close={this.toggleDropdown}
|
|
|
|
changeType={this.changeType}
|
|
|
|
toggleWorkspace={this.toggleWorkspace}
|
|
|
|
isFederationEnabled={isFederationEnabled}
|
|
|
|
/>
|
|
|
|
) : null}
|
2019-06-10 16:22:35 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-10 16:22:35 +00:00
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2022-05-02 11:53:47 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
|
|
|
user: getUserSelector(state),
|
2022-05-02 11:53:47 +00:00
|
|
|
isFederationEnabled: state.settings.FEDERATION_Enabled as boolean,
|
|
|
|
directoryDefaultView: state.settings.Accounts_Directory_DefaultView as string,
|
2020-06-15 14:00:46 +00:00
|
|
|
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));
|