@@ -46,13 +46,18 @@ const ChatRoom: React.FC = () => {
4646 const chatWindowRef = useRef < HTMLDivElement > ( null ) ;
4747 const messageLengthRef = useRef ( 0 ) ;
4848
49+ const nav = useNavigate ( ) ;
50+ const socket = useSocket ( ) ;
51+
4952 const storageValue = localStorage . getItem ( 'my_id' ) ;
5053 const userId = storageValue ? Number ( storageValue ) : - 1 ;
5154 const { chatRoomId } = useParams ( ) ;
5255 const opponentInfo = useRecoilValue ( OpponentInfoAtom ) ;
5356
54- const nav = useNavigate ( ) ;
55- const socket = useSocket ( ) ;
57+ // 메시지 수신 시 아래로 스크롤 (스크롤 아래 고정)
58+ const scrollToBottom = ( ref : React . RefObject < HTMLDivElement > ) => {
59+ if ( ref . current ) ref . current . scrollIntoView ( ) ;
60+ } ;
5661
5762 // 프로필 사진 클릭 시 프로필 페이지로 이동
5863 const handleUserClick = useCallback ( ( ) => {
@@ -100,7 +105,7 @@ const ChatRoom: React.FC = () => {
100105 }
101106 } ;
102107
103- // 전체 메시지 조회
108+ // 전체 메시지 조회 socket api
104109 const getChatRoomMessages = ( data : chatRoomMessagesData [ ] ) => {
105110 setAllMessages ( data ) ;
106111 if ( data . length > messageLengthRef . current ) {
@@ -109,12 +114,37 @@ const ChatRoom: React.FC = () => {
109114 setIsLoading ( false ) ;
110115 } ;
111116
112- // 새 메시지 수신
117+ // 새 메시지 수신 socket api
113118 const getNewMessage = ( data : chatRoomMessagesData ) => {
114119 setAllMessages ( ( prevMessages ) => [ ...prevMessages , data ] ) ;
115120 setIsScroll ( ( prev ) => ! prev ) ;
116121 } ;
117122
123+ // 채팅방 입장 시 스크롤 아래로 이동
124+ useEffect ( ( ) => {
125+ const messagesContainer = chatWindowRef . current ?. parentElement ;
126+
127+ if ( messagesContainer ) {
128+ messagesContainer . style . scrollBehavior = 'auto' ;
129+ messagesContainer . scrollTop = messagesContainer . scrollHeight ;
130+ }
131+ } , [ ] ) ;
132+
133+ // 메시지 수신 시 스크롤 아래로 이동
134+ useEffect ( ( ) => {
135+ if ( isScroll ) {
136+ scrollToBottom ( chatWindowRef ) ;
137+ setIsScroll ( ( prev ) => ! prev ) ;
138+ }
139+ } , [ isScroll ] ) ;
140+
141+ // 메시지 수신 시 렌더링에 필요한 정보 추가
142+ // 이거 위에랑 합칠수없나?
143+ useEffect ( ( ) => {
144+ const temp = createExtendedMessages ( allMessages , userId , opponentInfo ) ;
145+ setextendedMessages ( temp ) ;
146+ } , [ allMessages ] ) ;
147+
118148 useEffect ( ( ) => {
119149 if ( socket ) {
120150 // 채팅방 입장
@@ -137,34 +167,6 @@ const ChatRoom: React.FC = () => {
137167 } ;
138168 } , [ chatRoomId , socket ] ) ;
139169
140- // 메시지 렌더링에 필요한 정보 추가
141- useEffect ( ( ) => {
142- const temp = createExtendedMessages ( allMessages , userId , opponentInfo ) ;
143- setextendedMessages ( temp ) ;
144- } , [ allMessages ] ) ;
145-
146- const scrollToBottom = ( ref : React . RefObject < HTMLDivElement > ) => {
147- if ( ref . current ) ref . current . scrollIntoView ( ) ;
148- } ;
149-
150- useEffect ( ( ) => {
151- // 채팅방 입장 시 스크롤 아래로 이동
152- const messagesContainer = chatWindowRef . current ?. parentElement ;
153-
154- if ( messagesContainer ) {
155- messagesContainer . style . scrollBehavior = 'auto' ;
156- messagesContainer . scrollTop = messagesContainer . scrollHeight ;
157- }
158- } , [ ] ) ;
159-
160- // 메시지 수신 시 스크롤 아래로 이동
161- useEffect ( ( ) => {
162- if ( isScroll ) {
163- scrollToBottom ( chatWindowRef ) ;
164- setIsScroll ( ( prev ) => ! prev ) ;
165- }
166- } , [ isScroll ] ) ;
167-
168170 const bottomSheetMenuProps : BottomSheetMenuProps = {
169171 items : [
170172 {
@@ -218,7 +220,7 @@ const ChatRoom: React.FC = () => {
218220 } ,
219221 } ;
220222
221- const kebabMenuBottomSheet : BottomSheetProps < BottomSheetMenuProps > = {
223+ const kebabMenuBottomSheetProps : BottomSheetProps < BottomSheetMenuProps > = {
222224 isOpenBottomSheet : isMenuBottomSheetOpen ,
223225 isHandlerVisible : true ,
224226 Component : BottomSheetMenu ,
@@ -230,11 +232,6 @@ const ChatRoom: React.FC = () => {
230232
231233 return (
232234 < OODDFrame >
233- { isLoading && < Loading /> }
234- { isLeaveModalOpen && < Modal { ...leaveModalProps } /> }
235- { isBlockModalOpen && < Modal { ...blockModalProps } /> }
236- { isStatusModalOpen && < Modal { ...statusModalProps } /> }
237- < BottomSheet { ...kebabMenuBottomSheet } />
238235 < TopBar
239236 text = { opponentInfo ?. nickname || '알수없음' }
240237 LeftButtonSrc = { Back }
@@ -264,7 +261,12 @@ const ChatRoom: React.FC = () => {
264261 } ) }
265262 < div ref = { chatWindowRef } />
266263 </ MessagesContainer >
267- < ChatBox > </ ChatBox >
264+ < ChatBox />
265+ { isLoading && < Loading /> }
266+ { isLeaveModalOpen && < Modal { ...leaveModalProps } /> }
267+ { isBlockModalOpen && < Modal { ...blockModalProps } /> }
268+ { isStatusModalOpen && < Modal { ...statusModalProps } /> }
269+ < BottomSheet { ...kebabMenuBottomSheetProps } />
268270 </ OODDFrame >
269271 ) ;
270272} ;
0 commit comments