-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from boostcampwm-2021/refactor/ChatRoom
Refactor/chat room
- Loading branch information
Showing
24 changed files
with
273 additions
and
493 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
client/src/Component/Molecules/Chat/ChatInput/ChatInput.hook.tsx
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,40 @@ | ||
import { postChat } from "@Util/data"; | ||
import { ChangeEvent, ChangeEventHandler, useRef } from "react"; | ||
import ClientSocket from "Socket"; | ||
|
||
type chatRoomInfo = { | ||
uid: string; | ||
chatRoomId: number; | ||
}; | ||
|
||
export const useChatMessageControl = ({ uid, chatRoomId }: chatRoomInfo) => { | ||
const messageRef = useRef<HTMLInputElement>(null); | ||
const sendMessage = () => { | ||
if (!messageRef.current?.value) return; | ||
sendChat({ uid, chatRoomId, message: messageRef.current.value }); | ||
messageRef.current.value = ""; | ||
}; | ||
const handleEnterPress = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.code !== "Enter") return; | ||
sendMessage(); | ||
}; | ||
return { messageRef, sendMessage, handleEnterPress }; | ||
}; | ||
|
||
type sendChatPropsType = chatRoomInfo & { message: string }; | ||
const sendChat = ({ uid, chatRoomId, message }: sendChatPropsType) => { | ||
const { socket } = new ClientSocket(uid); | ||
socket?.emit("sendChat", { chatRoomId, message: { from: uid, message, read: false, source: "" } }); | ||
}; | ||
|
||
export const useChatImageControl = ({ uid, chatRoomId }: chatRoomInfo) => { | ||
const imageInputTag = useRef<HTMLInputElement>(null); | ||
const handleImageButtonClick = () => (imageInputTag.current as HTMLInputElement).click(); | ||
|
||
const changeImage: ChangeEventHandler<HTMLInputElement> = (event: ChangeEvent<HTMLInputElement>) => { | ||
if (!event.target.files) return; | ||
postChat(chatRoomId, uid, event.target.files[0]); | ||
}; | ||
|
||
return { imageInputTag, handleImageButtonClick, changeImage }; | ||
}; |
57 changes: 57 additions & 0 deletions
57
client/src/Component/Molecules/Chat/ChatInput/ChatInput.tsx
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,57 @@ | ||
import { css } from "@emotion/react"; | ||
import { useRecoilValue } from "recoil"; | ||
import { Input } from "@Atom/."; | ||
import { userChatRoomInfo } from "@Recoil/ChatData"; | ||
import { useChatImageControl, useChatMessageControl } from "./ChatInput.hook"; | ||
|
||
export const ChatInput = () => { | ||
const { chatRoomId, id: uid } = useRecoilValue(userChatRoomInfo); | ||
const { messageRef, sendMessage, handleEnterPress } = useChatMessageControl({ uid, chatRoomId }); | ||
const { imageInputTag, handleImageButtonClick, changeImage } = useChatImageControl({ uid, chatRoomId }); | ||
|
||
return ( | ||
<div css={InputContainer}> | ||
<div css={sendImageStyle} aria-hidden="true" onClick={handleImageButtonClick} /> | ||
<Input placeholder="메시지를 입력하세요" ref={messageRef} onKeyPress={handleEnterPress} /> | ||
<div css={sendButtonStyle} aria-hidden="true" onClick={sendMessage} /> | ||
<input ref={imageInputTag} type="file" accept="image/*" css={ImageInputStlye} onChange={changeImage} /> | ||
</div> | ||
); | ||
}; | ||
|
||
const ImageSendButton = "/Asset/ImageSendButton.svg"; | ||
const SendButton = "/Asset/SendButton.svg"; | ||
|
||
const InputContainer = css` | ||
display: flex; | ||
justify-content: center; | ||
padding-top: 10px; | ||
align-items: center; | ||
height: fit-content; | ||
`; | ||
|
||
const sendImageStyle = css` | ||
background-image: url(${ImageSendButton}); | ||
background-position: center; | ||
background-repeat: no-repeat; | ||
background-size: cover; | ||
width: 25px; | ||
height: 25px; | ||
margin-right: 10px; | ||
cursor: pointer; | ||
`; | ||
|
||
const sendButtonStyle = css` | ||
background-image: url(${SendButton}); | ||
background-position: center; | ||
background-repeat: no-repeat; | ||
background-size: cover; | ||
width: 25px; | ||
height: 25px; | ||
margin-left: 10px; | ||
cursor: pointer; | ||
`; | ||
|
||
const ImageInputStlye = css` | ||
display: none; | ||
`; |
Oops, something went wrong.