import { useState, useEffect } from "react";
import { CometChat } from "@cometchat/chat-sdk-javascript";
import {
CometChatProvider,
CometChatMessageList,
CometChatMessageComposer,
CometChatMessageHeader,
CometChatConfirmDialog,
usePublishEvent,
useCometChatEvents,
} from "@cometchat/chat-uikit-react";
import type { CometChatEvent } from "@cometchat/chat-uikit-react";
import "@cometchat/chat-uikit-react/styles";
function ChatView({ targetUid }: { targetUid: string }) {
const [user, setUser] = useState<CometChat.User | null>(null);
const [isBlocked, setIsBlocked] = useState(false);
const [showBlockDialog, setShowBlockDialog] = useState(false);
const publish = usePublishEvent();
useEffect(() => {
async function fetchUser() {
try {
const fetchedUser = await CometChat.getUser(targetUid);
setUser(fetchedUser);
setIsBlocked(fetchedUser.getBlockedByMe());
} catch (error) {
console.error("Error fetching user:", error);
}
}
fetchUser();
}, [targetUid]);
useCometChatEvents(
(event: CometChatEvent) => {
if (!user) return;
if (event.type === "ui:user/blocked" && event.user.getUid() === user.getUid()) {
setIsBlocked(true);
}
if (event.type === "ui:user/unblocked" && event.user.getUid() === user.getUid()) {
setIsBlocked(false);
}
},
[user?.getUid()]
);
async function handleBlock() {
if (!user) return;
try {
await CometChat.blockUsers([user.getUid()]);
user.setBlockedByMe(true);
setIsBlocked(true);
setShowBlockDialog(false);
publish({ type: "ui:user/blocked", user });
} catch (error) {
console.error("Error blocking user:", error);
}
}
async function handleUnblock() {
if (!user) return;
try {
await CometChat.unblockUsers([user.getUid()]);
user.setBlockedByMe(false);
setIsBlocked(false);
publish({ type: "ui:user/unblocked", user });
} catch (error) {
console.error("Error unblocking user:", error);
}
}
if (!user) return <div>Loading...</div>;
return (
<div style={{ display: "flex", flexDirection: "column", height: "100vh" }}>
{/* Confirmation dialog */}
{showBlockDialog && (
<CometChatConfirmDialog.Root isOpen={true} onClose={() => setShowBlockDialog(false)}>
<CometChatConfirmDialog.Icon />
<CometChatConfirmDialog.Content
title="Block Contact"
messageText="Are you sure you want to block this user? They won't be able to send you messages."
/>
<CometChatConfirmDialog.Actions
confirmButtonText="Block"
onConfirm={handleBlock}
onCancel={() => setShowBlockDialog(false)}
/>
</CometChatConfirmDialog.Root>
)}
{/* Header with block/unblock action */}
<CometChatMessageHeader
user={user}
auxiliaryButtonView={
<button
onClick={isBlocked ? handleUnblock : () => setShowBlockDialog(true)}
style={{ background: "none", border: "none", cursor: "pointer", fontSize: "14px", color: isBlocked ? "#3399ff" : "#f44336" }}
>
{isBlocked ? "Unblock" : "Block"}
</button>
}
/>
{/* Message list */}
<div style={{ flex: 1, overflow: "hidden" }}>
<CometChatMessageList user={user} />
</div>
{/* Composer or blocked prompt */}
{!isBlocked ? (
<CometChatMessageComposer user={user} />
) : (
<div style={{ padding: "16px", textAlign: "center", background: "#f5f5f5" }}>
<p>Cannot send a message to a blocked user.</p>
<span
onClick={handleUnblock}
role="button"
tabIndex={0}
style={{ color: "#3399ff", cursor: "pointer" }}
>
Click to unblock.
</span>
</div>
)}
</div>
);
}
function App() {
return (
<CometChatProvider
appId="YOUR_APP_ID"
region="YOUR_REGION"
authKey="YOUR_AUTH_KEY"
>
<ChatView targetUid="user-to-chat-with" />
</CometChatProvider>
);
}
export default App;