Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat: make emojis optimistically update (#1104)

gbirmanb1e897641c63
diff --git a/js/app/packages/block-channel/signal/reactions.ts b/js/app/packages/block-channel/signal/reactions.ts
index 37cc223f58..f4bb026106 100644
--- a/js/app/packages/block-channel/signal/reactions.ts
+++ b/js/app/packages/block-channel/signal/reactions.ts
@@ -24,6 +24,7 @@ export async function reactToMessage(emoji: string, messageId: string) {
if (!channelId || !userId) return;
const messageToReaction = messageToReactionStore.get;
+ const setMessageToReaction = messageToReactionStore.set;
let action: 'Add' | 'Remove' = 'Add';
let prev = messageToReaction?.[messageId] ?? [];
@@ -36,6 +37,47 @@ export async function reactToMessage(emoji: string, messageId: string) {
action = 'Remove';
}
+ // Optimistically update the UI
+ const optimisticReactions = prev.slice();
+ const existingReactionIndex = optimisticReactions.findIndex(
+ (r) => r.emoji === emoji
+ );
+
+ if (action === 'Add') {
+ if (existingReactionIndex >= 0) {
+ // Add user to existing reaction
+ optimisticReactions[existingReactionIndex] = {
+ ...optimisticReactions[existingReactionIndex],
+ users: [...optimisticReactions[existingReactionIndex].users, userId],
+ };
+ } else {
+ // Create new reaction
+ optimisticReactions.push({
+ emoji,
+ users: [userId],
+ });
+ }
+ } else {
+ // Remove user from reaction
+ if (existingReactionIndex >= 0) {
+ const updatedUsers = optimisticReactions[
+ existingReactionIndex
+ ].users.filter((id) => id !== userId);
+
+ if (updatedUsers.length === 0) {
+ // Remove reaction entirely if no users left
+ optimisticReactions.splice(existingReactionIndex, 1);
+ } else {
+ optimisticReactions[existingReactionIndex] = {
+ ...optimisticReactions[existingReactionIndex],
+ users: updatedUsers,
+ };
+ }
+ }
+ }
+
+ setMessageToReaction(messageId, optimisticReactions);
+
try {
await commsServiceClient.postReaction({
channel_id: channelId,
@@ -45,6 +87,8 @@ export async function reactToMessage(emoji: string, messageId: string) {
});
} catch (e) {
console.error(e);
+ // Revert optimistic update on error
+ setMessageToReaction(messageId, prev);
}
track(TrackingEvents.BLOCKCHANNEL.MESSAGE.REACTION, {