-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: integrate new chat component with web socket
- Loading branch information
1 parent
e83c387
commit f28e5f9
Showing
5 changed files
with
91 additions
and
19 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
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
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,53 @@ | ||
import { useEffect, useReducer, useState } from "react"; | ||
import type { MessageType } from "@/components/shared/Chat/v2/ChatMessages"; | ||
import useChatroom from "./context/useChatroom"; | ||
import useSocketCore from "./context/useSocketCore"; | ||
import useUser from "./useUser"; | ||
|
||
export default function useChat() { | ||
const { lastMessage, sendChatMessage, joinChatroom, leaveChatroom } = | ||
useChatroom(); | ||
const [isChatVisible, toggleChatVisibility] = useReducer( | ||
(preState: boolean) => !preState, | ||
false | ||
); | ||
const { socket } = useSocketCore(); | ||
const [messageList, setMessageList] = useState<MessageType[]>([]); | ||
const { getRoomId } = useUser(); | ||
const roomId = getRoomId(); | ||
|
||
// join chatroom by roomId | ||
useEffect(() => { | ||
if (!roomId) return; | ||
if (!socket || !socket.connected) return; | ||
joinChatroom(roomId); | ||
return () => leaveChatroom(roomId); | ||
}, [joinChatroom, leaveChatroom, roomId, socket, socket?.connected]); | ||
|
||
// update message list while received new message from websocket | ||
useEffect(() => { | ||
if (!lastMessage || !roomId) return; | ||
if (lastMessage.target === `ROOM_${roomId}`) { | ||
setMessageList((prev) => [...prev, lastMessage]); | ||
} | ||
}, [lastMessage, roomId]); | ||
|
||
const handleSubmitText = ( | ||
message: Pick<MessageType, "content" | "target"> | ||
) => { | ||
if (!message.content) return; | ||
const data: Pick<MessageType, "content" | "target"> = { | ||
content: message.content, | ||
target: message.target, | ||
}; | ||
sendChatMessage(data); | ||
}; | ||
|
||
return { | ||
roomId, | ||
messageList, | ||
isChatVisible, | ||
toggleChatVisibility, | ||
handleSubmitText, | ||
}; | ||
} |