// Copyright 2018 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useCallback, useMemo } from 'react'; import classNames from 'classnames'; import { Blurhash } from 'react-blurhash'; import { Spinner } from '../Spinner'; import type { LocalizerType, ThemeType } from '../../types/Util'; import type { AttachmentType } from '../../types/Attachment'; import { isDownloaded as isDownloadedFunction, defaultBlurHash, } from '../../types/Attachment'; export enum CurveType { None = 0, Tiny = 4, Small = 10, Normal = 18, } export type Props = { alt: string; attachment: AttachmentType; url?: string; isDownloaded?: boolean; className?: string; height?: number; width?: number; cropWidth?: number; cropHeight?: number; tabIndex?: number; overlayText?: string; noBorder?: boolean; noBackground?: boolean; bottomOverlay?: boolean; closeButton?: boolean; curveBottomLeft?: CurveType; curveBottomRight?: CurveType; curveTopLeft?: CurveType; curveTopRight?: CurveType; darkOverlay?: boolean; playIconOverlay?: boolean; blurHash?: string; i18n: LocalizerType; theme?: ThemeType; onClick?: (attachment: AttachmentType) => void; onClickClose?: (attachment: AttachmentType) => void; onError?: () => void; }; export function Image({ alt, attachment, blurHash, bottomOverlay, className, closeButton, curveBottomLeft, curveBottomRight, curveTopLeft, curveTopRight, darkOverlay, isDownloaded, height = 0, i18n, noBackground, noBorder, onClick, onClickClose, onError, overlayText, playIconOverlay, tabIndex, theme, url, width = 0, cropWidth = 0, cropHeight = 0, }: Props): JSX.Element { const { caption, pending } = attachment || { caption: null, pending: true }; const imgNotDownloaded = isDownloaded ? false : !isDownloadedFunction(attachment); const resolvedBlurHash = blurHash || defaultBlurHash(theme); const curveStyles = { borderTopLeftRadius: curveTopLeft || CurveType.None, borderTopRightRadius: curveTopRight || CurveType.None, borderBottomLeftRadius: curveBottomLeft || CurveType.None, borderBottomRightRadius: curveBottomRight || CurveType.None, }; const canClick = useMemo(() => { return onClick != null && !pending; }, [pending, onClick]); const handleClick = useCallback( (event: React.MouseEvent) => { if (!canClick) { event.preventDefault(); event.stopPropagation(); return; } if (onClick) { event.preventDefault(); event.stopPropagation(); onClick(attachment); } }, [attachment, canClick, onClick] ); const handleKeyDown = useCallback( (event: React.KeyboardEvent) => { if (!canClick) { event.preventDefault(); event.stopPropagation(); return; } if (onClick && (event.key === 'Enter' || event.key === 'Space')) { event.preventDefault(); event.stopPropagation(); onClick(attachment); } }, [attachment, canClick, onClick] ); /* eslint-disable no-nested-ternary */ return (
{pending ? ( blurHash ? (
) : (
) ) : url ? ( {alt} ) : resolvedBlurHash ? ( ) : null} {caption ? ( {i18n('icu:imageCaptionIconAlt')} ) : null} {bottomOverlay ? (
) : null} {!pending && !imgNotDownloaded && playIconOverlay ? (
) : null} {overlayText ? (
{overlayText}
) : null} {canClick ? ( ) : null} {closeButton ? (
); /* eslint-enable no-nested-ternary */ }