import React from 'react'; import classNames from 'classnames'; import { Spinner } from '../Spinner'; import { LocalizerType } from '../../types/Util'; import { AttachmentType } from '../../types/Attachment'; import { useEncryptedFileFetch } from '../../hooks/useEncryptedFileFetch'; import { fromArrayBufferToBase64 } from '../../session/utils/String'; import toArrayBuffer from 'to-arraybuffer'; type Props = { alt: string; attachment: AttachmentType; url: string; height?: number; width?: number; overlayText?: string; bottomOverlay?: boolean; closeButton?: boolean; curveBottomLeft?: boolean; curveBottomRight?: boolean; curveTopLeft?: boolean; curveTopRight?: boolean; smallCurveTopLeft?: boolean; darkOverlay?: boolean; playIconOverlay?: boolean; softCorners?: boolean; i18n: LocalizerType; onClick?: (attachment: AttachmentType) => void; onClickClose?: (attachment: AttachmentType) => void; onError?: () => void; }; export const Image = (props: Props) => { // tslint:disable-next-line max-func-body-length cyclomatic-complexity const { alt, attachment, bottomOverlay, closeButton, curveBottomLeft, curveBottomRight, curveTopLeft, curveTopRight, darkOverlay, height, i18n, onClick, onClickClose, onError, overlayText, playIconOverlay, smallCurveTopLeft, softCorners, url, width, } = props; const { caption, pending } = attachment || { caption: null, pending: true }; const canClick = onClick && !pending; const role = canClick ? 'button' : undefined; const { loading, data } = useEncryptedFileFetch(url); //FIXME jpg is hardcoded const srcData = !loading ? data?.length ? `data:image/jpg;base64,${fromArrayBufferToBase64(toArrayBuffer(data))}` : url : ''; return (
{ if (canClick && onClick) { e.stopPropagation(); onClick(attachment); } }} className={classNames( 'module-image', canClick ? 'module-image__with-click-handler' : null, curveBottomLeft ? 'module-image--curved-bottom-left' : null, curveBottomRight ? 'module-image--curved-bottom-right' : null, curveTopLeft ? 'module-image--curved-top-left' : null, curveTopRight ? 'module-image--curved-top-right' : null, smallCurveTopLeft ? 'module-image--small-curved-top-left' : null, softCorners ? 'module-image--soft-corners' : null )} > {pending || loading ? (
) : ( {alt} )} {caption ? ( {i18n('imageCaptionIconAlt')} ) : null}
{closeButton ? (
{ e.stopPropagation(); if (onClickClose) { onClickClose(attachment); } }} className="module-image__close-button" /> ) : null} {bottomOverlay ? (
) : null} {!(pending || loading) && playIconOverlay ? (
) : null} {overlayText ? (
{overlayText}
) : null}
); };