Skip to content

Commit ec67947

Browse files
committed
fix: chat rooms refetch issue
1 parent 2692120 commit ec67947

File tree

1 file changed

+20
-3
lines changed
  • packages/components/modules/messages/ChatRoomsList

1 file changed

+20
-3
lines changed

packages/components/modules/messages/ChatRoomsList/index.tsx

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
'use client'
22

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'
413

514
import { Searchbar as DefaultSearchbar, LoadingState } from '@baseapp-frontend/design-system'
615

@@ -37,21 +46,22 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
3746

3847
const [isPending, startTransition] = useTransition()
3948
const { control, reset, watch } = useForm({ defaultValues: { search: '' } })
49+
const cursor = useRef<string | null | undefined>(null)
4050

4151
const isInUnreadTab = tab === CHAT_TAB_VALUES.unread
4252
const isInArchivedTab = tab === CHAT_TAB_VALUES.archived
4353

4454
const handleSearchChange: ChangeEventHandler<HTMLInputElement> = (e) => {
4555
const value = e.target.value || ''
4656
startTransition(() => {
47-
refetch({ q: value })
57+
refetch({ q: value, cursor: cursor.current })
4858
})
4959
}
5060

5161
const handleSearchClear = () => {
5262
startTransition(() => {
5363
reset()
54-
refetch({ q: '' })
64+
refetch({ q: '', cursor: cursor.current })
5565
})
5666
}
5767

@@ -62,12 +72,19 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
6272
{
6373
unreadMessages: newTab === CHAT_TAB_VALUES.unread,
6474
archived: newTab === CHAT_TAB_VALUES.archived,
75+
cursor: cursor.current,
6576
},
6677
{ fetchPolicy: 'store-and-network' },
6778
)
6879
})
6980
}
7081

82+
useEffect(() => {
83+
if (data?.chatRooms?.pageInfo?.endCursor) {
84+
cursor.current = data.chatRooms.pageInfo.endCursor
85+
}
86+
}, [data?.chatRooms?.pageInfo?.endCursor])
87+
7188
const { id: selectedRoom, setChatRoom } = useChatRoom()
7289
const chatRooms = useMemo(
7390
() => data?.chatRooms?.edges?.filter((edge) => edge?.node).map((edge) => edge?.node) || [],

0 commit comments

Comments
 (0)