11"use client"
22
3- import { useEffect , useState } from "react"
3+ import { useEffect , useRef , useState } from "react"
44import { MessageCircle , Send , Car , Bell } from "lucide-react"
55import { useRecoilState , useRecoilValue } from "recoil" ;
66import {
@@ -19,6 +19,14 @@ function ChatSection() {
1919 const [ newNotice , setNewNotice ] = useRecoilState ( systemNoticeAtom ) ; // 단일 채팅 수신
2020 const playerList = useRecoilValue ( playerListAtom ) ;
2121 const loginUser = useRecoilValue ( loginUserAtom ) ;
22+ const scrollRef = useRef ( null ) ;
23+
24+ useEffect ( ( ) => {
25+ // 메시지 추가 시 마지막 요소로 스크롤
26+ if ( scrollRef . current ) {
27+ scrollRef . current . scrollIntoView ( { behavior : 'smooth' } ) ;
28+ }
29+ } , [ messages ] ) ; // messages가 바뀔 때마다 실행
2230
2331 // 닉네임 → color 매핑 함수
2432 const getColorByNickname = ( nickname ) => {
@@ -54,49 +62,57 @@ function ChatSection() {
5462 }
5563
5664 return (
57- < div className = "flex-1 border-t border-gray-200 flex flex-col" >
58- < div className = "p-4 bg-gray-50 border-b border-gray-200" >
59- < h3 className = "text-base font-semibold text-gray-800 flex items-center" >
60- < MessageCircle className = "w-5 h-5 mr-2 text-blue-600" />
61- 실시간 채팅
62- </ h3 >
63- </ div >
64- < div className = "flex-1 p-4 overflow-y-auto space-y-2" >
65- { messages . map ( ( msg ) => (
66- msg . type === 'chat' ?
67- < div key = { msg . id } className = "flex items-start space-x-3" >
68- < Car className = { `w-5 h-5 mt-0.5 ${ msg . color ? msg . color : "text-gray-500" } ` } />
69- < div >
70- < div className = "text-sm! font-medium text-gray-600" > { msg . nickname } </ div >
71- < div className = "text-sm! text-gray-800" > { msg . message } </ div >
72- </ div >
73- </ div >
74- : < div key = { msg . id } className = "flex items-start space-x-3" >
75- < Bell className = { `w-5 h-5 mt-0.5 "text-gray-500"` } />
76- < div >
77- < div className = "text-sm! text-gray-800" > { msg . noticeMessage } </ div >
65+ < div className = "flex flex-col h-full border-t border-gray-200" >
66+ { /* 제목 */ }
67+ < div className = "p-4 bg-gray-50 border-b border-gray-200" >
68+ < h3 className = "text-base font-semibold text-gray-800 flex items-center" >
69+ < MessageCircle className = "w-5 h-5 mr-2 text-blue-600" />
70+ 실시간 채팅
71+ </ h3 >
72+ </ div >
73+
74+ { /* 채팅 목록 (스크롤) */ }
75+ < div className = "flex-1 min-h-0 p-4 overflow-y-auto space-y-2" >
76+ { messages . map ( ( msg ) => (
77+ msg . type === "chat" ? (
78+ < div key = { msg . id } className = "flex items-start space-x-3" >
79+ < Car className = { `w-5 h-5 mt-0.5 ${ msg . color ?? "text-gray-500" } ` } />
80+ < div >
81+ < div className = "text-sm font-medium text-gray-600" > { msg . nickname } </ div >
82+ < div className = "text-sm text-gray-800" > { msg . message } </ div >
83+ </ div >
84+ </ div >
85+ ) : (
86+ < div key = { msg . id } className = "flex items-start space-x-3" >
87+ < Bell className = "w-5 h-5 mt-0.5 text-gray-500" />
88+ < div >
89+ < div className = "text-sm text-gray-800" > { msg . noticeMessage } </ div >
90+ </ div >
7891 </ div >
79- </ div >
80- ) ) }
81- </ div >
82- < div className = "p-3 border-t border-gray-200" >
83- < div className = "flex space-x-2" >
84- < input
85- type = "text"
86- placeholder = "메시지를 입력하세요..."
87- value = { chatMessage }
88- onChange = { ( e ) => setChatMessage ( e . target . value ) }
89- onKeyPress = { ( e ) => e . key === "Enter" && handleSendMessage ( ) }
90- className = "flex-1 px-3 py-2 border border-gray-300 rounded text-sm focus:outline-none focus:ring-1 focus:ring-red-500"
91- />
92- < button
93- onClick = { handleSendMessage }
94- className = "px-3 py-2 bg-red-600 text-white rounded hover:bg-red-700 transition-colors"
95- >
96- < Send className = "w-4 h-4" />
97- </ button >
92+ )
93+ ) ) }
94+ < div ref = { scrollRef } />
95+ </ div >
96+
97+ { /* 입력창 */ }
98+ < div className = "p-3 border-t border-gray-200 bg-white" >
99+ < div className = "flex space-x-2" >
100+ < input
101+ type = "text"
102+ placeholder = "메시지를 입력하세요..."
103+ value = { chatMessage }
104+ onChange = { ( e ) => setChatMessage ( e . target . value ) }
105+ onKeyPress = { ( e ) => e . key === "Enter" && handleSendMessage ( ) }
106+ className = "flex-1 px-3 py-2 border border-gray-300 rounded text-sm focus:outline-none focus:ring-1 focus:ring-red-500"
107+ />
108+ < button
109+ onClick = { handleSendMessage }
110+ className = "px-3 py-2 bg-red-600 text-white rounded hover:bg-red-700 transition-colors"
111+ >
112+ < Send className = "w-4 h-4" />
113+ </ button >
114+ </ div >
98115 </ div >
99- </ div >
100116 </ div >
101117 )
102118}
0 commit comments