{"version":3,"file":"useLatestMessagePreview.mjs","names":[],"sources":["../../../../../src/components/SummarizedMessagePreview/hooks/useLatestMessagePreview.ts"],"sourcesContent":["import { useMemo } from 'react';\nimport type { Attachment, LocalMessage, VoiceRecordingAttachment } from 'stream-chat';\nimport {\n  isAudioAttachment,\n  isFileAttachment,\n  isImageAttachment,\n  isScrapedContent,\n  isVideoAttachment,\n  isVoiceRecordingAttachment,\n} from 'stream-chat';\n\nimport {\n  getTranslatedMessageText,\n  type TranslationContextValue,\n  useChatContext,\n  useTranslationContext,\n} from '../../../context';\nimport { isMessageDeleted } from '../../Message/utils';\n\nimport type { MessageDeliveryStatus } from '../../ChannelListItem';\n\nexport type ChannelPreviewMessageType =\n  | 'text'\n  | 'deleted'\n  | 'error'\n  | 'empty'\n  | 'image'\n  | 'giphy'\n  | 'video'\n  | 'voice'\n  | 'file'\n  | 'unsupported'\n  | 'link'\n  | 'location'\n  | 'poll';\n\nconst SUPPORTED_VIDEO_FORMATS: string[] = [\n  'video/mp4',\n  'video/ogg',\n  'video/webm',\n  'video/quicktime',\n];\n\n/**\n * Delivery status of the last own message, which selects the status icon: a clock for `sending`, a\n * checkmark for `sent`, double checkmarks for `delivered` and `read`.\n */\nexport type ChannelPreviewDeliveryStatus = 'sending' | 'sent' | 'delivered' | 'read';\n\nexport type LatestMessagePreviewData = {\n  /** Kind of content previewed, which selects the content-type icon. */\n  type: ChannelPreviewMessageType;\n  text: string;\n  /** Set only for own messages that did not fail to send. */\n  deliveryStatus?: ChannelPreviewDeliveryStatus;\n  /**\n   * Sender prefix: \"You\" (translated) for own messages, the sender's name for incoming messages\n   * when `participantCount` is above 2, otherwise undefined.\n   */\n  senderName?: string;\n};\n\nfunction getAttachmentContentType(attachment: Attachment): ChannelPreviewMessageType {\n  if (!attachment) return 'text';\n\n  if (attachment.type === 'giphy') return 'giphy';\n  if (isScrapedContent(attachment)) return 'link';\n  if (isImageAttachment(attachment)) return 'image';\n  if (isVideoAttachment(attachment, SUPPORTED_VIDEO_FORMATS)) return 'video';\n  if (isVoiceRecordingAttachment(attachment)) return 'voice';\n  if (\n    isAudioAttachment(attachment) ||\n    isFileAttachment(attachment, SUPPORTED_VIDEO_FORMATS)\n  ) {\n    return 'file';\n  }\n\n  return 'unsupported';\n}\n\nfunction getAttachmentFallbackText(\n  type: ChannelPreviewMessageType,\n  count: number,\n  t: TranslationContextValue['t'],\n): string {\n  switch (type) {\n    case 'image':\n      return t('messagePreview.latestMessagePreview.imageCount.label', {\n        count,\n        defaultValue_one: 'Image',\n        defaultValue_other: '{{ count }} images',\n      });\n    case 'video':\n      return t('messagePreview.latestMessagePreview.videoCount.label', {\n        count,\n        defaultValue_one: 'Video',\n        defaultValue_other: '{{ count }} videos',\n      });\n    case 'voice':\n      return t('messagePreview.latestMessagePreview.voiceMessageCount.label', {\n        count,\n        defaultValue_one: 'Voice message',\n        defaultValue_other: '{{ count }} voice messages',\n      });\n    case 'link':\n      return t('messagePreview.latestMessagePreview.linkCount.label', {\n        count,\n        defaultValue_one: 'Link',\n        defaultValue_other: '{{ count }} links',\n      });\n    case 'file':\n      return t('messagePreview.latestMessagePreview.fileCount.label', {\n        count,\n        defaultValue_one: 'File',\n        defaultValue_other: '{{ count }} files',\n      });\n    case 'unsupported':\n      return t('common.unsupportedAttachment.text', 'Unsupported attachment');\n    default:\n      return t('messagePreview.latestMessagePreview.fileCount.label', {\n        count,\n        defaultValue_one: 'File',\n        defaultValue_other: '{{ count }} files',\n      });\n  }\n}\n\nexport type UseLatestMessagePreviewParams = {\n  /** Number of channel participants. Incoming messages show the sender above 2. */\n  participantCount?: number;\n  /** Message to preview. Without one the preview is the empty-state text. */\n  latestMessage?: LocalMessage;\n  /**\n   * Delivery status from `useMessageDeliveryStatus`. Defaults to the message's own `status` for\n   * own messages.\n   */\n  messageDeliveryStatus?: MessageDeliveryStatus;\n};\n\nexport const useLatestMessagePreview = ({\n  latestMessage,\n  messageDeliveryStatus,\n  participantCount,\n}: UseLatestMessagePreviewParams): LatestMessagePreviewData => {\n  const { client } = useChatContext();\n  const { t, userLanguage } = useTranslationContext();\n\n  return useMemo(() => {\n    if (!latestMessage) {\n      return {\n        text: t('common.nothingYet.text', 'Nothing yet...'),\n        type: 'empty' as const,\n      };\n    }\n\n    if (latestMessage.status === 'failed' || latestMessage.type === 'error') {\n      return {\n        text: t(\n          'messagePreview.latestMessagePreview.messageFailedSend.text',\n          'Message failed to send',\n        ),\n        type: 'error' as const,\n      };\n    }\n\n    const isOwnMessage = latestMessage.user?.id === client.user?.id;\n\n    let deliveryStatus: ChannelPreviewDeliveryStatus | undefined;\n    if (isOwnMessage) {\n      // Of the message's own statuses only `sending` is a delivery status.\n      deliveryStatus =\n        messageDeliveryStatus ??\n        (latestMessage.status === 'sending' ? 'sending' : undefined);\n    }\n\n    let senderName: string | undefined;\n    if (isOwnMessage) {\n      senderName = t('common.you.label', 'You');\n    } else if (!isOwnMessage && participantCount !== undefined && participantCount > 2) {\n      senderName = latestMessage.user?.name || latestMessage.user?.id;\n    }\n\n    if (isMessageDeleted(latestMessage)) {\n      return {\n        deliveryStatus,\n        senderName,\n        text: t('common.messageDeleted.text', 'Message deleted'),\n        type: 'deleted' as const,\n      };\n    }\n\n    if (latestMessage.poll) {\n      return {\n        deliveryStatus,\n        senderName,\n        text: t('common.poll.label', 'Poll'),\n        type: 'poll' as const,\n      };\n    }\n\n    const textContent =\n      getTranslatedMessageText({ language: userLanguage, message: latestMessage }) ||\n      latestMessage.text;\n\n    if (latestMessage.shared_location) {\n      return {\n        deliveryStatus,\n        senderName,\n        text: textContent || t('common.location.text', 'Location'),\n        type: 'location' as const,\n      };\n    }\n\n    if (latestMessage.attachments && latestMessage.attachments.length) {\n      const attachments = latestMessage.attachments;\n\n      let contentType: ChannelPreviewMessageType;\n\n      const [firstAttachment] = attachments;\n      const firstAttachmentType = getAttachmentContentType(firstAttachment);\n\n      if (\n        attachments.every(\n          (attachment) => getAttachmentContentType(attachment) === firstAttachmentType,\n        )\n      ) {\n        contentType = firstAttachmentType;\n      } else {\n        contentType = 'file';\n      }\n\n      let text =\n        contentType === 'giphy'\n          ? `GIPHY ${firstAttachment.title ?? ''}`.trim()\n          : // message text first\n            textContent ||\n            // then a lone attachment's fallback or title, skipped for voice and unsupported ones\n            // because their fallback text is generic\n            (attachments.length === 1 &&\n            contentType !== 'voice' &&\n            contentType !== 'unsupported'\n              ? firstAttachment.fallback || firstAttachment.title\n              : '') ||\n            // then a generic label from the attachment type and count\n            getAttachmentFallbackText(contentType, attachments.length, t);\n\n      // Appends the duration of a lone attachment that has one. `Attachment.custom` is typed as\n      // required but is missing at runtime on some attachments (og-scraped, giphy, custom\n      // integrations), and the cast covers every type, so it is read defensively.\n      const firstAttachmentDuration = (firstAttachment as VoiceRecordingAttachment).custom\n        ?.duration;\n      if (attachments.length === 1 && typeof firstAttachmentDuration === 'number') {\n        const minutes = Math.floor(firstAttachmentDuration / 60);\n        const seconds = Math.ceil(firstAttachmentDuration) % 60;\n        const durationString = `${minutes}:${seconds.toString().padStart(2, '0')}`;\n        text += ` (${durationString})`;\n      }\n\n      return {\n        deliveryStatus,\n        senderName,\n        text,\n        type: contentType,\n      };\n    }\n\n    if (textContent) {\n      return {\n        deliveryStatus,\n        senderName,\n        text: textContent,\n        type: 'text' as const,\n      };\n    }\n\n    return {\n      text: t('common.emptyMessage.text', 'Empty message...'),\n      type: 'empty' as const,\n    };\n  }, [\n    client.user?.id,\n    latestMessage,\n    messageDeliveryStatus,\n    participantCount,\n    t,\n    userLanguage,\n  ]);\n};\n"],"mappings":";;;;;;;AAoCA,IAAM,0BAAoC;CACxC;CACA;CACA;CACA;AACF;AAqBA,SAAS,yBAAyB,YAAmD;CACnF,IAAI,CAAC,YAAY,OAAO;CAExB,IAAI,WAAW,SAAS,SAAS,OAAO;CACxC,IAAI,iBAAiB,UAAU,GAAG,OAAO;CACzC,IAAI,kBAAkB,UAAU,GAAG,OAAO;CAC1C,IAAI,kBAAkB,YAAY,uBAAuB,GAAG,OAAO;CACnE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