From e8eeb2a79df8cb06ea9419dfa011a1c96b87744d Mon Sep 17 00:00:00 2001 From: gabriellsh <40830821+gabriellsh@users.noreply.github.com> Date: Thu, 12 Oct 2023 12:12:12 -0300 Subject: [PATCH] fix: Threads breaking after sending messages too fast (#30622) Co-authored-by: Yash Rajpal <58601732+yash-rajpal@users.noreply.github.com> --- .changeset/cool-zoos-move.md | 5 +++++ .../Threads/hooks/useThreadMainMessageQuery.ts | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 .changeset/cool-zoos-move.md diff --git a/.changeset/cool-zoos-move.md b/.changeset/cool-zoos-move.md new file mode 100644 index 000000000000..dda6fbe2b02e --- /dev/null +++ b/.changeset/cool-zoos-move.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +fixed threads breaking when sending messages too fast diff --git a/apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMainMessageQuery.ts b/apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMainMessageQuery.ts index 8b3bef03f793..aca714549cf1 100644 --- a/apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMainMessageQuery.ts +++ b/apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMainMessageQuery.ts @@ -1,13 +1,12 @@ -import { isThreadMainMessage } from '@rocket.chat/core-typings'; import type { IMessage, IThreadMainMessage } from '@rocket.chat/core-typings'; import { useStream } from '@rocket.chat/ui-contexts'; import type { UseQueryResult } from '@tanstack/react-query'; import { useQueryClient, useQuery } from '@tanstack/react-query'; import { useCallback, useEffect, useRef } from 'react'; +import { withDebouncing } from '../../../../../../lib/utils/highOrderFunctions'; import type { FieldExpression, Query } from '../../../../../lib/minimongo'; import { createFilterFromQuery } from '../../../../../lib/minimongo'; -import { onClientMessageReceived } from '../../../../../lib/onClientMessageReceived'; import { useRoom } from '../../../contexts/RoomContext'; import { useGetMessageByID } from './useGetMessageByID'; @@ -87,19 +86,22 @@ export const useThreadMainMessageQuery = ( }, [tmid]); return useQuery(['rooms', room._id, 'threads', tmid, 'main-message'] as const, async ({ queryKey }) => { - const message = await getMessage(tmid); + const mainMessage = await getMessage(tmid); - const mainMessage = (await onClientMessageReceived(message)) || message; - - if (!mainMessage && !isThreadMainMessage(mainMessage)) { + if (!mainMessage) { throw new Error('Invalid main message'); } + const debouncedInvalidate = withDebouncing({ wait: 10000 })(() => { + queryClient.invalidateQueries(queryKey, { exact: true }); + }); + unsubscribeRef.current = unsubscribeRef.current || subscribeToMessage(mainMessage, { - onMutate: () => { - queryClient.invalidateQueries(queryKey, { exact: true }); + onMutate: (message) => { + queryClient.setQueryData(queryKey, () => message); + debouncedInvalidate(); }, onDelete: () => { onDelete?.();