{"version":3,"file":"VideoAttachment.mjs","names":[],"sources":["../../../../src/components/Attachment/VideoAttachment.tsx"],"sourcesContent":["import type { VideoAttachment as VideoAttachmentType } from 'stream-chat';\nimport React, { type ComponentType, useLayoutEffect, useRef, useState } from 'react';\nimport { getCssDimensionsVariables } from './utils';\nimport type { VideoPlayerProps } from '../VideoPlayer';\nimport { VideoPlayer as DefaultVideoPlayer } from '../VideoPlayer';\nimport { VideoThumbnail } from '../VideoPlayer/VideoThumbnail';\nimport clsx from 'clsx';\nimport { useLocalVideoDimensions } from './hooks/useLocalVideoDimensions';\nimport { getAttachmentPreviewUrl, isPendingUpload } from 'stream-chat';\nimport {\n  useAttachmentContext,\n  type VideoAttachmentConfiguration,\n} from '../../context/AttachmentContext';\n\nexport type VideoAttachmentProps = {\n  attachment: VideoAttachmentType;\n  VideoPlayer?: ComponentType<VideoPlayerProps>;\n};\n\nexport const VideoAttachment = ({\n  attachment,\n  VideoPlayer = DefaultVideoPlayer,\n}: VideoAttachmentProps) => {\n  const { shouldGenerateVideoThumbnail, videoAttachmentSizeHandler } =\n    useAttachmentContext();\n  const videoElement = useRef<HTMLDivElement>(null);\n  const [attachmentConfiguration, setAttachmentConfiguration] =\n    useState<VideoAttachmentConfiguration>();\n  // A video that mounts while uploading stays on the player after the upload lands: switching to\n  // the thumbnail would swap the rendered element when `thumb_url` arrives and again on play, and\n  // each swap resizes the `fit-content` bubble (local `<video>`, CDN `<img>` and CDN `<video>`\n  // have different intrinsic widths).\n  const [showVideo, setShowVideo] = React.useState(\n    () => !shouldGenerateVideoThumbnail || isPendingUpload(attachment),\n  );\n  // Only a click on the thumbnail's play button requests playback; `showVideo` is also true for\n  // videos that never had a thumbnail to click.\n  const [playbackRequested, setPlaybackRequested] = React.useState(false);\n  // Whether the upload is still pending: the bytes are not on the CDN yet, whether or not a\n  // request is running right now.\n  const isUploading = isPendingUpload(attachment);\n  // While the upload is pending there is no CDN `thumb_url` to read `oh`/`ow` from, so the box\n  // would lay out from the 1000000x1000000 fallback and resize later. The local file knows them.\n  const localDimensions = useLocalVideoDimensions(\n    isUploading ? getAttachmentPreviewUrl(attachment) : undefined,\n  );\n  const dimensionVariables = localDimensions\n    ? {\n        '--original-height': localDimensions.height,\n        '--original-width': localDimensions.width,\n      }\n    : getCssDimensionsVariables(attachment.thumb_url || '');\n\n  useLayoutEffect(() => {\n    if (videoElement.current && videoAttachmentSizeHandler) {\n      const config = videoAttachmentSizeHandler(\n        attachment,\n        videoElement.current,\n        shouldGenerateVideoThumbnail,\n      );\n      setAttachmentConfiguration(config);\n    }\n  }, [\n    videoElement,\n    videoAttachmentSizeHandler,\n    attachment,\n    shouldGenerateVideoThumbnail,\n  ]);\n\n  const renderThumbnailFirst = Boolean(\n    attachment.thumb_url && shouldGenerateVideoThumbnail,\n  );\n\n  // todo: handle failed thumbnail loading\n  return (\n    <div\n      className={clsx('str-chat__player-wrapper', {\n        // Only until the dimensions are known: lets the element use its own aspect ratio instead\n        // of the fallback square.\n        'str-chat__player-wrapper--uploading': isUploading && !localDimensions,\n      })}\n      data-testid='video-wrapper'\n      ref={videoElement}\n      style={dimensionVariables}\n    >\n      {renderThumbnailFirst && !showVideo ? (\n        <VideoThumbnail\n          alt={attachment.title}\n          data-testid='image-test'\n          onPlay={() => {\n            setPlaybackRequested(true);\n            setShowVideo(true);\n          }}\n          src={attachment.thumb_url}\n          title={attachment.title}\n        />\n      ) : (\n        <VideoPlayer\n          // Playback stays available while the upload runs (the local blob plays); the progress\n          // overlay sits clear of the transport controls. Autoplay only after a click on the\n          // thumbnail: `renderThumbnailFirst` only says a thumbnail exists, so using it would\n          // restart a video watched from its local blob once `thumb_url` arrives.\n          isPlaying={playbackRequested}\n          thumbnailUrl={attachmentConfiguration?.thumbUrl}\n          // Falls back to the local blob preview while the upload is still in flight.\n          videoUrl={getAttachmentPreviewUrl(attachment, attachmentConfiguration?.url)}\n        />\n      )}\n    </div>\n  );\n};\n"],"mappings":";;;;;;;;;;AAmBA,IAAa,mBAAmB,EAC9B,YACA,aAAA,gBAAc,kBACY;CAC1B,MAAM,EAAE,8BAA8B,+BACpC,qBAAqB;CACvB,MAAM,eAAe,OAAuB,IAAI;CAChD,MAAM,CAAC,yBAAyB,8BAC9B,SAAuC;CAKzC,MAAM,CAAC,WAAW,gBAAgB,MAAM,eAChC,CAAC,gCAAgC,gBAAgB,UAAU,CACnE;CAGA,MAAM,CAAC,mBAAmB,wBAAwB,MAAM,SAAS,KAAK;CAGtE,MAAM,cAAc,gBAAgB,UAAU;CAG9C,MAAM,kBAAkB,wBACtB,cAAc,wBAAwB,UAAU,IAAI,KAAA,CACtD;CACA,MAAM,qBAAqB,kBACvB;EACE,qBAAqB,gBAAgB;EACrC,oBAAoB,gBAAgB;CACtC,IACA,0BAA0B,WAAW,aAAa,EAAE;CAExD,sBAAsB;EACpB,IAAI,aAAa,WAAW,4BAM1B,2BALe,2BACb,YACA,aAAa,SACb,4BAEyB,CAAM;CAErC,GAAG;EACD;EACA;EACA;EACA;CACF,CAAC;CAED,MAAM,uBAAuB,QAC3B,WAAW,aAAa,4BAC1B;CAGA,OACE,oBAAC,OAAD;EACE,WAAW,KAAK,4BAA4B,EAG1C,uCAAuC,eAAe,CAAC,gBACzD,CAAC;EACD,eAAY;EACZ,KAAK;EACL,OAAO;YAEN,wBAAwB,CAAC,YACxB,oBAAC,gBAAD;GACE,KAAK,WAAW;GAChB,eAAY;GACZ,cAAc;IACZ,qBAAqB,IAAI;IACzB,aAAa,IAAI;GACnB;GACA,KAAK,WAAW;GAChB,OAAO,WAAW;EACnB,CAAA,IAED,oBAAC,eAAD;GAKE,WAAW;GACX,cAAc,yBAAyB;GAEvC,UAAU,wBAAwB,YAAY,yBAAyB,GAAG;EAC3E,CAAA;CAEA,CAAA;AAET"}