diff --git a/web/media/encrypted-multimedia.react.js b/web/media/encrypted-multimedia.react.js index 223bb697d..cf83b2bf7 100644 --- a/web/media/encrypted-multimedia.react.js +++ b/web/media/encrypted-multimedia.react.js @@ -1,143 +1,145 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; import 'react-circular-progressbar/dist/styles.css'; import { AlertCircle as AlertCircleIcon } from 'react-feather'; import type { EncryptedMediaType } from 'lib/types/media-types.js'; -import { fetchAndDecryptMedia } from './encryption-utils.js'; +import { useFetchAndDecryptMedia } from './encryption-utils.js'; import LoadableVideo from './loadable-video.react.js'; import css from './media.css'; import LoadingIndicator from '../loading-indicator.react.js'; import type { CSSStyle } from '../types/styles'; type Props = { +blobURI: string, +encryptionKey: string, +type: EncryptedMediaType, +thumbnailBlobURI?: ?string, +thumbnailEncryptionKey?: ?string, +placeholderSrc?: ?string, +multimediaClassName?: string, +elementStyle?: ?Partial, // if provided, this component will be shown instead of the loading indicator +loadingIndicatorComponent?: React.Node, // if true, the loading indicator will not be shown +invisibleLoad?: boolean, }; type Source = { +uri: string } | { +error: string }; function EncryptedMultimedia(props: Props): React.Node { const { blobURI, encryptionKey, placeholderSrc, elementStyle, multimediaClassName, invisibleLoad, } = props; const [source, setSource] = React.useState(null); const videoRef = React.useRef(null); + const fetchAndDecryptMedia = useFetchAndDecryptMedia(); + React.useEffect(() => { let isMounted = true, uriToDispose; setSource(null); const loadDecrypted = async () => { const { result } = await fetchAndDecryptMedia(blobURI, encryptionKey); if (!isMounted) { return; } if (result.success) { const { uri } = result; setSource({ uri }); uriToDispose = uri; } else { setSource({ error: result.reason }); } }; void loadDecrypted(); return () => { isMounted = false; if (uriToDispose) { URL.revokeObjectURL(uriToDispose); } }; - }, [blobURI, encryptionKey]); + }, [blobURI, encryptionKey, fetchAndDecryptMedia]); // we need to update the video source when the source changes // because re-rendering the element wouldn't reload parent