{"version":3,"file":"useReactionHandler.mjs","names":[],"sources":["../../../../../src/components/Message/hooks/useReactionHandler.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo } from 'react';\nimport throttle from 'lodash.throttle';\n\nimport { useChannel, useChatContext, useComponentContext } from '../../../context';\nimport {\n  defaultReactionOptions,\n  getEmojiCodeByReactionType,\n} from '../../Reactions/reactionOptions';\nimport { useThreadContext } from '../../Threads';\n\nimport type { LocalMessage, ReactionResponse } from 'stream-chat';\n\nexport const reactionHandlerWarning = `Reaction handler was called, but it is missing one of its required arguments.\nMake sure the hook is initialized with a valid message.`;\n\ntype ToggleReactionParams = {\n  add: boolean;\n  emojiCode?: string;\n  messageId: string;\n  type: string;\n};\n\nexport const useReactionHandler = (message?: LocalMessage) => {\n  const channel = useChannel();\n  const thread = useThreadContext();\n  const { client } = useChatContext();\n  const { reactionOptions = defaultReactionOptions } = useComponentContext();\n\n  const { messageOperations } = thread ?? channel;\n\n  // One throttle per message and reaction type, kept per `messageOperations` so it holds across the\n  // re-render each optimistic update causes. Repeated toggles of one reaction are throttled; a\n  // different reaction is not held back by them.\n  const toggleReaction = useMemo(() => {\n    const send = async ({ add, emojiCode, messageId, type }: ToggleReactionParams) => {\n      try {\n        if (add) {\n          await messageOperations.addReaction({\n            messageId,\n            // The native emoji, sent as `emoji_code` so push notifications in mobile SDKs can render it.\n            reaction: { type, ...(emojiCode && { emoji_code: emojiCode }) },\n          });\n        } else {\n          await messageOperations.deleteReaction({ messageId, type });\n        }\n      } catch {\n        // A failed reaction shows no error: `messageOperations` has already rolled back the\n        // optimistic change, unless the request is queued for replay.\n      }\n    };\n    const throttledByReaction = new Map<\n      string,\n      (params: ToggleReactionParams) => Promise<void> | undefined\n    >();\n\n    return (params: ToggleReactionParams) => {\n      const key = `${params.messageId}:${params.type}`;\n      let throttled = throttledByReaction.get(key);\n      if (!throttled) {\n        throttled = throttle(send, 1000);\n        throttledByReaction.set(key, throttled);\n      }\n      return throttled(params);\n    };\n  }, [messageOperations]);\n\n  return async (reactionType: string, event?: React.BaseSyntheticEvent) => {\n    if (event?.preventDefault) {\n      event.preventDefault();\n    }\n\n    if (!message) {\n      return console.warn(reactionHandlerWarning);\n    }\n\n    let userExistingReaction = null as unknown as ReactionResponse;\n\n    if (message.own_reactions) {\n      message.own_reactions.forEach((reaction) => {\n        // `own_reactions` should only hold the current user's reactions; the check guards\n        // against a bad message update breaking reactions.\n        if (\n          reaction.user &&\n          client.userID === reaction.user.id &&\n          reaction.type === reactionType\n        ) {\n          userExistingReaction = reaction;\n        } else if (reaction.user && client.userID !== reaction.user.id) {\n          console.warn(\n            `message.own_reactions contained reactions from a different user, this indicates a bug`,\n          );\n        }\n      });\n    }\n\n    try {\n      if (userExistingReaction) {\n        await toggleReaction({\n          add: false,\n          messageId: message.id,\n          type: userExistingReaction.type,\n        });\n      } else {\n        await toggleReaction({\n          add: true,\n          emojiCode: getEmojiCodeByReactionType(reactionOptions, reactionType),\n          messageId: message.id,\n          type: reactionType,\n        });\n      }\n    } catch (error) {\n      console.log({ error });\n    }\n  };\n};\n"],"mappings":";;;;;;;;AAaA,IAAa,yBAAyB;;AAUtC,IAAa,sBAAsB,YAA2B;CAC5D,MAAM,UAAU,WAAW;CAC3B,MAAM,SAAS,iBAAiB;CAChC,MAAM,EAAE,WAAW,eAAe;CAClC,MAAM,EAAE,kBAAkB,2BAA2B,oBAAoB;CAEzE,MAAM,EAAE,sBAAsB,UAAU;CAKxC,MAAM,iBAAiB,cAAc;EACnC,MAAM,OAAO,OAAO,EAAE,KAAK,WAAW,WAAW,WAAiC;GAChF,IAAI;IACF,IAAI,KACF,MAAM,kBAAkB,YAAY;KAClC;KAEA,UAAU;MAAE;MAAM,GAAI,aAAa,EAAE,YAAY,UAAU;KAAG;IAChE,CAAC;SAED,MAAM,kBAAkB,eAAe;KAAE;KAAW;IAAK,CAAC;GAE9D,QAAQ,CAGR;EACF;EACA,MAAM,sCAAsB,IAAI,IAG9B;EAEF,QAAQ,WAAiC;GACvC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