Skip to content

Commit dcb9d18

Browse files
authored
fix(web): logic for optimistic updates of reactions (#1528)
1 parent 6471cf4 commit dcb9d18

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

frontend/src/hooks/usePostMessageReaction.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

raven/api/reactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def calculate_message_reaction(message_id, channel_id: str = None):
9393
"Raven Message",
9494
message_id,
9595
"message_reactions",
96-
json.dumps(total_reactions),
96+
json.dumps(total_reactions, indent=4),
9797
update_modified=False,
9898
)
9999
frappe.publish_realtime(

0 commit comments

Comments
 (0)