{"version":3,"file":"Attachment.mjs","names":[],"sources":["../../../../src/components/Attachment/Attachment.tsx"],"sourcesContent":["import React, { useMemo } from 'react';\nimport type {\n  GiphyVersions,\n  SharedLocationResponseData,\n  Attachment as StreamAttachment,\n} from 'stream-chat';\nimport {\n  isAudioAttachment,\n  isFileAttachment,\n  isImageAttachment,\n  isScrapedContent,\n  isSharedLocationResponseData,\n  isVideoAttachment,\n  isVoiceRecordingAttachment,\n} from 'stream-chat';\n\nimport {\n  CardContainer,\n  FileContainer,\n  GeolocationContainer,\n  GiphyContainer,\n  MediaContainer,\n  UnsupportedAttachmentContainer,\n} from './AttachmentContainer';\nimport type { GroupedRenderedAttachment } from './utils';\nimport { SUPPORTED_VIDEO_FORMATS } from './utils';\nimport type {\n  AttachmentActionsDefaultFocusByType,\n  AttachmentActionsProps,\n} from './AttachmentActions';\nimport { defaultAttachmentActionsDefaultFocus } from './AttachmentActions';\nimport type { AudioProps } from './Audio';\nimport type { VoiceRecordingProps } from './VoiceRecording';\nimport type { CardProps } from './LinkPreview/Card';\nimport type { FileAttachmentProps } from './FileAttachment';\nimport type { UnsupportedAttachmentProps } from './UnsupportedAttachment';\nimport type { ActionHandlerReturnType } from '../Message/hooks/useActionHandler';\nimport type { GeolocationProps } from './Geolocation';\nimport type { GiphyAttachmentProps } from './Giphy';\nimport type { VideoPlayerProps } from '../VideoPlayer';\nimport type { ModalGalleryProps } from './ModalGallery';\nimport type { ImageProps } from './Image';\nimport {\n  AttachmentContextProvider,\n  defaultAttachmentContextValue,\n  type ImageAttachmentConfiguration,\n  type VideoAttachmentConfiguration,\n} from '../../context/AttachmentContext';\n\nexport const ATTACHMENT_GROUPS_ORDER = [\n  'media',\n  'giphy',\n  'card',\n  'geolocation',\n  'file',\n  'unsupported',\n] as const;\n\nexport type ImageAttachmentSizeHandler = (\n  attachment: StreamAttachment,\n  element: HTMLElement,\n) => ImageAttachmentConfiguration;\n\nexport type VideoAttachmentSizeHandler = (\n  attachment: StreamAttachment,\n  element: HTMLElement,\n  shouldGenerateVideoThumbnail: boolean,\n) => VideoAttachmentConfiguration;\n\nexport type AttachmentProps = {\n  /** Attachments to render. See [attachment structure](https://getstream.io/chat/docs/javascript/message_format/?language=javascript). */\n  attachments: (StreamAttachment | SharedLocationResponseData)[];\n  /** Called when an attachment action is performed, e.g. sending or shuffling a giphy preview. */\n  actionHandler?: ActionHandlerReturnType;\n  /**\n   * Which action button gets initial focus, keyed by attachment type. Values match an action's\n   * `value` (e.g. `'send'`, `'cancel'`, `'shuffle'` for giphy). Defaults to `{ giphy: 'send' }`.\n   * Pass an empty object to disable auto-focus, e.g. when the Giphy preview sits above the\n   * composer and focus should stay in the message input.\n   */\n  attachmentActionsDefaultFocus?: AttachmentActionsDefaultFocusByType;\n  /** Custom attachment actions UI. Defaults to `AttachmentActions`. */\n  AttachmentActions?: React.ComponentType<AttachmentActionsProps>;\n  /** Custom UI for audio attachments. Defaults to `Audio`. */\n  Audio?: React.ComponentType<AudioProps>;\n  /** Custom UI for card attachments. Defaults to `Card`. */\n  Card?: React.ComponentType<CardProps>;\n  /** Custom UI for file attachments. Defaults to `FileAttachment`. */\n  File?: React.ComponentType<FileAttachmentProps>;\n  /** Custom UI for the gallery of image attachments. Defaults to `ModalGallery`. */\n  ModalGallery?: React.ComponentType<ModalGalleryProps>;\n  Geolocation?: React.ComponentType<GeolocationProps>;\n  /** Custom UI for Giphy attachments. Defaults to `Giphy`. */\n  Giphy?: React.ComponentType<GiphyAttachmentProps>;\n  /** Custom UI for image attachments. Defaults to `ImageComponent`. */\n  Image?: React.ComponentType<ImageProps>;\n  /** Giphy rendition to render. Defaults to `'fixed_height'`. */\n  giphyVersion?: GiphyVersions;\n  /** Computes the responsive size configuration of an image attachment. */\n  imageAttachmentSizeHandler?: ImageAttachmentSizeHandler;\n  /** Whether the attachments belong to a quoted message. */\n  isQuoted?: boolean;\n  /** Custom UI for video attachments. Defaults to `VideoPlayer`. */\n  Media?: React.ComponentType<VideoPlayerProps>;\n  /** Whether videos show their thumbnail before playback starts. Defaults to true. */\n  shouldGenerateVideoThumbnail?: boolean;\n  /** Custom UI for unsupported attachment types. Defaults to `UnsupportedAttachment`. */\n  UnsupportedAttachment?: React.ComponentType<UnsupportedAttachmentProps>;\n  /** Computes the responsive size configuration of a video attachment. */\n  videoAttachmentSizeHandler?: VideoAttachmentSizeHandler;\n  /** Custom UI for voice recording attachments. Defaults to `VoiceRecording`. */\n  VoiceRecording?: React.ComponentType<VoiceRecordingProps>;\n};\n\n/** Renders message attachments grouped by type. */\nexport const Attachment = (props: AttachmentProps) => {\n  const {\n    attachmentActionsDefaultFocus = defaultAttachmentActionsDefaultFocus,\n    attachments,\n    giphyVersion,\n    imageAttachmentSizeHandler,\n    shouldGenerateVideoThumbnail,\n    videoAttachmentSizeHandler,\n    ...rest\n  } = props;\n  const attachmentContextValue = useMemo(\n    () => ({\n      giphyVersion: giphyVersion ?? defaultAttachmentContextValue.giphyVersion,\n      imageAttachmentSizeHandler:\n        imageAttachmentSizeHandler ??\n        defaultAttachmentContextValue.imageAttachmentSizeHandler,\n      shouldGenerateVideoThumbnail:\n        shouldGenerateVideoThumbnail ??\n        defaultAttachmentContextValue.shouldGenerateVideoThumbnail,\n      videoAttachmentSizeHandler:\n        videoAttachmentSizeHandler ??\n        defaultAttachmentContextValue.videoAttachmentSizeHandler,\n    }),\n    [\n      giphyVersion,\n      imageAttachmentSizeHandler,\n      shouldGenerateVideoThumbnail,\n      videoAttachmentSizeHandler,\n    ],\n  );\n\n  const groupedAttachments = useMemo(\n    () =>\n      renderGroupedAttachments({\n        attachmentActionsDefaultFocus,\n        attachments,\n        ...rest,\n      }),\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n    [attachments, attachmentActionsDefaultFocus],\n  );\n\n  return (\n    <AttachmentContextProvider value={attachmentContextValue}>\n      <div className='str-chat__attachment-list'>\n        {ATTACHMENT_GROUPS_ORDER.reduce(\n          (acc, groupName) => [...acc, ...groupedAttachments[groupName]],\n          [] as React.ReactNode[],\n        )}\n      </div>\n    </AttachmentContextProvider>\n  );\n};\n\nconst renderGroupedAttachments = ({\n  attachments,\n  ...rest\n}: AttachmentProps): GroupedRenderedAttachment => {\n  const mediaAttachments: StreamAttachment[] = [];\n  const containers = attachments.reduce<GroupedRenderedAttachment>(\n    (typeMap, attachment) => {\n      if (isSharedLocationResponseData(attachment)) {\n        typeMap.geolocation.push(\n          <GeolocationContainer\n            {...rest}\n            key={`geolocation-${typeMap.geolocation.length}`}\n            location={attachment}\n          />,\n        );\n      } else if (attachment.type === 'giphy') {\n        typeMap.giphy.push(\n          <GiphyContainer\n            key={`giphy-${typeMap.giphy.length}`}\n            {...rest}\n            attachment={attachment}\n          />,\n        );\n      } else if (isScrapedContent(attachment)) {\n        typeMap.card.push(\n          <CardContainer\n            key={`card-${typeMap.card.length}`}\n            {...rest}\n            attachment={attachment}\n          />,\n        );\n      } else if (\n        isImageAttachment(attachment) ||\n        isVideoAttachment(attachment, SUPPORTED_VIDEO_FORMATS)\n      ) {\n        mediaAttachments.push(attachment);\n      } else if (\n        isAudioAttachment(attachment) ||\n        isVoiceRecordingAttachment(attachment) ||\n        isFileAttachment(attachment, SUPPORTED_VIDEO_FORMATS)\n      ) {\n        typeMap.file.push(\n          <FileContainer\n            key={`file-${typeMap.file.length}`}\n            {...rest}\n            attachment={attachment}\n          />,\n        );\n      } else {\n        typeMap.unsupported.push(\n          <UnsupportedAttachmentContainer\n            key={`unsupported-${typeMap.unsupported.length}`}\n            {...rest}\n            attachment={attachment}\n          />,\n        );\n      }\n\n      return typeMap;\n    },\n    {\n      card: [],\n      file: [],\n      geolocation: [],\n      giphy: [],\n      media: [],\n      unsupported: [],\n    },\n  );\n\n  if (mediaAttachments.length) {\n    containers.media.push(\n      <MediaContainer key='media-container' {...rest} attachments={mediaAttachments} />,\n    );\n  }\n\n  return containers;\n};\n"],"mappings":";;;;;;;;AAiDA,IAAa,0BAA0B;CACrC;CACA;CACA;CACA;CACA;CACA;AACF;;AA2DA,IAA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