-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useChatRoomMessages } from './api/useChat'; | ||
import { ChatMessage } from './api/types/chat'; | ||
import { useSocket } from './socket/useSocket'; | ||
|
||
/** | ||
* | ||
* @param chatRoomId 현재 접속한 채팅방의 id | ||
* @returns messages: 채팅방 메세지 | sendMessage: 메세지 보내는 함수 | ||
*/ | ||
export const useChat = (chatRoomId: string) => { | ||
const { socket } = useSocket(); | ||
const { data: previousMessages } = useChatRoomMessages({ | ||
chatRoomId, | ||
}); | ||
|
||
const [messages, setMessages] = useState<ChatMessage[]>( | ||
previousMessages?.data || [] | ||
); | ||
|
||
useEffect(() => { | ||
if (!socket) return; | ||
socket.emit('join', chatRoomId); | ||
|
||
const handleReceiveMessage = (msg: ChatMessage[]) => { | ||
setMessages((prevMessages) => [...prevMessages, ...msg]); | ||
}; | ||
|
||
socket.on('receive', handleReceiveMessage); | ||
|
||
return () => { | ||
socket.off('receive', handleReceiveMessage); | ||
}; | ||
}, [chatRoomId, socket]); | ||
|
||
const sendMessage = useCallback( | ||
(message: object, senderUuid: string) => { | ||
if (socket) { | ||
const socketUniqueId = `${socket.id}-${senderUuid}`; | ||
socket.emit('send', message, socketUniqueId); | ||
} | ||
}, | ||
[socket] | ||
); | ||
|
||
return { messages, sendMessage }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Suspense } from 'react'; | ||
import ChatRoom from './ChatRoom'; | ||
import LoadingSpinner from '@/pages/Loading/LoadingSpinner'; | ||
|
||
const ChatRoomPage = () => { | ||
return ( | ||
<Suspense fallback={<LoadingSpinner />}> | ||
<ChatRoom /> | ||
</Suspense> | ||
); | ||
}; | ||
|
||
export default ChatRoomPage; |