{"version":3,"file":"WithAudioPlayback.mjs","names":[],"sources":["../../../../src/components/AudioPlayback/WithAudioPlayback.tsx"],"sourcesContent":["import React, { useContext, useEffect, useState } from 'react';\nimport type { AudioPlayerOptions } from './AudioPlayer';\nimport type { AudioPlayerPoolState } from './AudioPlayerPool';\nimport { AudioPlayerPool } from './AudioPlayerPool';\nimport { AudioPlaybackArbiter } from './AudioPlaybackArbiter';\nimport { audioPlayerNotificationsPluginFactory } from './plugins/AudioPlayerNotificationsPlugin';\nimport { useNotificationApi, useNotificationTarget } from '../Notifications';\nimport { useTranslationContext } from '../../context';\nimport { useStateStore } from '../../store';\nimport { useAudioPlaybackChangeAnnouncements } from '../Accessibility/hooks/useAudioPlaybackChangeAnnouncements';\n\nexport type WithAudioPlaybackProps = {\n  children?: React.ReactNode;\n  /**\n   * What the playback belongs to. When it changes, every player this provider started is stopped\n   * and discarded. Needed because a provider is not always unmounted when its subject changes\n   * (`Channel` stays mounted across a channel switch).\n   */\n  playbackScope?: unknown;\n};\n\nconst AudioPlayerContext = React.createContext<{ audioPlayers: AudioPlayerPool | null }>({\n  audioPlayers: null,\n});\n\n/**\n * The arbiter every pool below shares, so no two surfaces play at once. `Chat` provides one; a\n * `WithAudioPlayback` mounted outside a `Chat` falls back to a pool that arbitrates alone.\n */\nconst SharedAudioPlaybackContext = React.createContext<AudioPlaybackArbiter | null>(null);\n\nexport const SharedAudioPlaybackProvider = ({\n  children,\n}: {\n  children?: React.ReactNode;\n}) => {\n  const [arbiter] = useState(() => new AudioPlaybackArbiter());\n  return (\n    <SharedAudioPlaybackContext.Provider value={arbiter}>\n      {children}\n    </SharedAudioPlaybackContext.Provider>\n  );\n};\n\nexport const WithAudioPlayback = ({\n  children,\n  playbackScope,\n}: WithAudioPlaybackProps) => {\n  const arbiter = useContext(SharedAudioPlaybackContext);\n  const [audioPlayers] = useState(\n    () => new AudioPlayerPool({ arbiter: arbiter ?? undefined }),\n  );\n\n  // Runs on unmount and whenever the scope changes: the players belong to the scope, not to this\n  // provider's lifetime.\n  useEffect(\n    () => () => {\n      audioPlayers.clear();\n    },\n    [audioPlayers, playbackScope],\n  );\n\n  return (\n    <AudioPlayerContext.Provider value={{ audioPlayers }}>\n      {children}\n    </AudioPlayerContext.Provider>\n  );\n};\n\nexport type UseAudioPlayerProps = {\n  /**\n   * Entity that requested the playback, e.g. a message ID. The player ID is `requester:src`,\n   * because a URL can back several UI elements and an element can show several sources. Requests\n   * with the same requester and URL (a message with duplicate attachments) share one player.\n   */\n  requester?: string;\n} & Partial<Omit<AudioPlayerOptions, 'id' | 'pool'>>;\n\nconst makeAudioPlayerId = ({ requester, src }: { src: string; requester?: string }) =>\n  `${requester ?? 'requester-unknown'}:${src}`;\n\nexport const useAudioPlayer = ({\n  durationSeconds,\n  fileSize,\n  mimeType,\n  playbackRates,\n  plugins,\n  requester = '',\n  src,\n  title,\n  waveformData,\n}: UseAudioPlayerProps) => {\n  const { addNotification } = useNotificationApi();\n  const panel = useNotificationTarget();\n  const { t } = useTranslationContext();\n  const { audioPlayers } = useContext(AudioPlayerContext);\n\n  const audioPlayer =\n    src && audioPlayers\n      ? audioPlayers.getOrAdd({\n          durationSeconds,\n          fileSize,\n          id: makeAudioPlayerId({ requester, src }),\n          mimeType,\n          playbackRates,\n          plugins,\n          src,\n          title,\n          waveformData,\n        })\n      : undefined;\n\n  useAudioPlaybackChangeAnnouncements(audioPlayer);\n\n  useEffect(() => {\n    if (!audioPlayer) return;\n    // A plugin produces the translated notifications, so AudioPlayer needs neither the client nor\n    // the translation function.\n    const notificationsPlugin = audioPlayerNotificationsPluginFactory({\n      addNotification,\n      panel,\n      t,\n    });\n    audioPlayer.setPlugins((currentPlugins) => [\n      ...currentPlugins.filter((plugin) => plugin.id !== notificationsPlugin.id),\n      notificationsPlugin,\n    ]);\n  }, [addNotification, audioPlayer, panel, t]);\n\n  return audioPlayer;\n};\n\nconst activeAudioPlayerSelector = ({ activeAudioPlayer }: AudioPlayerPoolState) => ({\n  activeAudioPlayer,\n});\n\nexport const useActiveAudioPlayer = () => {\n  const { audioPlayers } = useContext(AudioPlayerContext);\n  const { activeAudioPlayer } =\n    useStateStore(audioPlayers?.state, activeAudioPlayerSelector) ?? {};\n  return activeAudioPlayer;\n};\n"],"mappings":";;;;;;;;;;;AAqBA,IAAM,qBAAqB,MAAM,cAAwD,EACvF,cAAc,KAChB,CAAC;;;;;AAMD,IAAM,6BAA6B,MAAM,cAA2C,IAAI;AAExF,IAAa,+BAA+B,EAC1C,eAGI;CACJ,MAAM,CAAC,WAAW,eAAe,IAAI,qBAAqB,CAAC;CAC3D,OACE,oBAAC,2BAA2B,UAA5B;EAAqC,OAAO;EACzC;CACkC,CAAA;AAEzC;AAEA,IAAa,qBAAqB,EAChC,UACA,oBAC4B;CAC5B,MAAM,UAAU,WAAW,0BAA0B;CACrD,MAAM,CAAC,gBAAgB,eACf,IAAI,gBAAgB,EAAE,SAAS,WAAW,KAAA,EAAU,CAAC,CAC7D;CAIA,sBACc;EACV,aAAa,MAAM;CACrB,GACA,CAAC,cAAc,aAAa,CAC9B;CAEA,OACE,oBAAC,mBAAmB,UAApB;EAA6B,OAAO,EAAE,aAAa;EAChD;CAC0B,CAAA;AAEjC;AAWA,IAAM,qBAAqB,EAAE,WAAW,UACtC,GAAG,aAAa,oBAAoB,GAAG;AAEzC,IAAa,kBAAkB,EAC7B,iBACA,UACA,UACA,eACA,SACA,YAAY,IACZ,KACA,OACA,mBACyB;CACzB,MAAM,EAAE,oBAAoB,mBAAmB;CAC/C,MAAM,QAAQ,sBAAsB;CACpC,MAAM,EAAE,MAAM,sBAAsB;CACpC,MAAM,EAAE,iBAAiB,WAAW,kBAAkB;CAEtD,MAAM,cACJ,OAAO,eACH,aAAa,SAAS;EACpB;EACA;EACA,IAAI,kBAAkB;GAAE;GAAW;EAAI,CAAC;EACxC;EACA;EACA;EACA;EACA;EACA;CACF,CAAC,IACD,KAAA;CAEN,oCAAoC,WAAW;CAE/C,gBAAgB;EACd,IAAI,CAAC,a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