{"version":3,"file":"VoiceRecording.mjs","names":[],"sources":["../../../../src/components/Attachment/VoiceRecording.tsx"],"sourcesContent":["import React, { useContext } from 'react';\nimport type { Attachment, VoiceRecordingAttachment } from 'stream-chat';\n\nimport {\n  AttachmentUploadProgressIndicator as DefaultAttachmentUploadProgressIndicator,\n  FileSizeIndicator as DefaultFileSizeIndicator,\n} from './components';\nimport { FileIcon } from '../FileIcon';\nimport {\n  MessageContext,\n  useComponentContext,\n  useTranslationContext,\n} from '../../context';\nimport {\n  type AudioPlayer,\n  type AudioPlayerState,\n  DurationDisplay,\n  PlaybackRateButton,\n  WaveProgressBar,\n} from '../AudioPlayback';\nimport { useAudioPlayer } from '../AudioPlayback/WithAudioPlayback';\nimport { useStateStore } from '../../store';\nimport { PlayButton } from '../Button';\nimport { useThreadContext } from '../Threads';\nimport { getAttachmentPreviewUrl } from 'stream-chat';\nimport { useAttachmentUploadState } from './hooks/useAttachmentUploadState';\n\nconst rootClassName = 'str-chat__message-attachment__voice-recording-widget';\n\nconst audioPlayerStateSelector = (state: AudioPlayerState) => ({\n  canPlayRecord: state.canPlayRecord,\n  durationSeconds: state.durationSeconds,\n  isPlaying: state.isPlaying,\n  playbackRate: state.currentPlaybackRate,\n  progress: state.progressPercent,\n  secondsElapsed: state.secondsElapsed,\n});\n\ntype VoiceRecordingPlayerUIProps = {\n  attachment?: Attachment;\n  audioPlayer: AudioPlayer;\n};\n\n// todo: finish creating a BaseAudioPlayer derived from VoiceRecordingPlayerUI and AudioAttachmentUI\nconst VoiceRecordingPlayerUI = ({\n  attachment,\n  audioPlayer,\n}: VoiceRecordingPlayerUIProps) => {\n  const {\n    AttachmentUploadProgressIndicator = DefaultAttachmentUploadProgressIndicator,\n    FileSizeIndicator = DefaultFileSizeIndicator,\n  } = useComponentContext();\n  const { isUploading } = useAttachmentUploadState(attachment);\n  const { t } = useTranslationContext();\n  const {\n    canPlayRecord,\n    durationSeconds,\n    isPlaying,\n    playbackRate,\n    progress,\n    secondsElapsed,\n  } = useStateStore(audioPlayer?.state, audioPlayerStateSelector) ?? {};\n\n  return (\n    <div className={rootClassName} data-testid='voice-recording-widget'>\n      <div className='str-chat__message-attachment__voice-recording-widget__play-button-container'>\n        <PlayButton isPlaying={!!isPlaying} onClick={audioPlayer.togglePlay} />\n      </div>\n      <div className='str-chat__message-attachment__voice-recording-widget__metadata'>\n        <div className='str-chat__message-attachment__voice-recording-widget__audio-state'>\n          <div className='str-chat__message-attachment__voice-recording-widget__timer'>\n            {/*\n              While uploading, the upload progress takes this slot, ahead of the duration:\n              `MediaRecorderController` stamps `duration` on every recording before upload, which\n              would hide the upload state.\n            */}\n            {isUploading ? (\n              <AttachmentUploadProgressIndicator attachment={attachment} />\n            ) : durationSeconds ? (\n              <DurationDisplay\n                duration={durationSeconds}\n                isPlaying={!!isPlaying}\n                secondsElapsed={secondsElapsed}\n                showRemaining\n              />\n            ) : (\n              <FileSizeIndicator\n                fileSize={audioPlayer.fileSize}\n                maximumFractionDigits={0}\n              />\n            )}\n          </div>\n          <WaveProgressBar\n            durationSeconds={durationSeconds}\n            progress={progress}\n            secondsElapsed={secondsElapsed}\n            seek={audioPlayer.seek}\n            waveformData={audioPlayer.waveformData || []}\n          />\n        </div>\n      </div>\n      <div className='str-chat__message-attachment__voice-recording-widget__right-section'>\n        <PlaybackRateButton\n          aria-label={t('common.playbackSpeedX.label', 'Playback speed {{ rate }}x', {\n            rate: playbackRate?.toString() ?? '1',\n          })}\n          disabled={!canPlayRecord}\n          onClick={audioPlayer.increasePlaybackRate}\n        >\n          x{playbackRate?.toString()}\n        </PlaybackRateButton>\n      </div>\n    </div>\n  );\n};\n\nexport type VoiceRecordingPlayerProps = Pick<VoiceRecordingProps, 'attachment'> & {\n  /** Playback speeds that replace the defaults (1.0, 1.5, 2.0). */\n  playbackRates?: number[];\n};\n\nexport const VoiceRecordingPlayer = ({\n  attachment,\n  playbackRates,\n}: VoiceRecordingPlayerProps) => {\n  const { t } = useTranslationContext();\n  const { asset_url, title = t('common.voiceMessage.label', 'Voice message') } =\n    attachment;\n  const {\n    duration = 0,\n    file_size,\n    mime_type,\n    waveform_data,\n  } = (attachment as VoiceRecordingAttachment).custom ?? {};\n\n  // Players are namespaced by message (and thread) through `requester`. Outside a message, or when\n  // several attachments of one message share an asset URL, widgets for the same URL share a player.\n  const { message } = useContext(MessageContext) ?? {};\n  const threadInstance = useThreadContext();\n\n  const audioPlayer = useAudioPlayer({\n    durationSeconds: duration ?? 0,\n    fileSize: file_size,\n    mimeType: mime_type,\n    playbackRates,\n    requester:\n      message?.id &&\n      `${threadInstance ? (message.parent_id ?? message.id) : ''}${message.id}`,\n    // Falls back to the local blob preview while the upload is still in flight.\n    src: getAttachmentPreviewUrl(attachment, asset_url),\n    title,\n    waveformData: waveform_data,\n  });\n\n  return audioPlayer ? (\n    <VoiceRecordingPlayerUI attachment={attachment} audioPlayer={audioPlayer} />\n  ) : null;\n};\n\nexport type QuotedVoiceRecordingProps = Pick<VoiceRecordingProps, 'attachment'>;\n\nexport const QuotedVoiceRecording = ({ attachment }: QuotedVoiceRecordingProps) => {\n  const { FileSizeIndicator = DefaultFileSizeIndicator } = useComponentContext();\n  const { duration, file_size, mime_type } =\n    (attachment as VoiceRecordingAttachment).custom ?? {};\n  return (\n    <div className={rootClassName} data-testid='quoted-voice-recording-widget'>\n      <div className='str-chat__message-attachment__voice-recording-widget__metadata'>\n        <div className='str-chat__message-attachment__voice-recording-widget__audio-state'>\n          <div className='str-chat__message-attachment__voice-recording-widget__timer'>\n            {duration ? (\n              <DurationDisplay\n                duration={duration}\n                isPlaying={false}\n                secondsElapsed={undefined}\n              />\n            ) : (\n              <FileSizeIndicator fileSize={file_size} maximumFractionDigits={0} />\n            )}\n          </div>\n        </div>\n      </div>\n      <FileIcon mimeType={mime_type} />\n    </div>\n  );\n};\n\nexport type VoiceRecordingProps = {\n  /** Voice recording attachment to render. */\n  attachment: Attachment;\n  /** Whether the attachment is in a quoted message, where it renders without a player. */\n  isQuoted?: boolean;\n};\n\nexport const VoiceRecording = ({ attachment, isQuoted }: VoiceRecordingProps) =>\n  isQuoted ? (\n    <QuotedVoiceRecording attachment={attachment} />\n  ) : (\n    <VoiceRecordingPlayer attachment={attachment} />\n  );\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2BA,IAAM,gBAAgB;AAEtB,IAAM,4BAA4B,WAA6B;CAC7D,eAAe,MAAM;CACrB,iBAAiB,MAAM;CACvB,WAAW,MAAM;CACjB,cAAc,MAAM;CACpB,UAAU,MAAM;CAChB,gBAAgB,MAAM;AACxB;AAQA,IAAM,0BAA0B,EAC9B,YACA,kBACiC;CACjC,MAAM,EACJ,mCAAA,sCAAoC,mCACpC,mBAAA,sBAAoB,sBAClB,oBAAoB;CACxB,MAAM,EAAE,gBAAgB,yBAAyB,UAAU;CAC3D,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,EACJ,eACA,iBACA,WACA,cACA,UACA,mBACE,cAAc,aAAa,OAAO,wBAAwB,KAAK,CAAC;CAEpE,OACE,qBAAC,OAAD;EAAK,WAAW;EAAe,eAAY;YAA3C;GACE,oBAAC,OAAD;IAAK,WAAU;cACb,oBAAC,YAAD;KAAY,WAAW,CAAC,CAAC;KAAW,SAAS,YAAY;IAAa,CAAA;GACnE,CAAA;GACL,oBAAC,OAAD;IAAK,WAAU;cACb,qBAAC,OAAD;KAAK,WAAU;eAAf,CACE,oBAAC,OAAD;MAAK,WAAU;gBAMZ,cACC,oBAAC,qCAAD,EAA+C,WAAa,CAAA,IAC1D,kBACF,oBAAC,iBAAD;OACE,UAAU;OACV,WAAW,CAAC,CAAC;OACG;OAChB,eAAA;MACD,CAAA,IAED,oBAAC,qBAAD;OACE,UAAU,YAAY;OACtB,uBAAuB;MACxB,CAAA;KAEA,CAAA,GACL,oBAAC,iBAAD;MACmB;MACP;MACM;MAChB,MAAM,YAAY;MAClB,cAAc,YAAY,gBAAgB,CAAC;KAC5C,CAAA,CACE;;GACF,CAAA;GACL,oBAAC,OAAD;IAAK,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