From 511fb3a94c69b8087f6c64e4925819958bc76171 Mon Sep 17 00:00:00 2001
From: fly_mc <me@flymc.cc>
Date: Sun, 10 Nov 2024 22:15:31 +0800
Subject: [PATCH] tweak merge reactions

---
 .../src/components/MkReactionsViewer.vue      | 59 +++++++++----------
 1 file changed, 27 insertions(+), 32 deletions(-)

diff --git a/packages/frontend/src/components/MkReactionsViewer.vue b/packages/frontend/src/components/MkReactionsViewer.vue
index 0fee9613d..1c3e3b5d3 100644
--- a/packages/frontend/src/components/MkReactionsViewer.vue
+++ b/packages/frontend/src/components/MkReactionsViewer.vue
@@ -25,26 +25,28 @@ import { defaultStore } from '@/store.js';
 import { customEmojisMap } from '@/custom-emojis.js';
 
 const localEmojiSet = new Set(Array.from(customEmojisMap.keys()));
-const emojiCache = new Map<string, boolean>();
+const emojiCache = new Map<string, { hasNative: boolean; base: string }>();
 
-function hasLocalEmoji(reaction: string): boolean {
-  if (emojiCache.has(reaction)) return emojiCache.get(reaction)!;
-  
-  let result: boolean;
-  if (!reaction.includes(':')) {
-    result = true;
-  } else {
-    const emojiName = reaction.split('@')[0].split(':')[1];
-    result = localEmojiSet.has(emojiName);
+function getReactionInfo(reaction: string) {
+  if (emojiCache.has(reaction)) {
+    return emojiCache.get(reaction)!;
   }
-  
-  emojiCache.set(reaction, result);
-  return result;
-}
 
-function getBaseReaction(reaction: string): string {
-  if (!reaction.includes(':')) return reaction;
-  return `:${reaction.split('@')[0].split(':')[1]}:`;
+  let hasNative: boolean;
+  let base: string;
+
+  if (!reaction.includes(':')) {
+    hasNative = true;
+    base = reaction;
+  } else {
+    const baseName = reaction.split('@')[0].split(':')[1];
+    hasNative = localEmojiSet.has(baseName);
+    base = hasNative ? `:${baseName}:` : reaction;
+  }
+
+  const info = { hasNative, base };
+  emojiCache.set(reaction, info);
+  return info;
 }
 
 const props = withDefaults(defineProps<{
@@ -67,22 +69,15 @@ const hasMoreReactions = ref(false);
 
 const mergedReactions = computed(() => {
   const reactionMap = new Map();
-  
+
   reactions.value.forEach(([reaction, count]) => {
-    if (!hasLocalEmoji(reaction)) {
-      if (reactionMap.has(reaction)) {
-        reactionMap.set(reaction, reactionMap.get(reaction) + count);
-      } else {
-        reactionMap.set(reaction, count);
-      }
-      return;
-    }
-    
-    const baseReaction = getBaseReaction(reaction);
-    if (reactionMap.has(baseReaction)) {
-      reactionMap.set(baseReaction, reactionMap.get(baseReaction) + count);
+    const info = getReactionInfo(reaction);
+    const key = info.base;
+
+    if (reactionMap.has(key)) {
+      reactionMap.set(key, reactionMap.get(key) + count);
     } else {
-      reactionMap.set(baseReaction, count);
+      reactionMap.set(key, count);
     }
   });
 
@@ -95,7 +90,7 @@ if (props.note.myReaction && !Object.keys(reactions.value).includes(props.note.m
 
 onBeforeMount(() => {
   Object.keys(props.note.reactions).forEach(reaction => {
-    hasLocalEmoji(reaction);
+    getReactionInfo(reaction);
   });
 });