fix(inbox): guard realtime cache updates during reload
This commit is contained in:
@@ -94,7 +94,7 @@ function updateRealtimeConversationList(
|
||||
conversationId: string,
|
||||
patch: Partial<ConversationListItem>,
|
||||
) {
|
||||
if (!current) return current
|
||||
if (!current || !Array.isArray(current.list)) return current
|
||||
let matched = false
|
||||
const list = current.list.map((item) => {
|
||||
if (item.id !== conversationId) return item
|
||||
@@ -255,10 +255,14 @@ export function InboxPage() {
|
||||
queryClient.setQueryData(['conversation', convId], (current: any) => current
|
||||
? { ...current, conversation: { ...current.conversation, ...conversation } }
|
||||
: current)
|
||||
queryClient.setQueriesData<any>({ queryKey: ['conversations'] }, (current: any) => current ? {
|
||||
...current,
|
||||
list: current.list.map((item: any) => item.id === convId ? { ...item, unreadCount: 0 } : item),
|
||||
} : current)
|
||||
queryClient.setQueriesData<any>({ queryKey: ['conversations'] }, (current: any) => (
|
||||
current && Array.isArray(current.list)
|
||||
? {
|
||||
...current,
|
||||
list: current.list.map((item: any) => item.id === convId ? { ...item, unreadCount: 0 } : item),
|
||||
}
|
||||
: current
|
||||
))
|
||||
})
|
||||
.catch(() => undefined)
|
||||
}, [convId, lastMessageId, queryClient])
|
||||
@@ -305,7 +309,7 @@ export function InboxPage() {
|
||||
queryClient.removeQueries({ queryKey: ['messages', event.conversationId] })
|
||||
queryClient.setQueriesData<{ list: ConversationListItem[]; total: number }>(
|
||||
{ queryKey: ['conversations'] },
|
||||
(current) => current
|
||||
(current) => current && Array.isArray(current.list)
|
||||
? {
|
||||
...current,
|
||||
list: current.list.filter((item) => item.id !== event.conversationId),
|
||||
@@ -326,7 +330,8 @@ export function InboxPage() {
|
||||
)
|
||||
const conversationWasVisible = Boolean(
|
||||
event.conversationId
|
||||
&& activeConversationCache?.list.some((item) => item.id === event.conversationId),
|
||||
&& Array.isArray(activeConversationCache?.list)
|
||||
&& activeConversationCache.list.some((item) => item.id === event.conversationId),
|
||||
)
|
||||
if (
|
||||
['message.created', 'message.received'].includes(event.type || '') &&
|
||||
@@ -356,7 +361,9 @@ export function InboxPage() {
|
||||
{ queryKey: ['conversations'] },
|
||||
(current) => {
|
||||
const typed = current as { list: ConversationListItem[]; total: number } | undefined
|
||||
const item = typed?.list.find((entry) => entry.id === event.conversationId)
|
||||
const item = Array.isArray(typed?.list)
|
||||
? typed.list.find((entry) => entry.id === event.conversationId)
|
||||
: undefined
|
||||
const isNewMessage = ['message.created', 'message.received'].includes(event.type || '')
|
||||
const isCurrentLastMessage =
|
||||
String(item?.lastMessage?.id || '') === String(event.message?.id || '')
|
||||
|
||||
Reference in New Issue
Block a user