diff --git a/src/components/room/chat/chat-container.tsx b/src/components/room/chat/chat-container.tsx index 653aabe..1f280b4 100644 --- a/src/components/room/chat/chat-container.tsx +++ b/src/components/room/chat/chat-container.tsx @@ -1,49 +1,81 @@ import { ChevronRightIcon, SendHorizonal, Smile, Users } from 'lucide-react'; -import { useState } from 'react'; +import { useState, type KeyboardEvent } from 'react'; import { v4 as uuidv4 } from 'uuid'; import { Assets } from '@/assets'; import { Typography } from '@/components/typography'; import { useSignaling } from '@/hooks/use-signaling'; import { cn } from '@/lib/utils'; -import { useModalChatOpen, usePeerMe } from '@/store/conf/hooks'; +import { + useChatActions, + useChats, + useModalChatOpen, + usePeerMe, +} from '@/store/conf/hooks'; import { Actions } from '@/types/actions'; +import ChatItem from './chat-item'; +import type { Chat } from '@/types'; const ChatContainer = () => { const chatOpen = useModalChatOpen(); const { signalingService } = useSignaling(); const peerMe = usePeerMe(); const [message, setMessage] = useState(''); + const chats = useChats(); + const chatActions = useChatActions(); const handleSendChat = async () => { - if (!signalingService) return; + if (!signalingService || !peerMe || !message.length) return; + if (!message.trim()) return; + const chatMessage: Chat = { + id: uuidv4(), + text: message, + sender: peerMe, + createdAt: Date.now(), + }; signalingService.sendMessage({ action: Actions.SendChat, - args: { - id: uuidv4(), - text: message, - sender: peerMe, - createdAt: Date.now(), - }, + args: { ...chatMessage }, }); - console.log(message, 'sent'); + chatActions.addChat(chatMessage); + setMessage(''); }; + + const handleOnKeyPress = async ( + event: KeyboardEvent + ) => { + if (event.key === 'Enter' && !event.shiftKey) { + event.preventDefault(); + handleSendChat(); + } + }; + return (
{/* content */} -
- Emtpy Chat - Start Conversation - - There are no messages here yet. Start a conversation by sending a - message. - -
+ {!chats.length ? ( +
+ Emtpy Chat + Start Conversation + + There are no messages here yet. Start a conversation by sending a + message. + +
+ ) : ( +
+
+ {chats.map(chat => ( + + ))} +
+
+ )} {/* input */}
@@ -54,18 +86,16 @@ const ChatContainer = () => {
-
+