import React, { ReactNode, useCallback, useEffect, useState } from 'react'; import { Badge, Box, Button, Chip, Icon, Icons, Spinner, Text, Tooltip, TooltipProvider, as, } from 'folds'; import classNames from 'classnames'; import { BlurhashCanvas } from 'react-blurhash'; import { EncryptedAttachmentInfo } from 'browser-encrypt-attachment'; import { IThumbnailContent, IVideoInfo, MATRIX_BLUR_HASH_PROPERTY_NAME, } from '../../../../types/matrix/common'; import * as css from './style.css'; import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { bytesToSize, millisecondsToMinutesAndSeconds } from '../../../utils/common'; import { decryptFile, downloadEncryptedMedia, downloadMedia, mxcUrlToHttp, } from '../../../utils/matrix'; import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication'; import { validBlurHash } from '../../../utils/blurHash'; type RenderVideoProps = { title: string; src: string; onLoadedMetadata: () => void; onError: () => void; autoPlay: boolean; controls: boolean; }; type VideoContentProps = { body: string; mimeType: string; url: string; info: IVideoInfo & IThumbnailContent; encInfo?: EncryptedAttachmentInfo; autoPlay?: boolean; markedAsSpoiler?: boolean; spoilerReason?: string; renderThumbnail?: () => ReactNode; renderVideo: (props: RenderVideoProps) => ReactNode; }; export const VideoContent = as<'div', VideoContentProps>( ( { className, body, mimeType, url, info, encInfo, autoPlay, markedAsSpoiler, spoilerReason, renderThumbnail, renderVideo, ...props }, ref ) => { const mx = useMatrixClient(); const useAuthentication = useMediaAuthentication(); const blurHash = validBlurHash(info.thumbnail_info?.[MATRIX_BLUR_HASH_PROPERTY_NAME]); const [load, setLoad] = useState(false); const [error, setError] = useState(false); const [blurred, setBlurred] = useState(markedAsSpoiler ?? false); const [srcState, loadSrc] = useAsyncCallback( useCallback(async () => { const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication) ?? url; const fileContent = encInfo ? await downloadEncryptedMedia(mediaUrl, (encBuf) => decryptFile(encBuf, mimeType, encInfo) ) : await downloadMedia(mediaUrl); return URL.createObjectURL(fileContent); }, [mx, url, useAuthentication, mimeType, encInfo]) ); const handleLoad = () => { setLoad(true); }; const handleError = () => { setLoad(false); setError(true); }; const handleRetry = () => { setError(false); loadSrc(); }; useEffect(() => { if (autoPlay) loadSrc(); }, [autoPlay, loadSrc]); return ( {typeof blurHash === 'string' && !load && ( )} {renderThumbnail && !load && ( {renderThumbnail()} )} {!autoPlay && !blurred && srcState.status === AsyncStatus.Idle && ( )} {srcState.status === AsyncStatus.Success && ( {renderVideo({ title: body, src: srcState.data, onLoadedMetadata: handleLoad, onError: handleError, autoPlay: true, controls: true, })} )} {blurred && !error && srcState.status !== AsyncStatus.Error && ( {spoilerReason} ) } position="Top" align="Center" > {(triggerRef) => ( { setBlurred(false); }} > Spoiler )} )} {(srcState.status === AsyncStatus.Loading || srcState.status === AsyncStatus.Success) && !load && !blurred && ( )} {(error || srcState.status === AsyncStatus.Error) && ( Failed to load video! } position="Top" align="Center" > {(triggerRef) => ( )} )} {!load && typeof info.size === 'number' && ( {millisecondsToMinutesAndSeconds(info.duration ?? 0)} {bytesToSize(info.size)} )} ); } );