{"version":3,"file":"AttachmentPreviewList.mjs","names":[],"sources":["../../../../../src/components/MessageComposer/AttachmentPreviewList/AttachmentPreviewList.tsx"],"sourcesContent":["import React, { type ComponentType, useCallback, useMemo, useRef, useState } from 'react';\nimport {\n  isLocalAttachment,\n  isLocalAudioAttachment,\n  isLocalFileAttachment,\n  isLocalImageAttachment,\n  isLocalVideoAttachment,\n  isLocalVoiceRecordingAttachment,\n  isScrapedContent,\n  isVoiceRecordingAttachment,\n} from 'stream-chat';\nimport {\n  UnsupportedAttachmentPreview as DefaultUnknownAttachmentPreview,\n  type UnsupportedAttachmentPreviewProps,\n} from './UnsupportedAttachmentPreview';\nimport {\n  FileAttachmentPreview as DefaultFileAttachmentPreview,\n  type FileAttachmentPreviewProps,\n} from './FileAttachmentPreview';\nimport { type AudioAttachmentPreviewProps } from './AudioAttachmentPreview';\nimport { type ImageAttachmentPreviewProps } from './ImageAttachmentPreview';\nimport { useAttachmentsForPreview, useMessageComposerController } from '../hooks';\nimport {\n  MediaAttachmentPreview,\n  type MediaAttachmentPreviewProps,\n} from './MediaAttachmentPreview';\nimport { toBaseImageDescriptors } from '../../BaseImage';\nimport { Gallery as DefaultGallery } from '../../Gallery';\nimport { GlobalModal, type ModalCloseSource } from '../../Modal';\nimport { useComponentContext } from '../../../context';\nimport { useRetryAttachmentUpload } from './useRetryAttachmentUpload';\n\nexport type AttachmentPreviewListProps = {\n  AudioAttachmentPreview?:\n    | ComponentType<AudioAttachmentPreviewProps>\n    | ComponentType<FileAttachmentPreviewProps>;\n  FileAttachmentPreview?: ComponentType<FileAttachmentPreviewProps>;\n  ImageAttachmentPreview?: ComponentType<ImageAttachmentPreviewProps>;\n  UnsupportedAttachmentPreview?: ComponentType<UnsupportedAttachmentPreviewProps>;\n  VideoAttachmentPreview?: ComponentType<MediaAttachmentPreviewProps>;\n};\n\nexport const AttachmentPreviewList = ({\n  AudioAttachmentPreview = DefaultFileAttachmentPreview,\n  FileAttachmentPreview = DefaultFileAttachmentPreview,\n  ImageAttachmentPreview = MediaAttachmentPreview,\n  UnsupportedAttachmentPreview = DefaultUnknownAttachmentPreview,\n  VideoAttachmentPreview = MediaAttachmentPreview,\n}: AttachmentPreviewListProps) => {\n  const messageComposer = useMessageComposerController();\n  const retryUpload = useRetryAttachmentUpload();\n  const { Gallery = DefaultGallery, Modal = GlobalModal } = useComponentContext();\n  const [showPreview, setShowPreview] = useState(false);\n  const initialIndexRef = useRef(0);\n  const preventOverlayClose = useCallback(\n    (source: ModalCloseSource) => source !== 'overlay',\n    [],\n  );\n\n  const { attachments } = useAttachmentsForPreview();\n  const filteredAttachments = useMemo(\n    () => attachments.filter((a) => !isVoiceRecordingAttachment(a)),\n    [attachments],\n  );\n\n  const { galleryItems, previewIndexById } = useMemo(() => {\n    const items: NonNullable<ReturnType<typeof toBaseImageDescriptors>>[] = [];\n    const indexById: Record<string, number> = {};\n    for (const a of attachments) {\n      if (isLocalImageAttachment(a) || isLocalVideoAttachment(a)) {\n        const descriptor = toBaseImageDescriptors(a);\n        if (descriptor) {\n          indexById[a.localMetadata.id] = items.length;\n          items.push(descriptor);\n        }\n      }\n    }\n    return { galleryItems: items, previewIndexById: indexById };\n  }, [attachments]);\n\n  const openPreviewAtIndex = useCallback((index: number) => {\n    initialIndexRef.current = index;\n    setShowPreview(true);\n  }, []);\n\n  if (!filteredAttachments.length) return null;\n\n  return (\n    <div\n      className='str-chat__attachment-preview-list'\n      data-testid='attachment-preview-list'\n    >\n      {attachments.map((attachment) => {\n        if (isScrapedContent(attachment)) return null;\n        // Voice recordings render in `VoiceRecordingPreviewSlot` instead.\n        if (isLocalVoiceRecordingAttachment(attachment)) return null;\n        if (isLocalAudioAttachment(attachment)) {\n          return (\n            <AudioAttachmentPreview\n              attachment={attachment}\n              handleRetry={retryUpload}\n              key={attachment.localMetadata.id || attachment.asset_url}\n              removeAttachments={messageComposer.attachmentManager.removeAttachments}\n            />\n          );\n        } else if (isLocalVideoAttachment(attachment)) {\n          return (\n            <VideoAttachmentPreview\n              attachment={attachment}\n              handleRetry={retryUpload}\n              key={attachment.localMetadata.id || attachment.asset_url}\n              openPreview={() =>\n                openPreviewAtIndex(previewIndexById[attachment.localMetadata.id] ?? 0)\n              }\n              removeAttachments={messageComposer.attachmentManager.removeAttachments}\n            />\n          );\n        } else if (isLocalImageAttachment(attachment)) {\n          return (\n            <ImageAttachmentPreview\n              attachment={attachment}\n              handleRetry={retryUpload}\n              key={attachment.localMetadata.id || attachment.image_url}\n              openPreview={() =>\n                openPreviewAtIndex(previewIndexById[attachment.localMetadata.id] ?? 0)\n              }\n              removeAttachments={messageComposer.attachmentManager.removeAttachments}\n            />\n          );\n        } else if (isLocalFileAttachment(attachment)) {\n          return (\n            <FileAttachmentPreview\n              attachment={attachment}\n              handleRetry={retryUpload}\n              key={attachment.localMetadata.id || attachment.asset_url}\n              removeAttachments={messageComposer.attachmentManager.removeAttachments}\n            />\n          );\n        } else if (isLocalAttachment(attachment)) {\n          return (\n            <UnsupportedAttachmentPreview\n              attachment={attachment}\n              handleRetry={retryUpload}\n              key={attachment.localMetadata.id}\n              removeAttachments={messageComposer.attachmentManager.removeAttachments}\n            />\n          );\n        }\n        return null;\n      })}\n      {galleryItems.length > 0 && (\n        <Modal\n          className='str-chat__gallery-modal'\n          onClose={() => setShowPreview(false)}\n          onCloseAttempt={preventOverlayClose}\n          open={showPreview}\n        >\n          <Gallery\n            initialIndex={initialIndexRef.current}\n            items={galleryItems}\n            onRequestClose={() => setShowPreview(false)}\n          />\n        </Modal>\n      )}\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;AA0CA,IAAa,yBAAyB,EACpC,yBAAyB,uBACzB,uBAAA,0BAAwB,uBACxB,yBAAyB,wBACzB,8BAAA,iCAA+B,8BAC/B,yBAAyB,6BACO;CAChC,MAAM,kBAAkB,6BAA6B;CACrD,MAAM,cAAc,yBAAyB;CAC7C,MAAM,EAAE,SAAA,YAAU,SAAgB,QAAQ,gBAAgB,oBAAoB;CAC9E,MAAM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