{"version":3,"file":"MessageReactions.mjs","names":[],"sources":["../../../../src/components/Reactions/MessageReactions.tsx"],"sourcesContent":["import React, {\n  type ComponentPropsWithoutRef,\n  type ComponentRef,\n  useMemo,\n  useRef,\n  useState,\n} from 'react';\nimport clsx from 'clsx';\n\nimport { MessageReactionsDetail as DefaultMessageReactionsDetail } from './MessageReactionsDetail';\nimport { useProcessReactions } from './hooks/useProcessReactions';\nimport type { MessageContextValue } from '../../context';\nimport {\n  useComponentContext,\n  useMessageContext,\n  useTranslationContext,\n} from '../../context';\n\nimport { MAX_MESSAGE_REACTIONS_TO_FETCH } from '../Message/hooks';\n\nimport type { ReactionGroupResponse, ReactionResponse } from 'stream-chat';\nimport type { ReactionsComparator, ReactionType } from './types';\nimport { DialogAnchor, useDialogIsOpen, useDialogOnNearestManager } from '../Dialog';\n\nexport type MessageReactionsProps = Partial<\n  Pick<MessageContextValue, 'handleFetchReactions' | 'reactionDetailsSort'>\n> & {\n  /** Own reactions, used to mark reaction types as own. Defaults to the message's own reactions. */\n  own_reactions?: ReactionResponse[];\n  /** Summary per reaction type. Defaults to the message's `reaction_groups`. */\n  reaction_groups?: Record<string, ReactionGroupResponse>;\n  /** Latest reactions, used for the reacting user names. Defaults to the message's latest ones. */\n  reactions?: ReactionResponse[];\n  /**\n   * Comparator that orders the reaction types. Defaults to `sortReactions` from `MessageContext`,\n   * then to ascending time of the first reaction.\n   */\n  sortReactions?: ReactionsComparator;\n  /** Aligns the list to the opposite side of the message. Defaults to false. */\n  flipHorizontalPosition?: boolean;\n  verticalPosition?: 'top' | 'bottom' | null;\n  visualStyle?: 'clustered' | 'segmented' | null;\n  capLimit?: {\n    [key in Extract<MessageReactionsProps['visualStyle'], string>]?: number;\n  };\n};\n\n// Renders a button when `buttonIf` is true, otherwise a fragment that drops all props but children.\nconst FragmentOrButton = ({\n  buttonIf: renderButton = false,\n  children,\n  type = 'button',\n  ...props\n}: ComponentPropsWithoutRef<'button'> & { buttonIf?: boolean }) => {\n  if (renderButton) {\n    return (\n      <button {...props} type={type}>\n        {children}\n      </button>\n    );\n  }\n\n  return <>{children}</>;\n};\n\nconst UnMemoizedMessageReactions = (props: MessageReactionsProps) => {\n  const {\n    capLimit: { clustered: capLimitClustered = 5, segmented: capLimitSegmented = 4 } = {},\n    flipHorizontalPosition = false,\n    handleFetchReactions,\n    reactionDetailsSort,\n    verticalPosition = 'top',\n    visualStyle = 'clustered',\n    ...rest\n  } = props;\n\n  const {\n    existingReactions,\n    hasReactions,\n    reactionGroups,\n    totalReactionCount,\n    uniqueReactionTypeCount,\n  } = useProcessReactions(rest);\n  const [selectedReactionType, setSelectedReactionType] = useState<ReactionType | null>(\n    null,\n  );\n  const { t } = useTranslationContext();\n  const { MessageReactionsDetail = DefaultMessageReactionsDetail } =\n    useComponentContext();\n  const { isMyMessage, message } = useMessageContext();\n\n  const divRef = useRef<ComponentRef<'div'>>(null);\n  const dialogId = DefaultMessageReactionsDetail.getDialogId({\n    messageId: message.id,\n  });\n  const { dialog, dialogManager } = useDialogOnNearestManager({ id: dialogId });\n  const isDialogOpen = useDialogIsOpen(dialogId, dialogManager?.id);\n\n  const handleReactionButtonClick = (reactionType: ReactionType | null) => {\n    if (totalReactionCount > MAX_MESSAGE_REACTIONS_TO_FETCH) {\n      return;\n    }\n\n    setSelectedReactionType(reactionType);\n\n    dialog.open();\n  };\n\n  // Segmented at the top caps the list and shows a count of the rest. Clustered caps it and shows\n  // the total count instead. Segmented elsewhere is uncapped, so this is null.\n  const cappedExistingReactions = useMemo(() => {\n    if (visualStyle === 'segmented' && verticalPosition !== 'top') return null;\n\n    const capLimit = visualStyle === 'segmented' ? capLimitSegmented : capLimitClustered;\n\n    const sliced = existingReactions.slice(0, capLimit);\n\n    return {\n      // Sum of the capped types' counts; the overflow count is the total minus this.\n      reactionCountToDisplay: sliced.reduce(\n        (accumulatedCount, { reactionCount }) => accumulatedCount + reactionCount,\n        0,\n      ),\n      reactionsToDisplay: sliced,\n    } as const;\n  }, [\n    capLimitClustered,\n    capLimitSegmented,\n    existingReactions,\n    verticalPosition,\n    visualStyle,\n  ]);\n\n  if (!hasReactions) return null;\n\n  return (\n    <>\n      <div\n        aria-label={t(\n          'reactions.messageReactions.reactionList.ariaLabel',\n          'Reaction list',\n        )}\n        className={clsx('str-chat__message-reactions', {\n          [`str-chat__message-reactions--flipped-horizontally`]: flipHorizontalPosition,\n          [`str-chat__message-reactions--${verticalPosition}`]:\n            typeof verticalPosition === 'string',\n          [`str-chat__message-reactions--${visualStyle}`]:\n            typeof visualStyle === 'string',\n        })}\n        ref={divRef}\n        role='figure'\n      >\n        <FragmentOrButton\n          aria-expanded={isDialogOpen}\n          aria-label={t(\n            'reactions.messageReactions.reactionList.ariaLabel',\n            'Reaction list',\n          )}\n          aria-pressed={isDialogOpen}\n          buttonIf={visualStyle === 'clustered'}\n          className='str-chat__message-reactions__list-button'\n          data-testid='message-reactions-list-button'\n          onClick={() => handleReactionButtonClick(null)}\n        >\n          <ul className='str-chat__message-reactions__list'>\n            {(cappedExistingReactions?.reactionsToDisplay ?? existingReactions).map(\n              ({ EmojiComponent, reactionCount, reactionType }) =>\n                EmojiComponent && (\n                  <li\n                    className='str-chat__message-reactions__list-item'\n                    data-testid='message-reactions-list-item'\n                    key={reactionType}\n                  >\n                    <FragmentOrButton\n                      aria-label={t(\n                        'reactions.messageReactions.selectReaction.ariaLabel',\n                        'Select Reaction: {{ reactionName }}',\n                        {\n                          reactionName: reactionType,\n                        },\n                      )}\n                      buttonIf={visualStyle === 'segmented'}\n                      className='str-chat__message-reactions__list-item-button'\n                      onClick={() => handleReactionButtonClick(reactionType)}\n                    >\n                      <span\n                        className='str-chat__message-reactions__list-item-icon'\n                        data-testid='message-reactions-list-item-icon'\n                      >\n                        <EmojiComponent />\n                      </span>\n                      {visualStyle === 'segmented' && reactionCount > 1 && (\n                        <span\n                          className='str-chat__message-reactions__list-item-count'\n                          data-testclass='message-reactions-item-count'\n                        >\n                          {reactionCount}\n                        </span>\n                      )}\n                    </FragmentOrButton>\n                  </li>\n                ),\n            )}\n            {uniqueReactionTypeCount > 4 &&\n              cappedExistingReactions &&\n              visualStyle === 'segmented' && (\n                <li className='str-chat__message-reactions__list-item str-chat__message-reactions__list-item--more'>\n                  <button\n                    aria-label={t(\n                      'reactions.messageReactions.reactionList.ariaLabel',\n                      'Reaction list',\n                    )}\n                    className='str-chat__message-reactions__list-item-button'\n                    onClick={() => handleReactionButtonClick(null)}\n                    type='button'\n                  >\n                    <span className='str-chat__message-reactions__overflow-count'>\n                      +\n                      {totalReactionCount -\n                        cappedExistingReactions.reactionCountToDisplay}\n                    </span>\n                  </button>\n                </li>\n              )}\n          </ul>\n          {visualStyle === 'clustered' && (\n            <span\n              className='str-chat__message-reactions__total-count'\n              data-testid='message-reactions-total-count'\n            >\n              {totalReactionCount}\n            </span>\n          )}\n        </FragmentOrButton>\n      </div>\n\n      <DialogAnchor\n        dialogManagerId={dialogManager?.id}\n        id={dialogId}\n        offset={8}\n        placement={isMyMessage() ? 'bottom-end' : 'bottom-start'}\n        referenceElement={divRef.current}\n        trapFocus\n        updatePositionOnContentResize\n      >\n        <MessageReactionsDetail\n          handleFetchReactions={handleFetchReactions}\n          onSelectedReactionTypeChange={setSelectedReactionType}\n          reactionDetailsSort={reactionDetailsSort}\n          reactionGroups={reactionGroups}\n          reactions={existingReactions}\n          selectedReactionType={selectedReactionType}\n          totalReactionCount={totalReactionCount}\n        />\n      </DialogAnchor>\n    </>\n  );\n};\n\n/** Displays the list of reactions on a message. */\nexport const MessageReactions = React.memo(\n  UnMemoizedMessageReactions,\n) as typeof UnMemoizedMessageReactions;\n"],"mappings":";;;;;;;;;;;;AAgDA,IAAM,oBAAoB,EACxB,UAAU,eAAe,OACzB,UACA,OAAO,UACP,GAAG,YAC8D;CACjE,IAAI,cACF,OACE,oBAAC,UAAD;EAAQ,GAAI;EAAa;EACtB;CACK,CAAA;CAIZ,OAAO,oBAAA,YAAA,EAAG,SAAW,CAAA;AACvB;AAEA,IAAM,8BA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