{"version":3,"file":"MessageText.mjs","names":[],"sources":["../../../../src/components/Message/MessageText.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport React, { useMemo } from 'react';\nimport { messageHasAttachments, messageTextHasEmojisOnly } from './utils';\nimport { useStableId } from '../UtilityComponents/useStableId';\n\nimport type { MessageContextValue } from '../../context';\nimport { useMessageContext, useTranslationContext } from '../../context';\nimport { VisuallyHidden } from '../VisuallyHidden';\nimport { renderText as defaultRenderText } from './renderText';\nimport { getRenderTextMentionEntities } from './renderText/rehypePlugins';\n\nimport type { LocalMessage } from 'stream-chat';\nimport { getTranslatedMessageText } from '../../context/MessageTranslationViewContext';\n\nexport type MessageTextProps = {\n  /** Class name for the inner `div`. */\n  customInnerClass?: string;\n  /** Replaces the default `str-chat__message-text` class on the outer `div`. */\n  customWrapperClass?: string;\n  /** Message to render; overrides the one from `MessageContext`. */\n  message?: LocalMessage;\n} & Pick<MessageContextValue, 'renderText'>;\n\nconst UnMemoizedMessageTextComponent = (props: MessageTextProps) => {\n  const {\n    customInnerClass,\n    customWrapperClass = '',\n    message: propMessage,\n    renderText: propsRenderText,\n  } = props;\n\n  const {\n    message: contextMessage,\n    onMentionsClickMessage,\n    onMentionsHoverMessage,\n    renderText: contextRenderText,\n    translationView = 'translated',\n    unsafeHTML,\n  } = useMessageContext();\n\n  const renderText = propsRenderText ?? contextRenderText ?? defaultRenderText;\n\n  const { t, userLanguage } = useTranslationContext();\n  const message = propMessage || contextMessage;\n  const hasAttachment = messageHasAttachments(message);\n  const messageContextId = useStableId();\n  const messageTextId = useStableId();\n\n  const messageTextToRender =\n    translationView === 'original'\n      ? message.text\n      : getTranslatedMessageText({ language: userLanguage, message }) || message.text;\n  const renderTextMentionEntities = useMemo(\n    () =>\n      getRenderTextMentionEntities({\n        mentioned_channel: message.mentioned_channel,\n        mentioned_groups: message.mentioned_groups,\n        mentioned_here: message.mentioned_here,\n        mentioned_roles: message.mentioned_roles,\n        mentioned_users: message.mentioned_users,\n      }),\n    [\n      message.mentioned_channel,\n      message.mentioned_groups,\n      message.mentioned_here,\n      message.mentioned_roles,\n      message.mentioned_users,\n    ],\n  );\n\n  const messageText = useMemo(\n    () =>\n      renderText(messageTextToRender, {\n        messageMentionEntities: renderTextMentionEntities,\n      }),\n    [messageTextToRender, renderText, renderTextMentionEntities],\n  );\n\n  const wrapperClass = customWrapperClass || 'str-chat__message-text';\n  const innerClass = customInnerClass;\n  const hasMentions = Boolean(renderTextMentionEntities.length);\n  const isMentionsInteractionEnabled =\n    hasMentions && typeof onMentionsClickMessage === 'function';\n  const senderName = message.user?.name;\n  const messageContext = senderName\n    ? t('message.text.message.withUser.ariaLabel', 'Message from {{ user }},', {\n        user: senderName,\n      })\n    : t('message.text.message.ariaLabel', 'Message,');\n  // `aria-labelledby` takes space-separated IDs: the hidden message context and the rendered\n  // text, so screen readers announce both.\n  const messageLabelledBy = `${messageContextId} ${messageTextId}`;\n\n  const handleMentionsKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {\n    if (!isMentionsInteractionEnabled || (event.key !== 'Enter' && event.key !== ' ')) {\n      return;\n    }\n\n    event.preventDefault();\n    onMentionsClickMessage?.(event);\n  };\n\n  if (!messageTextToRender) return null;\n\n  // Two mutually exclusive focus models. Without interactive mentions the outer wrapper is the\n  // only focus stop. With them the inner element takes focus, because its onClick, onKeyDown and\n  // hover handlers live there, and the outer one is not focusable so it adds no dead focus stop.\n  return (\n    <div\n      aria-labelledby={isMentionsInteractionEnabled ? undefined : messageLabelledBy}\n      className={wrapperClass}\n      tabIndex={isMentionsInteractionEnabled ? undefined : 0}\n    >\n      <VisuallyHidden id={messageContextId}>{messageContext}</VisuallyHidden>\n      <div\n        aria-labelledby={isMentionsInteractionEnabled ? messageLabelledBy : undefined}\n        className={clsx(innerClass, {\n          'str-chat__message-text-inner--has-attachment': hasAttachment,\n          'str-chat__message-text-inner--is-emoji':\n            messageTextHasEmojisOnly(message) && !message.quoted_message,\n          'str-chat__message-text-inner--mentions-clickable':\n            isMentionsInteractionEnabled,\n        })}\n        data-testid='message-text-inner-wrapper'\n        onClick={isMentionsInteractionEnabled ? onMentionsClickMessage : undefined}\n        onKeyDown={isMentionsInteractionEnabled ? handleMentionsKeyDown : undefined}\n        onMouseOver={onMentionsHoverMessage}\n        tabIndex={isMentionsInteractionEnabled ? 0 : undefined}\n      >\n        {unsafeHTML && message.html ? (\n          <div dangerouslySetInnerHTML={{ __html: message.html }} id={messageTextId} />\n        ) : (\n          <div id={messageTextId}>{messageText}</div>\n        )}\n      </div>\n    </div>\n  );\n};\n\nexport const MessageText = React.memo(\n  UnMemoizedMessageTextComponent,\n) as typeof UnMemoizedMessageTextComponent;\n"],"mappings":";;;;;;;;;;;;AAuBA,IAAM,kCAAkC,UAA4B;CAClE,MAAM,EACJ,kBACA,qBAAqB,IACrB,SAAS,aACT,YAAY,oBACV;CAEJ,MAAM,EACJ,SAAS,gBACT,wBACA,wBACA,YAAY,mBACZ,kBAAkB,cAClB,eACE,kBAAkB;CAEtB,MAAM,eAAa,mBAAmB,qBAAqB;CAE3D,MAAM,EAAE,GAAG,iBAAiB,sBAAsB;CAClD,MAAM,UAAU,eAAe;CAC/B,MAAM,gBAAgB,sBAAsB,OAAO;CACnD,MAAM,mBAAmB,YAAY;CACrC,MAAM,gBAAgB,YAAY;CAElC,MAAM,sBACJ,oBAAoB,aAChB,QAAQ,OACR,yBAAyB;EAAE,UAAU;EAAc;CAAQ,CAAC,KAAK,QAAQ;CAC/E,MAAM,4BAA4B,cAE9B,6BAA6B;EAC3B,mBAAmB,QAAQ;EAC3B,kBAAkB,QAAQ;EAC1B,gBAAgB,QAAQ;EACxB,iBAAiB,QAAQ;EACzB,iBAAiB,QAAQ;CAC3B,CAAC,GACH;EACE,QAAQ;EACR,QAAQ;EACR,QAAQ;EACR,QAAQ;EACR,QAAQ;CACV,CACF;CAEA,MAAM,cAAc,cAEhB,aAAW,qBAAqB,EAC9B,wBAAwB,0BAC1B,CAAC,GACH;EAAC;EAAqB;EAAY;C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