import { useRosettaColors } from "@/app/hooks/useRosettaColors"; import { DeliveredMessageState } from "@/app/providers/DialogProvider/DialogProvider"; import { useImageViewer } from "@/app/providers/ImageViewerProvider/useImageViewer"; import { AspectRatio, Box, Flex, Overlay, Portal, Text } from "@mantine/core"; import { IconArrowDown, IconCircleX, IconFlameFilled } from "@tabler/icons-react"; import { useEffect, useRef, useState } from "react"; import { AttachmentProps } from "./MessageAttachments"; import { blurhashToBase64Image, isMessageDeliveredByTime } from "@/app/utils/utils"; import { AnimatedRoundedProgress } from "../AnimatedRoundedProgress/AnimatedRoundedProgress"; import { ImageToView } from "@/app/providers/ImageViewerProvider/ImageViewerProvider"; import { DownloadStatus, useAttachment } from "@/app/providers/AttachmentProvider/useAttachment"; export function MessageImage(props: AttachmentProps) { const colors = useRosettaColors(); const { downloadPercentage, uploadedPercentage, download, downloadStatus, getBlob, getPreview} = useAttachment(props.attachment, props.chacha_key_plain); const mainRef = useRef(null); const error = downloadStatus == DownloadStatus.ERROR; const { open } = useImageViewer(); const preview = getPreview(); const [blob, setBlob] = useState(props.attachment.blob); const [loadedImage, setLoadedImage] = useState(false); useEffect(() => { constructBlob(); }, [downloadStatus]); const constructBlob = async () => { let blob = await getBlob(); setBlob(blob); } const openImageViewer = () => { const images: ImageToView[] = [{ src: blob, caption: props.text, timestamp: props.timestamp }]; open(images, 0); } const onClick = () => { if (downloadStatus == DownloadStatus.DOWNLOADED) { openImageViewer(); return; } if (downloadStatus == DownloadStatus.NOT_DOWNLOADED) { download(); return; } } return ( {blob != "" && ( setLoadedImage(true)}>)} {((downloadStatus != DownloadStatus.DOWNLOADED && downloadStatus != DownloadStatus.PENDING) || !loadedImage) && preview.length >= 20 && ( <> {!error && ( {downloadPercentage > 0 ? ( ) : ( )} )} {error && ( Image expired )} )} {(props.delivered == DeliveredMessageState.WAITING && uploadedPercentage > 0 && isMessageDeliveredByTime(props.timestamp || 0, props.attachments.length)) && 95 ? 95 : uploadedPercentage}> } {(props.delivered == DeliveredMessageState.ERROR || (props.delivered != DeliveredMessageState.DELIVERED && !isMessageDeliveredByTime(props.timestamp || 0, props.attachments.length) )) && ( )} ); }