Skip to content

Commit ceeab46

Browse files
committed
fix: chat rooms refetch issue
1 parent 25c5d9d commit ceeab46

File tree

1 file changed

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

1 file changed

+58
-33
lines changed

packages/components/modules/messages/web/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 { LoadingState } from '@baseapp-frontend/design-system/components/web/displays'
614
import { Searchbar as DefaultSearchbar } from '@baseapp-frontend/design-system/components/web/inputs'
@@ -28,6 +36,7 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
2836
VirtuosoProps = {},
2937
}) => {
3038
const [tab, setTab] = useState<ChatTabValues>(CHAT_TAB_VALUES.active)
39+
const [renderList, setRenderList] = useState<boolean>(true)
3140

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

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

6681
const handleChange = (event: React.SyntheticEvent, newTab: string) => {
6782
setTab(newTab as ChatTabValues)
6883
startRefetchTransition(() => {
84+
setRenderList(false)
6985
refetch(
7086
{
7187
q: searchValue,
7288
unreadMessages: newTab === CHAT_TAB_VALUES.unread,
7389
archived: newTab === CHAT_TAB_VALUES.archived,
7490
},
75-
{ fetchPolicy: 'store-and-network' },
91+
{ fetchPolicy: 'network-only' },
7692
)
7793
})
7894
}
7995

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

128151
const renderListContent = () => {
129-
const emptyChatRoomsList = chatRooms.length === 0
130-
131-
if (!isPending && searchValue && emptyChatRoomsList) return <SearchNotFoundState />
152+
const hasEmptyStates = !isPending && chatRooms.length === 0
132153

133-
if (!isPending && emptyChatRoomsList) return <EmptyChatRoomsState />
154+
if (hasEmptyStates) {
155+
if (searchValue) return <SearchNotFoundState />
156+
return <EmptyChatRoomsState />
157+
}
134158

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

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

0 commit comments

Comments
 (0)