import React, { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import {
CometChatUsers,
CometChatGroups,
useTheme,
useCometChatTranslation,
Icon,
} from '@cometchat/chat-uikit-react-native';
import { CometChat } from '@cometchat/chat-sdk-react-native';
const NewChatScreen = () => {
const navigation = useNavigation();
const theme = useTheme();
const { t } = useCometChatTranslation();
const [selectedTab, setSelectedTab] = useState<'user' | 'group'>('user');
const handleUserPress = (user: CometChat.User) => {
navigation.navigate('Messages', { user });
};
const handleGroupPress = (group: CometChat.Group) => {
if (group.getHasJoined()) {
navigation.navigate('Messages', { group });
} else {
// Handle join flow for non-joined groups
handleJoinGroup(group);
}
};
const handleJoinGroup = async (group: CometChat.Group) => {
if (group.getType() === CometChat.GROUP_TYPE.PUBLIC) {
try {
const joinedGroup = await CometChat.joinGroup(
group.getGuid(),
group.getType() as CometChat.GroupType,
''
);
navigation.navigate('Messages', { group: joinedGroup });
} catch (error) {
console.error('Error joining group:', error);
}
} else if (group.getType() === CometChat.GROUP_TYPE.PASSWORD) {
// Show password input modal
navigation.navigate('JoinGroup', { group });
}
};
return (
<View style={[styles.container, { backgroundColor: theme.color.background1 }]}>
{/* Header */}
<View style={[styles.header, { borderBottomColor: theme.color.borderLight }]}>
<TouchableOpacity onPress={() => navigation.goBack()}>
{/* Back icon */}
</TouchableOpacity>
<Text style={[theme.typography.heading1.bold, { color: theme.color.textPrimary }]}>
{t('NEW_CHAT')}
</Text>
</View>
{/* Tabs */}
<View style={styles.tabs}>
<TouchableOpacity
style={[
styles.tab,
selectedTab === 'user' && { borderBottomColor: theme.color.primary, borderBottomWidth: 2 },
]}
onPress={() => setSelectedTab('user')}
>
<Text style={{
color: selectedTab === 'user' ? theme.color.primary : theme.color.textSecondary,
}}>
{t('USERS')}
</Text>
</TouchableOpacity>
<TouchableOpacity
style={[
styles.tab,
selectedTab === 'group' && { borderBottomColor: theme.color.primary, borderBottomWidth: 2 },
]}
onPress={() => setSelectedTab('group')}
>
<Text style={{
color: selectedTab === 'group' ? theme.color.primary : theme.color.textSecondary,
}}>
{t('GROUPS')}
</Text>
</TouchableOpacity>
</View>
{/* Content */}
<View style={{ flex: 1 }}>
{selectedTab === 'user' ? (
<CometChatUsers
hideHeader={true}
onItemPress={handleUserPress}
/>
) : (
<CometChatGroups
hideHeader={true}
onItemPress={handleGroupPress}
/>
)}
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
flexDirection: 'row',
alignItems: 'center',
padding: 15,
borderBottomWidth: 1,
},
tabs: {
flexDirection: 'row',
},
tab: {
flex: 1,
paddingVertical: 15,
alignItems: 'center',
},
});
export default NewChatScreen;