@@ -2,21 +2,27 @@ import { ChatBoxContainer, Textarea, SendButton } from './styles';
22import { useEffect , useRef , useState } from 'react' ;
33import { useRecoilValue } from 'recoil' ;
44import { useParams } from 'react-router-dom' ;
5- import { OpponentInfoAtom } from '../../../../recoil/util/OpponentInfo' ;
6- import { useSocket } from '../../../../context/SocketProvider' ;
5+ import { OpponentInfoAtom } from '@recoil/util/OpponentInfo' ;
6+ import { useSocket } from '@context/SocketProvider' ;
7+ import { getCurrentUserId } from '@utils/getCurrentUserId' ;
78
89const ChatBox : React . FC = ( ) => {
9- const opponentInfo = useRecoilValue ( OpponentInfoAtom ) ;
10- const storageValue = localStorage . getItem ( 'my_id' ) ;
11- const userId = storageValue ? Number ( storageValue ) : - 1 ;
12- const { chatRoomId } = useParams ( ) ;
13- const textareaRef = useRef < HTMLTextAreaElement | null > ( null ) ;
14-
1510 const [ newMessage , setNewMessage ] = useState ( '' ) ;
16-
11+ const textareaRef = useRef < HTMLTextAreaElement | null > ( null ) ;
1712 const socket = useSocket ( ) ;
13+
14+ const { chatRoomId } = useParams ( ) ;
15+ const currentUserId = getCurrentUserId ( ) ;
16+ const opponentInfo = useRecoilValue ( OpponentInfoAtom ) ;
1817 const isOpponentValid = ! ! ( opponentInfo && opponentInfo . id ) ;
1918
19+ useEffect ( ( ) => {
20+ if ( textareaRef . current && ! isOpponentValid ) {
21+ textareaRef . current . disabled = true ;
22+ textareaRef . current . placeholder = '메시지를 보낼 수 없습니다.' ;
23+ }
24+ } , [ ] ) ;
25+
2026 // textarea 내용에 따라 높이 조정
2127 useEffect ( ( ) => {
2228 if ( textareaRef . current ) {
@@ -25,56 +31,48 @@ const ChatBox: React.FC = () => {
2531 }
2632 } , [ newMessage ] ) ;
2733
28- useEffect ( ( ) => {
29- if ( textareaRef . current && ! isOpponentValid ) {
30- textareaRef . current . disabled = true ;
31- textareaRef . current . placeholder = '메시지를 보낼 수 없습니다.' ;
32- }
33- } , [ ] ) ;
34-
35- const onChangeMessage = ( e : React . ChangeEvent < HTMLTextAreaElement > ) : void => {
34+ const handleMessageChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
3635 setNewMessage ( e . target . value ) ;
3736 } ;
3837
39- const sendNewMessage = ( ) : void => {
38+ const handleEnterKeyDown = ( e : React . KeyboardEvent < HTMLTextAreaElement > ) => {
39+ if ( e . key === 'Enter' && ! e . shiftKey ) {
40+ e . preventDefault ( ) ;
41+ handleNewMessageSubmit ( ) ;
42+ }
43+ } ;
44+
45+ const handleNewMessageSubmit = ( ) => {
4046 if ( newMessage === '' ) {
4147 return ;
4248 }
4349
44- // 메시지 전송
50+ // 메시지 전송 api
4551 if ( socket ) {
4652 const sendMessageRequest = {
4753 chatRoomId : Number ( chatRoomId ) ,
4854 toUserId : opponentInfo ?. id ,
4955 content : newMessage ,
50- fromUserId : userId ,
56+ fromUserId : currentUserId ,
5157 createdAt : new Date ( ) . toISOString ( ) ,
5258 } ;
53-
5459 socket . emit ( 'sendMessage' , sendMessageRequest ) ;
5560 setNewMessage ( '' ) ;
5661 }
5762 } ;
5863
59- const onKeyDown = ( e : React . KeyboardEvent < HTMLTextAreaElement > ) : void => {
60- if ( e . key === 'Enter' && ! e . shiftKey ) {
61- e . preventDefault ( ) ;
62- sendNewMessage ( ) ;
63- }
64- } ;
65-
6664 return (
6765 < ChatBoxContainer >
6866 < Textarea
6967 $isOpponentValid = { isOpponentValid }
7068 placeholder = "메시지 보내기"
7169 ref = { textareaRef }
7270 value = { newMessage }
73- onKeyDown = { onKeyDown }
74- onChange = { onChangeMessage }
75- onSubmit = { sendNewMessage }
71+ onChange = { handleMessageChange }
72+ onKeyDown = { handleEnterKeyDown }
73+ onSubmit = { handleNewMessageSubmit }
7674 />
77- < SendButton $isOpponentValid = { isOpponentValid } onClick = { sendNewMessage } />
75+ < SendButton $isOpponentValid = { isOpponentValid } onClick = { handleNewMessageSubmit } />
7876 </ ChatBoxContainer >
7977 ) ;
8078} ;
0 commit comments