1
1
'use client'
2
2
3
- import { ChangeEventHandler , FC , useCallback , useMemo , useState , useTransition } from 'react'
3
+ import {
4
+ ChangeEventHandler ,
5
+ FC ,
6
+ useCallback ,
7
+ useEffect ,
8
+ useMemo ,
9
+ useRef ,
10
+ useState ,
11
+ useTransition ,
12
+ } from 'react'
4
13
5
14
import { Searchbar as DefaultSearchbar , LoadingState } from '@baseapp-frontend/design-system'
6
15
@@ -37,21 +46,22 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
37
46
38
47
const [ isPending , startTransition ] = useTransition ( )
39
48
const { control, reset, watch } = useForm ( { defaultValues : { search : '' } } )
49
+ const cursor = useRef < string | null | undefined > ( null )
40
50
41
51
const isInUnreadTab = tab === CHAT_TAB_VALUES . unread
42
52
const isInArchivedTab = tab === CHAT_TAB_VALUES . archived
43
53
44
54
const handleSearchChange : ChangeEventHandler < HTMLInputElement > = ( e ) => {
45
55
const value = e . target . value || ''
46
56
startTransition ( ( ) => {
47
- refetch ( { q : value } )
57
+ refetch ( { q : value , cursor : cursor . current } )
48
58
} )
49
59
}
50
60
51
61
const handleSearchClear = ( ) => {
52
62
startTransition ( ( ) => {
53
63
reset ( )
54
- refetch ( { q : '' } )
64
+ refetch ( { q : '' , cursor : cursor . current } )
55
65
} )
56
66
}
57
67
@@ -62,12 +72,19 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
62
72
{
63
73
unreadMessages : newTab === CHAT_TAB_VALUES . unread ,
64
74
archived : newTab === CHAT_TAB_VALUES . archived ,
75
+ cursor : cursor . current ,
65
76
} ,
66
77
{ fetchPolicy : 'store-and-network' } ,
67
78
)
68
79
} )
69
80
}
70
81
82
+ useEffect ( ( ) => {
83
+ if ( data ?. chatRooms ?. pageInfo ?. endCursor ) {
84
+ cursor . current = data . chatRooms . pageInfo . endCursor
85
+ }
86
+ } , [ data ?. chatRooms ?. pageInfo ?. endCursor ] )
87
+
71
88
const { id : selectedRoom , setChatRoom } = useChatRoom ( )
72
89
const chatRooms = useMemo (
73
90
( ) => data ?. chatRooms ?. edges ?. filter ( ( edge ) => edge ?. node ) . map ( ( edge ) => edge ?. node ) || [ ] ,
0 commit comments