Skip to content

Commit a96ed0f

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

File tree

1 file changed

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

1 file changed

+55
-33
lines changed

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

+55-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,50 @@ 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+
useEffect(() => {
98+
setRenderList(true)
99+
}, [data])
100+
81101
const { id: selectedRoom, setChatRoom } = useChatRoom()
82102
const chatRooms = useMemo(
83103
() => data?.chatRooms?.edges?.filter((edge) => edge?.node).map((edge) => edge?.node) || [],
@@ -127,29 +147,14 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
127147
)
128148

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

134-
if (!isPending && emptyChatRoomsList) return <EmptyChatRoomsState />
152+
if (hasEmptyStates) {
153+
if (searchValue) return <SearchNotFoundState />
154+
return <EmptyChatRoomsState />
155+
}
135156

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-
)
157+
return undefined
153158
}
154159

155160
return (
@@ -195,6 +200,23 @@ const ChatRoomsList: FC<ChatRoomsListProps> = ({
195200
<Tab label={renderTabLabel(CHAT_TAB_VALUES.archived)} value={CHAT_TAB_VALUES.archived} />
196201
</Tabs>
197202
{renderListContent()}
203+
{renderList && (
204+
<Virtuoso
205+
data={chatRooms}
206+
overscan={1}
207+
itemContent={(_index, item) => renderItem(item)}
208+
style={{ scrollbarWidth: 'none' }}
209+
components={{
210+
Footer: renderLoadingState,
211+
}}
212+
endReached={() => {
213+
if (hasNext) {
214+
loadNext(5)
215+
}
216+
}}
217+
{...VirtuosoProps}
218+
/>
219+
)}
198220
</ChatRoomListContainer>
199221
)
200222
}

0 commit comments

Comments
 (0)