import React, { useState, useEffect } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { CometChat } from '@cometchat/chat-sdk-react-native';
import {
CometChatMessageComposer,
CometChatUIEventHandler,
CometChatUIEvents,
useTheme,
useCometChatTranslation,
} from '@cometchat/chat-uikit-react-native';
interface MessageComposerWithBlockProps {
user: CometChat.User;
}
const MessageComposerWithBlock: React.FC<MessageComposerWithBlockProps> = ({ user }) => {
const theme = useTheme();
const { t } = useCometChatTranslation();
const [localUser, setLocalUser] = useState(user);
// Listen for block/unblock events
useEffect(() => {
const listenerId = 'composer_block_listener_' + Date.now();
CometChatUIEventHandler.addUserListener(listenerId, {
ccUserBlocked: (payload: { user: CometChat.User }) => {
if (payload.user.getUid() === localUser.getUid()) {
setLocalUser(payload.user);
}
},
ccUserUnBlocked: (payload: { user: CometChat.User }) => {
if (payload.user.getUid() === localUser.getUid()) {
setLocalUser(payload.user);
}
},
});
return () => {
CometChatUIEventHandler.removeUserListener(listenerId);
};
}, [localUser]);
const handleUnblock = async () => {
try {
await CometChat.unblockUsers([localUser.getUid()]);
const freshUser = await CometChat.getUser(localUser.getUid());
setLocalUser(freshUser);
CometChatUIEventHandler.emitUserEvent(
CometChatUIEvents.ccUserUnBlocked,
{ user: freshUser }
);
} catch (error) {
console.error('Error unblocking user:', error);
}
};
if (localUser.getBlockedByMe()) {
return (
<View style={{
alignItems: 'center',
padding: 20,
backgroundColor: theme.color.background3,
}}>
<Text style={{
color: theme.color.textSecondary,
textAlign: 'center',
marginBottom: 10,
}}>
{t('BLOCKED_USER_DESC')}
</Text>
<TouchableOpacity
onPress={handleUnblock}
style={{
borderWidth: 1,
borderColor: theme.color.borderDefault,
borderRadius: 8,
paddingHorizontal: 20,
paddingVertical: 10,
}}
>
<Text style={{ color: theme.color.textPrimary }}>
{t('UNBLOCK')}
</Text>
</TouchableOpacity>
</View>
);
}
return <CometChatMessageComposer user={localUser} />;
};