import type React from 'react'; import { IconCloudDownload } from '@tencentcloud/uikit-base-component-react'; import cs from 'classnames'; import { View } from 'tuikit-atomicx-react/chat'; import classes from './FileMessage.module.scss'; import type { MessageInfo, FileMessagePayload } from 'tuikit-atomicx-react/chat'; interface FileMessageProps { message: MessageInfo; } function formatFileSize(bytes: number | undefined): string { if (!bytes || bytes <= 0) { return ''; } const MB = 1024 * 1024; if (bytes >= MB) { return `${(bytes / MB).toFixed(1)} MB`; } return `${(bytes / 1024).toFixed(1)} KB`; } function FileMessage(props: FileMessageProps) { const { message } = props; const payload = message.messagePayload as FileMessagePayload; const messageContent = { name: payload.fileName, size: formatFileSize(payload.fileSize), url: payload.fileURL, }; const handleFileClick = async (event: React.MouseEvent) => { if (event.metaKey || event.ctrlKey) { window.open(messageContent.url, '_blank'); } else { try { event.preventDefault(); const response = await fetch(messageContent.url ?? ''); if (!response.ok) { throw new Error('Download failed'); } const blob = await response.blob(); const blobUrl = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = blobUrl; link.download = messageContent.name || ''; link.click(); window.URL.revokeObjectURL(blobUrl); } catch (error) { console.error('Download error:', error); } } }; return ( {messageContent.name} {messageContent.size} ); } function FileIcon() { return ( ); } export default FileMessage;