Chore: Evaluate DirectoryView - TypeScript (#4130)

This commit is contained in:
Reinaldo Neto 2022-05-02 08:53:47 -03:00 committed by GitHub
parent 744ee27e18
commit a27d63c22e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 26 deletions

View File

@ -17,8 +17,8 @@ interface IDirectoryItemLabel {
interface IDirectoryItem { interface IDirectoryItem {
title: string; title: string;
description: string; description?: string;
avatar: string; avatar?: string;
type: string; type: string;
onPress(): void; onPress(): void;
testID: string; testID: string;

View File

@ -142,6 +142,11 @@ export interface IServerRoom extends IRocketChatRecord {
encrypted?: boolean; encrypted?: boolean;
topic?: any; topic?: any;
username?: string;
nickname?: string;
federation?: any;
roomsCount?: number;
u: Pick<IUser, '_id' | 'username' | 'name'>; u: Pick<IUser, '_id' | 'username' | 'name'>;
uids: Array<string>; uids: Array<string>;

View File

@ -1,9 +1,11 @@
import React from 'react'; import React from 'react';
import { FlatList, Text, View } from 'react-native'; import { FlatList, ListRenderItem, Text, View } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack'; import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
import { CompositeNavigationProp } from '@react-navigation/native';
import { ChatsStackParamList } from '../../stacks/types'; import { ChatsStackParamList } from '../../stacks/types';
import { MasterDetailInsideStackParamList } from '../../stacks/MasterDetailStack/types';
import * as List from '../../containers/List'; import * as List from '../../containers/List';
import Touch from '../../utils/touch'; import Touch from '../../utils/touch';
import DirectoryItem from '../../containers/DirectoryItem'; import DirectoryItem from '../../containers/DirectoryItem';
@ -20,25 +22,36 @@ import { TSupportedThemes, withTheme } from '../../theme';
import { themes } from '../../lib/constants'; import { themes } from '../../lib/constants';
import { getUserSelector } from '../../selectors/login'; import { getUserSelector } from '../../selectors/login';
import SafeAreaView from '../../containers/SafeAreaView'; import SafeAreaView from '../../containers/SafeAreaView';
import { goRoom } from '../../utils/goRoom'; import { goRoom, TGoRoomItem } from '../../utils/goRoom';
import { IApplicationState, IServerRoom, IUser, SubscriptionType } from '../../definitions';
import styles from './styles'; import styles from './styles';
import Options from './Options'; import Options from './Options';
import { Services } from '../../lib/services'; import { Services } from '../../lib/services';
interface IDirectoryViewProps { interface IDirectoryViewProps {
navigation: StackNavigationProp<ChatsStackParamList, 'DirectoryView'>; navigation: CompositeNavigationProp<
StackNavigationProp<ChatsStackParamList, 'DirectoryView'>,
StackNavigationProp<MasterDetailInsideStackParamList>
>;
baseUrl: string; baseUrl: string;
isFederationEnabled: boolean; isFederationEnabled: boolean;
user: { user: IUser;
id: string;
token: string;
};
theme: TSupportedThemes; theme: TSupportedThemes;
directoryDefaultView: string; directoryDefaultView: string;
isMasterDetail: boolean; isMasterDetail: boolean;
} }
class DirectoryView extends React.Component<IDirectoryViewProps, any> { interface IDirectoryViewState {
data: IServerRoom[];
loading: boolean;
text: string;
total: number;
showOptionsDropdown: boolean;
globalUsers: boolean;
type: string;
}
class DirectoryView extends React.Component<IDirectoryViewProps, IDirectoryViewState> {
static navigationOptions = ({ navigation, isMasterDetail }: IDirectoryViewProps) => { static navigationOptions = ({ navigation, isMasterDetail }: IDirectoryViewProps) => {
const options: StackNavigationOptions = { const options: StackNavigationOptions = {
title: I18n.t('Directory') title: I18n.t('Directory')
@ -70,7 +83,6 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
this.setState({ text }, this.search); this.setState({ text }, this.search);
}; };
// eslint-disable-next-line react/sort-comp
load = debounce(async ({ newSearch = false }) => { load = debounce(async ({ newSearch = false }) => {
if (newSearch) { if (newSearch) {
this.setState({ data: [], total: -1, loading: false }); this.setState({ data: [], total: -1, loading: false });
@ -99,7 +111,7 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
}); });
if (directories.success) { if (directories.success) {
this.setState({ this.setState({
data: [...data, ...directories.result], data: [...data, ...(directories.result as IServerRoom[])],
loading: false, loading: false,
total: directories.total total: directories.total
}); });
@ -130,17 +142,17 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
toggleWorkspace = () => { toggleWorkspace = () => {
this.setState( this.setState(
({ globalUsers }: any) => ({ globalUsers: !globalUsers, data: [] }), ({ globalUsers }) => ({ globalUsers: !globalUsers, data: [] }),
() => this.search() () => this.search()
); );
}; };
toggleDropdown = () => { toggleDropdown = () => {
this.setState(({ showOptionsDropdown }: any) => ({ showOptionsDropdown: !showOptionsDropdown })); this.setState(({ showOptionsDropdown }) => ({ showOptionsDropdown: !showOptionsDropdown }));
}; };
goRoom = (item: any) => { goRoom = (item: TGoRoomItem) => {
const { navigation, isMasterDetail }: any = this.props; const { navigation, isMasterDetail } = this.props;
if (isMasterDetail) { if (isMasterDetail) {
navigation.navigate('DrawerNavigator'); navigation.navigate('DrawerNavigator');
} else { } else {
@ -149,12 +161,12 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
goRoom({ item, isMasterDetail }); goRoom({ item, isMasterDetail });
}; };
onPressItem = async (item: any) => { onPressItem = async (item: IServerRoom) => {
const { type } = this.state; const { type } = this.state;
if (type === 'users') { if (type === 'users') {
const result = await Services.createDirectMessage(item.username); const result = await Services.createDirectMessage(item.username as string);
if (result.success) { if (result.success) {
this.goRoom({ rid: result.room._id, name: item.username, t: 'd' }); this.goRoom({ rid: result.room._id, name: item.username, t: SubscriptionType.DIRECT });
} }
} else if (['p', 'c'].includes(item.t) && !item.teamMain) { } else if (['p', 'c'].includes(item.t) && !item.teamMain) {
const result = await Services.getRoomInfo(item._id); const result = await Services.getRoomInfo(item._id);
@ -163,7 +175,7 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
rid: item._id, rid: item._id,
name: item.name, name: item.name,
joinCodeRequired: result.room.joinCodeRequired, joinCodeRequired: result.room.joinCodeRequired,
t: item.t, t: item.t as SubscriptionType,
search: true search: true
}); });
} }
@ -171,7 +183,7 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
this.goRoom({ this.goRoom({
rid: item._id, rid: item._id,
name: item.name, name: item.name,
t: item.t, t: item.t as SubscriptionType,
search: true, search: true,
teamMain: item.teamMain, teamMain: item.teamMain,
teamId: item.teamId teamId: item.teamId
@ -218,7 +230,7 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
); );
}; };
renderItem = ({ item, index }: any) => { renderItem: ListRenderItem<IServerRoom> = ({ item, index }) => {
const { data, type } = this.state; const { data, type } = this.state;
const { baseUrl, user, theme } = this.props; const { baseUrl, user, theme } = this.props;
@ -231,7 +243,7 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
} }
const commonProps = { const commonProps = {
title: item.name, title: item.name as string,
onPress: () => this.onPressItem(item), onPress: () => this.onPressItem(item),
baseUrl, baseUrl,
testID: `directory-view-item-${item.name}`.toLowerCase(), testID: `directory-view-item-${item.name}`.toLowerCase(),
@ -311,11 +323,11 @@ class DirectoryView extends React.Component<IDirectoryViewProps, any> {
}; };
} }
const mapStateToProps = (state: any) => ({ const mapStateToProps = (state: IApplicationState) => ({
baseUrl: state.server.server, baseUrl: state.server.server,
user: getUserSelector(state), user: getUserSelector(state),
isFederationEnabled: state.settings.FEDERATION_Enabled, isFederationEnabled: state.settings.FEDERATION_Enabled as boolean,
directoryDefaultView: state.settings.Accounts_Directory_DefaultView, directoryDefaultView: state.settings.Accounts_Directory_DefaultView as string,
isMasterDetail: state.app.isMasterDetail isMasterDetail: state.app.isMasterDetail
}); });