import React, { ReactNode, useCallback, useEffect, useState } from 'react'; import { Badge, Box, Button, Icon, Icons, Modal, Overlay, OverlayBackdrop, OverlayCenter, Spinner, Text, Tooltip, TooltipProvider, as, } from 'folds'; import classNames from 'classnames'; import { BlurhashCanvas } from 'react-blurhash'; import FocusTrap from 'focus-trap-react'; import { EncryptedAttachmentInfo } from 'browser-encrypt-attachment'; import { IImageInfo, MATRIX_BLUR_HASH_PROPERTY_NAME } from '../../../../types/matrix/common'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { useMatrixClient } from '../../../hooks/useMatrixClient'; import * as css from './style.css'; import { bytesToSize } from '../../../utils/common'; import { FALLBACK_MIMETYPE } from '../../../utils/mimeTypes'; import { stopPropagation } from '../../../utils/keyboard'; import { decryptFile, downloadEncryptedMedia, mxcUrlToHttp } from '../../../utils/matrix'; import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication'; type RenderViewerProps = { src: string; alt: string; requestClose: () => void; }; type RenderImageProps = { alt: string; title: string; src: string; onLoad: () => void; onError: () => void; onClick: () => void; tabIndex: number; }; export type ImageContentProps = { body: string; mimeType?: string; url: string; info?: IImageInfo; encInfo?: EncryptedAttachmentInfo; autoPlay?: boolean; renderViewer: (props: RenderViewerProps) => ReactNode; renderImage: (props: RenderImageProps) => ReactNode; }; export const ImageContent = as<'div', ImageContentProps>( ( { className, body, mimeType, url, info, encInfo, autoPlay, renderViewer, renderImage, ...props }, ref ) => { const mx = useMatrixClient(); const useAuthentication = useMediaAuthentication(); const blurHash = info?.[MATRIX_BLUR_HASH_PROPERTY_NAME]; const [load, setLoad] = useState(false); const [error, setError] = useState(false); const [viewer, setViewer] = useState(false); const [srcState, loadSrc] = useAsyncCallback( useCallback(async () => { const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication) ?? url; if (encInfo) { const fileContent = await downloadEncryptedMedia(mediaUrl, (encBuf) => decryptFile(encBuf, mimeType ?? FALLBACK_MIMETYPE, encInfo) ); return URL.createObjectURL(fileContent); } return mediaUrl; }, [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 ( {srcState.status === AsyncStatus.Success && ( }> setViewer(false), clickOutsideDeactivates: true, escapeDeactivates: stopPropagation, }} > evt.stopPropagation()} > {renderViewer({ src: srcState.data, alt: body, requestClose: () => setViewer(false), })} )} {typeof blurHash === 'string' && !load && ( )} {!autoPlay && srcState.status === AsyncStatus.Idle && ( )} {srcState.status === AsyncStatus.Success && ( {renderImage({ alt: body, title: body, src: srcState.data, onLoad: handleLoad, onError: handleError, onClick: () => setViewer(true), tabIndex: 0, })} )} {(srcState.status === AsyncStatus.Loading || srcState.status === AsyncStatus.Success) && !load && ( )} {(error || srcState.status === AsyncStatus.Error) && ( Failed to load image! } position="Top" align="Center" > {(triggerRef) => ( )} )} {!load && typeof info?.size === 'number' && ( {bytesToSize(info.size)} )} ); } );