{"version":3,"file":"MessageRepliesCountButton.mjs","names":[],"sources":["../../../../src/components/Message/MessageRepliesCountButton.tsx"],"sourcesContent":["import type { MouseEventHandler } from 'react';\nimport React, { useCallback, useContext, useMemo } from 'react';\nimport type { UserResponse } from 'stream-chat';\n\nimport { useTranslationContext } from '../../context/TranslationContext';\nimport {\n  MessageContext,\n  useChannel,\n  useComponentContext,\n  useWorkspaceNavigation,\n} from '../../context';\nimport { useStateStore } from '../../store';\nimport { AvatarStack as DefaultAvatarStack } from '../Avatar';\nimport { extractDisplayInfo as defaultExtractDisplayInfo } from '../Avatar/utils';\n\nexport type MessageRepliesCountButtonProps = {\n  /** Replaces the default text after the count when there is more than one reply. */\n  labelPlural?: string;\n  /** Replaces the default text after the count when there is one reply. */\n  labelSingle?: string;\n  /** Replaces the default open-thread behavior. */\n  onClick?: MouseEventHandler;\n  /** Fallback when the channel's message paginator has no reply count for the message. */\n  reply_count?: number;\n  thread_participants?: UserResponse[];\n};\n\nfunction UnMemoizedMessageRepliesCountButton(props: MessageRepliesCountButtonProps) {\n  const {\n    AvatarStack = DefaultAvatarStack,\n    extractDisplayInfo = defaultExtractDisplayInfo,\n  } = useComponentContext();\n  const {\n    labelPlural,\n    labelSingle,\n    onClick,\n    reply_count: replyCountFromProps = 0,\n    thread_participants: threadParticipantsFromProps = [],\n  } = props;\n  // Also renders outside a message, from props alone.\n  const { handleOpenThread, message: contextMessage } = useContext(MessageContext) ?? {};\n  const channel = useChannel();\n  const { openThread } = useWorkspaceNavigation();\n  const replyMetadataSelector = useMemo(\n    () => () => {\n      const targetMessage = contextMessage?.id\n        ? channel.messagePaginator.getItem(contextMessage.id)\n        : undefined;\n\n      return {\n        replyCountFromPaginator: targetMessage?.reply_count,\n        threadParticipantsFromPaginator: targetMessage?.thread_participants,\n      };\n    },\n    [channel.messagePaginator, contextMessage?.id],\n  );\n  const { replyCountFromPaginator, threadParticipantsFromPaginator } =\n    useStateStore(channel.messagePaginator.state, replyMetadataSelector) ?? {};\n  const replyCount = replyCountFromPaginator ?? replyCountFromProps;\n  const threadParticipants =\n    threadParticipantsFromPaginator ?? threadParticipantsFromProps;\n\n  const { t } = useTranslationContext();\n\n  const avatarStackDisplayInfo = useMemo(\n    () => threadParticipants.slice(0, 3).map((user) => extractDisplayInfo({ user })),\n    [extractDisplayInfo, threadParticipants],\n  );\n\n  const handleClick = useCallback<MouseEventHandler>(\n    (event) => {\n      if (onClick) {\n        onClick(event);\n        return;\n      }\n\n      if (!contextMessage) return;\n      // The message's own handler honors a `Message.openThread` override before falling back to\n      // workspace navigation.\n      if (handleOpenThread) {\n        handleOpenThread(event);\n        return;\n      }\n      void openThread({ channel, message: contextMessage });\n    },\n    [channel, contextMessage, handleOpenThread, onClick, openThread],\n  );\n\n  if (!replyCount) return null;\n\n  let replyCountText = t('common.replyCount.label', {\n    count: replyCount,\n    defaultValue_one: '1 reply',\n    defaultValue_other: '{{ count }} replies',\n  });\n\n  if (labelPlural && replyCount > 1) {\n    replyCountText = `${replyCount} ${labelPlural}`;\n  } else if (labelSingle && replyCount === 1) {\n    replyCountText = `1 ${labelSingle}`;\n  }\n\n  return (\n    <div className='str-chat__message-replies-count-button-wrapper'>\n      <button\n        className='str-chat__message-replies-count-button'\n        data-testid='replies-count-button'\n        onClick={handleClick}\n      >\n        {replyCountText}\n\n        <AvatarStack displayInfo={avatarStackDisplayInfo} size='xs' />\n      </button>\n    </div>\n  );\n}\n\nexport const MessageRepliesCountButton = React.memo(\n  UnMemoizedMessageRepliesCountButton,\n) as typeof UnMemoizedMessageRepliesCountButton;\n"],"mappings":";;;;;;;;;;;AA2BA,SAAS,oCAAoC,OAAuC;CAClF,MAAM,EACJ,aAAA,gBAAc,aACd,oBAAA,uBAAqB,uBACnB,oBAAoB;CACxB,MAAM,EACJ,aACA,aACA,SACA,aAAa,sBAAsB,GACnC,qBAAqB,8BAA8B,CAAC,MAClD;CAEJ,MAAM,EAAE,kBAAkB,SAAS,mBAAmB,WAAW,cAAc,KAAK,CAAC;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