diff --git a/.changeset/tricky-tomatoes-sit.md b/.changeset/tricky-tomatoes-sit.md new file mode 100644 index 00000000000..5abba73ae52 --- /dev/null +++ b/.changeset/tricky-tomatoes-sit.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/meteor': minor +--- + +fix: Message sent triggering thread subscriptions multiple times 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 9a4ff6f2490..51f424697a8 100644 --- a/apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMainMessageQuery.ts +++ b/apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMainMessageQuery.ts @@ -82,8 +82,9 @@ export const useThreadMainMessageQuery = ( useEffect(() => { return () => { unsubscribeRef.current?.(); + unsubscribeRef.current = undefined; }; - }, []); + }, [tmid]); return useQuery( ['rooms', room._id, 'threads', tmid, 'main-message'] as const, @@ -96,17 +97,17 @@ export const useThreadMainMessageQuery = ( throw new Error('Invalid main message'); } - unsubscribeRef.current?.(); - - unsubscribeRef.current = subscribeToMessage(mainMessage, { - onMutate: () => { - queryClient.invalidateQueries(queryKey, { exact: true }); - }, - onDelete: () => { - onDelete?.(); - queryClient.invalidateQueries(queryKey, { exact: true }); - }, - }); + unsubscribeRef.current = + unsubscribeRef.current || + subscribeToMessage(mainMessage, { + onMutate: () => { + queryClient.invalidateQueries(queryKey, { exact: true }); + }, + onDelete: () => { + onDelete?.(); + queryClient.invalidateQueries(queryKey, { exact: true }); + }, + }); return mainMessage; },