import { DownloadStatus, useAttachment } from "@/app/providers/AttachmentProvider/useAttachment"; import { AttachmentProps } from "./MessageAttachments"; import { useRosettaColors } from "@/app/hooks/useRosettaColors"; import { Avatar, Box, Flex, Loader, Text } from "@mantine/core"; import { IconArrowDown, IconFile, IconX } from "@tabler/icons-react"; import { dotCenterIfNeeded, humanFilesize } from "@/app/utils/utils"; import { AnimatedRoundedProgress } from "../AnimatedRoundedProgress/AnimatedRoundedProgress"; import { DeliveredMessageState } from "@/app/providers/DialogProvider/DialogProvider"; export function MessageFile(props : AttachmentProps) { const colors = useRosettaColors(); const { downloadPercentage, downloadStatus, uploadedPercentage, download, getPreview, } = useAttachment( props.attachment, props.chacha_key_plain, ); const preview = getPreview(); const error = downloadStatus == DownloadStatus.ERROR; const filesize = parseInt(preview.split("::")[0]); const filename = preview.split("::")[1]; const filetype = filename.split(".")[filename.split(".").length - 1]; const isEncrypting = props.delivered == DeliveredMessageState.WAITING && uploadedPercentage <= 0; const isUploading = props.delivered == DeliveredMessageState.WAITING && uploadedPercentage > 0 && uploadedPercentage < 100; const onClick = async () => { if(downloadStatus == DownloadStatus.ERROR){ return; } if(downloadStatus == DownloadStatus.DOWNLOADED){ //let content = await getBlob(); //let buffer = Buffer.from(content.split(",")[1], 'base64'); let pathInDownloads = window.downloadsPath + "/Rosetta Downloads/" + filename; //await writeFile(pathInDownloads, buffer, false); window.shell.showItemInFolder(pathInDownloads); return; } if(downloadStatus == DownloadStatus.NOT_DOWNLOADED){ download(); return; } } return ( {!error && <> {(downloadStatus == DownloadStatus.DOWNLOADING && downloadPercentage > 0 && downloadPercentage < 100) && (
)} {downloadStatus != DownloadStatus.DOWNLOADED && ( )} {isUploading && (
)} {downloadStatus == DownloadStatus.DOWNLOADED && } } {error && <> }
{dotCenterIfNeeded(filename, 25, 8)} {!error && !isEncrypting && !isUploading && (downloadStatus == DownloadStatus.DOWNLOADED || downloadStatus == DownloadStatus.NOT_DOWNLOADED) && {humanFilesize(filesize)} {filetype.toUpperCase()} } {downloadStatus == DownloadStatus.DOWNLOADING && Downloading... {downloadPercentage}% } {isEncrypting && Encrypting... } {isUploading && Uploading... {uploadedPercentage}% } {downloadStatus == DownloadStatus.DECRYPTING && Decrypting... } {error && File expired }
) }