components#ChatToolbar JavaScript Examples
The following examples show how to use
components#ChatToolbar.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Chat.jsx From react-chatengine-demo with MIT License | 4 votes |
Chat = () => {
const {
myChats,
setMyChats,
chatConfig,
selectedChat,
selectChatClick,
setSelectedChat,
} = useChat();
useEffect(() => {
console.log('My Chats: ', myChats);
}, [myChats]);
useEffect(() => {
console.log('Selected Chat: ', selectedChat);
}, [selectedChat]);
return (
<>
{!!chatConfig && (
<ChatEngine
hideUI={true}
userName={chatConfig.userName}
projectID={chatConfig.projectID}
userSecret={chatConfig.userSecret}
onConnect={() => {
getChats(chatConfig, setMyChats);
}}
onNewChat={chat => {
if (chat.admin.username === chatConfig.userName) {
selectChatClick(chat);
}
setMyChats([...myChats, chat].sort((a, b) => a.id - b.id));
}}
onDeleteChat={chat => {
if (selectedChat?.id === chat.id) {
setSelectedChat(null);
}
setMyChats(
myChats.filter(c => c.id !== chat.id).sort((a, b) => a.id - b.id),
);
}}
onNewMessage={(chatId, message) => {
if (selectedChat && chatId === selectedChat.id) {
setSelectedChat({
...selectedChat,
messages: [...selectedChat.messages, message],
});
}
const chatThatMessageBelongsTo = myChats.find(c => c.id === chatId);
const filteredChats = myChats.filter(c => c.id !== chatId);
const updatedChat = {
...chatThatMessageBelongsTo,
last_message: message,
};
setMyChats(
[updatedChat, ...filteredChats].sort((a, b) => a.id - b.id),
);
}}
/>
)}
<div className="chat-container">
<LeftRail />
<div className="current-chat">
{selectedChat ? (
<div className="chat">
<ChatToolbar />
<MessageList />
<ChatInput />
</div>
) : (
<div className="no-chat-selected">
<img
src="/img/pointLeft.png"
className="point-left"
alt="point-left"
/>
Select A Chat
</div>
)}
</div>
</div>
</>
);
}