@@ -2,10 +2,7 @@ import type { MessageResponse } from 'stream-chat';
22
33import type { ChannelContextValue } from '../../../contexts/channelContext/ChannelContext' ;
44import type { ChatContextValue } from '../../../contexts/chatContext/ChatContext' ;
5- import type {
6- MessageContextValue ,
7- Reactions ,
8- } from '../../../contexts/messageContext/MessageContext' ;
5+ import type { MessageContextValue } from '../../../contexts/messageContext/MessageContext' ;
96import type { MessagesContextValue } from '../../../contexts/messagesContext/MessagesContext' ;
107
118import type { DefaultStreamChatGenerics } from '../../../types/types' ;
@@ -22,7 +19,6 @@ export const useMessageActionHandlers = <
2219 sendReaction,
2320 setEditingState,
2421 setQuotedMessageState,
25- supportedReactions,
2622} : Pick <
2723 MessagesContextValue < StreamChatGenerics > ,
2824 | 'sendReaction'
@@ -89,41 +85,26 @@ export const useMessageActionHandlers = <
8985 setEditingState ( message ) ;
9086 } ;
9187
92- const clientId = client . userID ;
93- const isMessageTypeDeleted = message . type === 'deleted' ;
94-
95- const hasReactions =
96- ! isMessageTypeDeleted && ! ! message . latest_reactions && message . latest_reactions . length > 0 ;
97-
98- const reactions = hasReactions
99- ? supportedReactions . reduce ( ( acc , cur ) => {
100- const reactionType = cur . type ;
101- const reactionsOfReactionType = message . latest_reactions ?. filter (
102- ( reaction ) => reaction . type === reactionType ,
103- ) ;
104-
105- if ( reactionsOfReactionType ?. length ) {
106- const hasOwnReaction = reactionsOfReactionType . some (
107- ( reaction ) => reaction . user_id === clientId ,
108- ) ;
109- acc . push ( { own : hasOwnReaction , type : reactionType } ) ;
110- }
111-
112- return acc ;
113- } , [ ] as Reactions )
114- : [ ] ;
115-
11688 const handleToggleReaction = async ( reactionType : string ) => {
11789 const messageId = message . id ;
118- const ownReaction = ! ! reactions . find (
119- ( reaction ) => reaction . own && reaction . type === reactionType ,
120- ) ;
121-
90+ const own_reactions = message . own_reactions ?? [ ] ;
91+ const userExistingReaction = own_reactions . find ( ( reaction ) => {
92+ // own user should only ever contain the current user id
93+ // just in case we check to prevent bugs with message updates from breaking reactions
94+ if ( reaction . user && client . userID === reaction . user . id && reaction . type === reactionType ) {
95+ return true ;
96+ } else if ( reaction . user && client . userID !== reaction . user . id ) {
97+ console . warn (
98+ `message.own_reactions contained reactions from a different user, this indicates a bug` ,
99+ ) ;
100+ }
101+ return false ;
102+ } ) ;
122103 // Change reaction in local state, make API call in background, revert to old message if fails
123104 try {
124105 if ( channel && messageId ) {
125- if ( ownReaction ) {
126- await deleteReaction ( reactionType , messageId ) ;
106+ if ( userExistingReaction ) {
107+ await deleteReaction ( userExistingReaction . type , messageId ) ;
127108 } else {
128109 await sendReaction ( reactionType , messageId ) ;
129110 }
0 commit comments