Skip to content

Commit 3635abb

Browse files
committed
fix: chat rooms refetch issue
1 parent 8de60b1 commit 3635abb

File tree

1 file changed

+58
-33
lines changed
  • packages/components/modules/messages/ChatRoomsList

1 file changed

+58
-33
lines changed

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

+58-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
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+
useState,
10+
useTransition,
11+
} from 'react'
412

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

@@ -29,6 +37,7 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
2937
VirtuosoProps = {},
3038
}) => {
3139
const [tab, setTab] = useState<ChatTabValues>(CHAT_TAB_VALUES.active)
40+
const [renderList, setRenderList] = useState<boolean>(true)
3241

3342
const [isRefetchPending, startRefetchTransition] = useTransition()
3443
const { data, loadNext, isLoadingNext, hasNext, refetch } = useRoomsList(
@@ -45,39 +54,53 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
4554
const handleSearchChange: ChangeEventHandler<HTMLInputElement> = (e) => {
4655
const value = e.target.value || ''
4756
startTransition(() => {
48-
refetch({
49-
q: value,
50-
unreadMessages: isInUnreadTab,
51-
archived: isInArchivedTab,
52-
})
57+
refetch(
58+
{
59+
q: value,
60+
unreadMessages: isInUnreadTab,
61+
archived: isInArchivedTab,
62+
},
63+
{ fetchPolicy: 'network-only' },
64+
)
5365
})
5466
}
5567

5668
const handleSearchClear = () => {
5769
startTransition(() => {
5870
reset()
59-
refetch({
60-
q: '',
61-
unreadMessages: isInUnreadTab,
62-
archived: isInArchivedTab,
63-
})
71+
refetch(
72+
{
73+
q: '',
74+
unreadMessages: isInUnreadTab,
75+
archived: isInArchivedTab,
76+
},
77+
{ fetchPolicy: 'network-only' },
78+
)
6479
})
6580
}
6681

6782
const handleChange = (event: React.SyntheticEvent, newTab: string) => {
6883
setTab(newTab as ChatTabValues)
6984
startRefetchTransition(() => {
85+
setRenderList(false)
7086
refetch(
7187
{
7288
q: searchValue,
7389
unreadMessages: newTab === CHAT_TAB_VALUES.unread,
7490
archived: newTab === CHAT_TAB_VALUES.archived,
7591
},
76-
{ fetchPolicy: 'store-and-network' },
92+
{ fetchPolicy: 'network-only' },
7793
)
7894
})
7995
}
8096

97+
// Virtuoso has a bug where it does not recognize endReached if the data is changed while the component is still mounted.
98+
// The `renderList` state is a workaround to force the component to dismount and remount,
99+
// to ensure that Virtuoso will recognized endReached everytime the tab is changed
100+
useEffect(() => {
101+
setRenderList(true)
102+
}, [data])
103+
81104
const { id: selectedRoom, setChatRoom } = useChatRoom()
82105
const chatRooms = useMemo(
83106
() => data?.chatRooms?.edges?.filter((edge) => edge?.node).map((edge) => edge?.node) || [],
@@ -127,29 +150,14 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
127150
)
128151

129152
const renderListContent = () => {
130-
const emptyChatRoomsList = chatRooms.length === 0
131-
132-
if (!isPending && searchValue && emptyChatRoomsList) return <SearchNotFoundState />
153+
const hasEmptyStates = !isPending && chatRooms.length === 0
133154

134-
if (!isPending && emptyChatRoomsList) return <EmptyChatRoomsState />
155+
if (hasEmptyStates) {
156+
if (searchValue) return <SearchNotFoundState />
157+
return <EmptyChatRoomsState />
158+
}
135159

136-
return (
137-
<Virtuoso
138-
data={chatRooms}
139-
overscan={1}
140-
itemContent={(_index, item) => renderItem(item)}
141-
style={{ scrollbarWidth: 'none' }}
142-
components={{
143-
Footer: renderLoadingState,
144-
}}
145-
endReached={() => {
146-
if (hasNext) {
147-
loadNext(5)
148-
}
149-
}}
150-
{...VirtuosoProps}
151-
/>
152-
)
160+
return undefined
153161
}
154162

155163
return (
@@ -195,6 +203,23 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
195203
<Tab label={renderTabLabel(CHAT_TAB_VALUES.archived)} value={CHAT_TAB_VALUES.archived} />
196204
</Tabs>
197205
{renderListContent()}
206+
{renderList && (
207+
<Virtuoso
208+
data={chatRooms}
209+
overscan={1}
210+
itemContent={(_index, item) => renderItem(item)}
211+
style={{ scrollbarWidth: 'none' }}
212+
components={{
213+
Footer: renderLoadingState,
214+
}}
215+
endReached={() => {
216+
if (hasNext) {
217+
loadNext(5)
218+
}
219+
}}
220+
{...VirtuosoProps}
221+
/>
222+
)}
198223
</ChatRoomListContainer>
199224
)
200225
}

0 commit comments

Comments
 (0)