@@ -3,8 +3,10 @@ import useGetChatHistory from '@/hooks/queries/useGetChatHistory';
3
3
import { ChatType } from '@/types/types' ;
4
4
import useGetUserInfo from '@/hooks/queries/useGetUserInfo' ;
5
5
import { Fragment , useRef } from 'react' ;
6
- import useChatScroll from '@/hooks/common/useChatScroll' ;
6
+
7
7
import makeDateSection from '@/utils/makeDateSection' ;
8
+
9
+ import useChatScroll from '@/hooks/common/useChatScroll' ;
8
10
import ChatItem from './ChatItem' ;
9
11
10
12
export default function ChatRoomBox ( {
@@ -13,41 +15,43 @@ export default function ChatRoomBox({
13
15
currentChatList : ChatType [ ] ;
14
16
} ) {
15
17
const roomId = usePathname ( ) . split ( '/' ) [ 2 ] ;
16
- const {
17
- data : chatHistory ,
18
- hasNextPage,
19
- isFetchingNextPage,
20
- fetchNextPage,
21
- } = useGetChatHistory ( roomId ) ;
18
+ const { data : chatHistory , fetchNextPage } = useGetChatHistory ( roomId ) ;
22
19
const { data : userInfo } = useGetUserInfo ( ) ;
23
- const chatRef = useRef < HTMLDivElement > ( null ) ;
20
+ const chatContainerRef = useRef < HTMLDivElement > ( null ) ;
24
21
const bottomRef = useRef < HTMLDivElement > ( null ) ;
22
+ const chatListRef = useRef < HTMLDivElement > ( null ) ;
23
+ const loadMoreRef = useRef < HTMLDivElement > ( null ) ;
25
24
useChatScroll ( {
26
- chatRef ,
25
+ chatContainerRef ,
27
26
bottomRef,
28
- count : currentChatList . length ,
29
27
beforeChatLoadMore : fetchNextPage ,
30
- shouldLoadMore : ! isFetchingNextPage && ! ! hasNextPage ,
28
+ chatListRef,
29
+ loadMoreRef,
31
30
} ) ;
32
31
33
32
const mergedChatList = [ ...( chatHistory ?. pages ?? [ ] ) , ...currentChatList ] ;
34
33
const chatListWithDateSection = makeDateSection (
35
34
mergedChatList && mergedChatList ,
36
35
) ;
37
-
38
36
return (
39
- < div className = "flex flex-col flex-1 p-4 overflow-y-scroll " ref = { chatRef } >
40
- { Object . entries ( chatListWithDateSection ) . map (
41
- ( [ date , chatList ] , index ) => (
42
- // eslint-disable-next-line react/no-array-index-key
43
- < Fragment key = { `date-${ index } ` } >
44
- < div className = "mb-5 text-center text-grey-500 body4" > { date } </ div >
45
- { chatList . map ( ( chat ) => (
46
- < ChatItem key = { chat . id } { ...chat } userId = { userInfo ! . userId } />
47
- ) ) }
48
- </ Fragment >
49
- ) ,
50
- ) }
37
+ < div
38
+ className = "flex flex-col flex-1 p-4 overflow-y-scroll "
39
+ ref = { chatContainerRef }
40
+ >
41
+ < div ref = { loadMoreRef } />
42
+ < div className = "flex flex-col" ref = { chatListRef } >
43
+ { Object . entries ( chatListWithDateSection ) . map (
44
+ ( [ date , chatList ] , index ) => (
45
+ // eslint-disable-next-line react/no-array-index-key
46
+ < Fragment key = { `date-${ index } ` } >
47
+ < div className = "mb-5 text-center text-grey-500 body4" > { date } </ div >
48
+ { chatList . map ( ( chat ) => (
49
+ < ChatItem key = { chat . id } { ...chat } userId = { userInfo ! . userId } />
50
+ ) ) }
51
+ </ Fragment >
52
+ ) ,
53
+ ) }
54
+ </ div >
51
55
< div ref = { bottomRef } />
52
56
</ div >
53
57
) ;
0 commit comments