@@ -31,28 +31,37 @@ const usePostMessageReaction = () => {
3131 // If the message ID matches, we need to update the reactions object
3232 const existingReactions = JSON . parse ( m . message_reactions ?? '{}' ) as Record < string , ReactionObject >
3333
34+ const emojiKey = is_custom ? ( emoji_name ?? emoji ) : emoji
35+
3436 // Check if the emoji is already in the reactions object
35- if ( existingReactions [ emoji ] ) {
37+ if ( existingReactions [ emojiKey ] ) {
38+ const hasCurrentUserReacted = existingReactions [ emojiKey ] . users . includes ( username )
3639 // If it is, check how many users have reacted to the message
37- const userCount = existingReactions [ emoji ] . count
38-
39- // If the user has already reacted, remove their reaction
40- if ( userCount > 1 ) {
41- existingReactions [ emoji ] . users = existingReactions [ emoji ] . users . filter ( u => u !== username )
42- existingReactions [ emoji ] . count = existingReactions [ emoji ] . count - 1
40+ const userCount = existingReactions [ emojiKey ] . count
41+
42+ if ( hasCurrentUserReacted ) {
43+ // Remove the user from the reaction
44+ if ( userCount === 1 ) {
45+ delete existingReactions [ emojiKey ]
46+ } else {
47+ existingReactions [ emojiKey ] . users = existingReactions [ emojiKey ] . users . filter ( u => u !== username )
48+ existingReactions [ emojiKey ] . count = existingReactions [ emojiKey ] . count - 1
49+ }
4350 } else {
44- // If there is only one user, remove the reaction
45- delete existingReactions [ emoji ]
51+ // If the user has not reacted, add them to the reaction
52+ existingReactions [ emojiKey ] . users . push ( username )
53+ existingReactions [ emojiKey ] . count = existingReactions [ emojiKey ] . count + 1
4654 }
4755 } else {
4856 // If it's not, add it
49- existingReactions [ emoji ] = {
57+ existingReactions [ emojiKey ] = {
5058 reaction : emoji ,
5159 users : [ username ] ,
5260 count : 1 ,
5361 emoji_name : emoji_name ?? ''
5462 }
5563 }
64+
5665 return {
5766 ...m ,
5867 message_reactions : JSON . stringify ( existingReactions )
0 commit comments