Skip to content

Commit 50f6aa8

Browse files
authored
Merge pull request #298 from boostcampwm-2021/refactor/ChatRoom
Refactor/chat room
2 parents 53f2fa8 + 74de9ae commit 50f6aa8

File tree

24 files changed

+273
-493
lines changed

24 files changed

+273
-493
lines changed

Diff for: client/public/Asset/게임/0.jpeg

-2.66 MB
Binary file not shown.

Diff for: client/public/Asset/게임/1.gif

-966 KB
Binary file not shown.

Diff for: client/public/Asset/게임/2.png

-310 KB
Binary file not shown.

Diff for: client/src/Component/Atom/Video.tsx

+3-17
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,10 @@ import React, { useEffect, useRef, useState } from "react";
22
import styled from "@emotion/styled";
33
import { css } from "@emotion/react";
44

5-
const GameStyle = css`
6-
top: -10%;
7-
width: 150px;
8-
height: 150px;
9-
`;
10-
11-
const GatherStyle = css`
12-
width: 150px;
13-
height: 150px;
14-
`;
15-
16-
const containerStyle = (props: { type: string }) => css`
5+
const containerStyle = css`
176
width: 240px;
187
height: 240px;
198
border: 1px solid #000000;
20-
${props.type === "Game" && GameStyle}
21-
${props.type === "Gather" && GatherStyle}
229
`;
2310

2411
const VideoContainer = styled.video`
@@ -29,11 +16,10 @@ const VideoContainer = styled.video`
2916

3017
interface Props {
3118
stream: MediaStream;
32-
type: string;
3319
muted?: boolean;
3420
}
3521

36-
export const Video = ({ type, stream, muted }: Props) => {
22+
export const Video = ({ stream, muted }: Props) => {
3723
const videoRef = useRef<HTMLVideoElement>(null);
3824
const [isMuted, setIsMuted] = useState<boolean>(false);
3925

@@ -45,7 +31,7 @@ export const Video = ({ type, stream, muted }: Props) => {
4531
}, [stream, muted]);
4632

4733
return (
48-
<div css={containerStyle({ type })}>
34+
<div css={containerStyle}>
4935
<VideoContainer ref={videoRef} muted={isMuted} autoPlay />
5036
</div>
5137
);

Diff for: client/src/Component/Molecules/Chat/ChatInput.tsx

-87
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { postChat } from "@Util/data";
2+
import { ChangeEvent, ChangeEventHandler, useRef } from "react";
3+
import ClientSocket from "Socket";
4+
5+
type chatRoomInfo = {
6+
uid: string;
7+
chatRoomId: number;
8+
};
9+
10+
export const useChatMessageControl = ({ uid, chatRoomId }: chatRoomInfo) => {
11+
const messageRef = useRef<HTMLInputElement>(null);
12+
const sendMessage = () => {
13+
if (!messageRef.current?.value) return;
14+
sendChat({ uid, chatRoomId, message: messageRef.current.value });
15+
messageRef.current.value = "";
16+
};
17+
const handleEnterPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
18+
if (e.code !== "Enter") return;
19+
sendMessage();
20+
};
21+
return { messageRef, sendMessage, handleEnterPress };
22+
};
23+
24+
type sendChatPropsType = chatRoomInfo & { message: string };
25+
const sendChat = ({ uid, chatRoomId, message }: sendChatPropsType) => {
26+
const { socket } = new ClientSocket(uid);
27+
socket?.emit("sendChat", { chatRoomId, message: { from: uid, message, read: false, source: "" } });
28+
};
29+
30+
export const useChatImageControl = ({ uid, chatRoomId }: chatRoomInfo) => {
31+
const imageInputTag = useRef<HTMLInputElement>(null);
32+
const handleImageButtonClick = () => (imageInputTag.current as HTMLInputElement).click();
33+
34+
const changeImage: ChangeEventHandler<HTMLInputElement> = (event: ChangeEvent<HTMLInputElement>) => {
35+
if (!event.target.files) return;
36+
postChat(chatRoomId, uid, event.target.files[0]);
37+
};
38+
39+
return { imageInputTag, handleImageButtonClick, changeImage };
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { css } from "@emotion/react";
2+
import { useRecoilValue } from "recoil";
3+
import { Input } from "@Atom/.";
4+
import { userChatRoomInfo } from "@Recoil/ChatData";
5+
import { useChatImageControl, useChatMessageControl } from "./ChatInput.hook";
6+
7+
export const ChatInput = () => {
8+
const { chatRoomId, id: uid } = useRecoilValue(userChatRoomInfo);
9+
const { messageRef, sendMessage, handleEnterPress } = useChatMessageControl({ uid, chatRoomId });
10+
const { imageInputTag, handleImageButtonClick, changeImage } = useChatImageControl({ uid, chatRoomId });
11+
12+
return (
13+
<div css={InputContainer}>
14+
<div css={sendImageStyle} aria-hidden="true" onClick={handleImageButtonClick} />
15+
<Input placeholder="메시지를 입력하세요" ref={messageRef} onKeyPress={handleEnterPress} />
16+
<div css={sendButtonStyle} aria-hidden="true" onClick={sendMessage} />
17+
<input ref={imageInputTag} type="file" accept="image/*" css={ImageInputStlye} onChange={changeImage} />
18+
</div>
19+
);
20+
};
21+
22+
const ImageSendButton = "/Asset/ImageSendButton.svg";
23+
const SendButton = "/Asset/SendButton.svg";
24+
25+
const InputContainer = css`
26+
display: flex;
27+
justify-content: center;
28+
padding-top: 10px;
29+
align-items: center;
30+
height: fit-content;
31+
`;
32+
33+
const sendImageStyle = css`
34+
background-image: url(${ImageSendButton});
35+
background-position: center;
36+
background-repeat: no-repeat;
37+
background-size: cover;
38+
width: 25px;
39+
height: 25px;
40+
margin-right: 10px;
41+
cursor: pointer;
42+
`;
43+
44+
const sendButtonStyle = css`
45+
background-image: url(${SendButton});
46+
background-position: center;
47+
background-repeat: no-repeat;
48+
background-size: cover;
49+
width: 25px;
50+
height: 25px;
51+
margin-left: 10px;
52+
cursor: pointer;
53+
`;
54+
55+
const ImageInputStlye = css`
56+
display: none;
57+
`;

0 commit comments

Comments
 (0)