Skip to content

Commit cccb2cd

Browse files
committed
fix: chat rooms refetch issue
1 parent e671963 commit cccb2cd

File tree

4 files changed

+79
-39
lines changed

4 files changed

+79
-39
lines changed

packages/components/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @baseapp-frontend/components
22

3+
## 1.0.8
4+
5+
### Patch Changes
6+
7+
- Enabled deletion of chat room messages
8+
39
## 1.0.7
410

511
### Patch Changes

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
}

packages/components/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@baseapp-frontend/components",
33
"description": "BaseApp components modules such as comments, notifications, messages, and more.",
4-
"version": "1.0.7",
4+
"version": "1.0.8",
55
"sideEffects": false,
66
"scripts": {
77
"babel:bundle": "babel modules -d tmp-babel --extensions .ts,.tsx --ignore '**/__tests__/**','**/__storybook__/**'",

packages/components/schema.graphql

+14-5
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,20 @@ type ChatRoomCreatePayload {
154154
clientMutationId: String
155155
}
156156

157-
"""
158-
A Relay edge containing a `ChatRoom` and its cursor.
159-
"""
157+
input ChatRoomDeleteMessageInput {
158+
id: ID!
159+
clientMutationId: String
160+
}
161+
162+
type ChatRoomDeleteMessagePayload {
163+
"""May contain more than one error for same field."""
164+
errors: [ErrorType]
165+
_debug: DjangoDebug
166+
deletedMessage: MessageEdge
167+
clientMutationId: String
168+
}
169+
170+
"""A Relay edge containing a `ChatRoom` and its cursor."""
160171
type ChatRoomEdge {
161172
"""The item at the end of the edge"""
162173
node: ChatRoom
@@ -190,7 +201,6 @@ type ChatRoomOnMessagesCountUpdate {
190201
type ChatRoomOnRoomUpdate {
191202
room: ChatRoomEdge
192203
removedParticipants: [ChatRoomParticipant]
193-
addedParticipants: [ChatRoomParticipant]
194204
}
195205

196206
type ChatRoomParticipant implements Node {
@@ -314,7 +324,6 @@ type ChatRoomUpdatePayload {
314324
_debug: DjangoDebug
315325
room: ChatRoomEdge
316326
removedParticipants: [ChatRoomParticipant]
317-
addedParticipants: [ChatRoomParticipant]
318327
clientMutationId: String
319328
}
320329

0 commit comments

Comments
 (0)