From 94da63cc8621fad12f59ee6dd0aeb72c84b3f017 Mon Sep 17 00:00:00 2001 From: Anirban Singha Date: Wed, 8 Jan 2025 22:00:52 +0530 Subject: [PATCH] fix: resolve mention highlighting issue for "application" and "text" file types and improve design --- .../src/views/AttachmentHandler/Attachment.js | 32 +-- .../views/AttachmentHandler/TextAttachment.js | 227 +++++++++--------- .../Message/BubbleVariant/Bubble.styles.js | 5 + 3 files changed, 133 insertions(+), 131 deletions(-) diff --git a/packages/react/src/views/AttachmentHandler/Attachment.js b/packages/react/src/views/AttachmentHandler/Attachment.js index ec752e9bc..20c329138 100644 --- a/packages/react/src/views/AttachmentHandler/Attachment.js +++ b/packages/react/src/views/AttachmentHandler/Attachment.js @@ -1,11 +1,9 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { css } from '@emotion/react'; -import { Box, Icon } from '@embeddedchat/ui-elements'; import ImageAttachment from './ImageAttachment'; import AudioAttachment from './AudioAttachment'; import VideoAttachment from './VideoAttachment'; -import TextAttachment from './TextAttachment'; +import FileAttachment from './TextAttachment'; const Attachment = ({ attachment, host, type, variantStyles = {}, msg }) => { const author = { @@ -45,16 +43,6 @@ const Attachment = ({ attachment, host, type, variantStyles = {}, msg }) => { /> ); } - if (attachment && attachment.text) { - return ( - - ); - } if ( attachment.attachments && Array.isArray(attachment.attachments) && @@ -101,16 +89,14 @@ const Attachment = ({ attachment, host, type, variantStyles = {}, msg }) => { ); } return ( - - {attachment?.description} - - - {attachment.title} - + ); }; diff --git a/packages/react/src/views/AttachmentHandler/TextAttachment.js b/packages/react/src/views/AttachmentHandler/TextAttachment.js index bc79b8c5c..bb76a4202 100644 --- a/packages/react/src/views/AttachmentHandler/TextAttachment.js +++ b/packages/react/src/views/AttachmentHandler/TextAttachment.js @@ -1,145 +1,156 @@ -import React, { useContext } from 'react'; +import React, { useState, useContext } from 'react'; import { css } from '@emotion/react'; import PropTypes from 'prop-types'; -import { Box, Avatar, useTheme } from '@embeddedchat/ui-elements'; +import { Box, Avatar, useTheme, Icon } from '@embeddedchat/ui-elements'; +import AttachmentMetadata from './AttachmentMetadata'; import RCContext from '../../context/RCInstance'; -const TextAttachment = ({ attachment, type, variantStyles = {} }) => { +const FileAttachment = ({ + attachment, + host, + type, + author, + variantStyles = {}, + msg, +}) => { const { RCInstance } = useContext(RCContext); - const getUserAvatarUrl = (authorIcon) => { - const host = RCInstance.getHost(); - const URL = `${host}${authorIcon}`; - return URL; + const { theme } = useTheme(); + const [isExpanded, setIsExpanded] = useState(true); + + const getUserAvatarUrl = (icon) => { + const instanceHost = RCInstance.getHost(); + return `${instanceHost}${icon}`; }; - const { theme } = useTheme(); + const { authorIcon, authorName } = author; + + const toggleExpanded = () => { + setIsExpanded((prevState) => !prevState); + }; + + const formatFileSize = (bytes) => { + if (!bytes || bytes === 0) return '0 B'; + + const units = ['B', 'KB', 'MB', 'GB']; + const k = 1024; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + const unitIndex = Math.min(i, units.length - 1); + + const size = bytes / k ** unitIndex; + const decimals = unitIndex === 0 ? 0 : unitIndex === 1 ? 1 : 2; + + return `${size.toFixed(decimals)} ${units[unitIndex]}`; + }; + + const getFileSizeWithFormat = (size, format) => { + const formattedSize = formatFileSize(size); + return format ? `${formattedSize} - ${format}` : formattedSize; + }; return ( - + - {attachment?.author_name && ( - <> + {type === 'file' && ( + - @{attachment?.author_name} - + @{authorName} + )} - - - {attachment?.text - ? attachment.text[0] === '[' - ? attachment.text.match(/\n(.*)/)?.[1] || '' - : attachment.text - : ''} - {attachment?.attachments && - attachment.attachments.map((nestedAttachment, index) => ( + + {isExpanded && ( + + - - {nestedAttachment?.author_name && ( - <> - - @{nestedAttachment?.author_name} - - )} - + {attachment.title} + - {nestedAttachment?.text - ? nestedAttachment.text[0] === '[' - ? nestedAttachment.text.match(/\n(.*)/)?.[1] || '' - : nestedAttachment.text - : ''} + {getFileSizeWithFormat(attachment.size, attachment.format)} - ))} + + )} ); }; -export default TextAttachment; +export default FileAttachment; -TextAttachment.propTypes = { +FileAttachment.propTypes = { attachment: PropTypes.object, + host: PropTypes.string, + type: PropTypes.string, + author: PropTypes.object, + variantStyles: PropTypes.object, + msg: PropTypes.object, }; diff --git a/packages/react/src/views/Message/BubbleVariant/Bubble.styles.js b/packages/react/src/views/Message/BubbleVariant/Bubble.styles.js index 5f87c0b5e..313869260 100644 --- a/packages/react/src/views/Message/BubbleVariant/Bubble.styles.js +++ b/packages/react/src/views/Message/BubbleVariant/Bubble.styles.js @@ -93,6 +93,11 @@ export const getBubbleStyles = (theme) => { border-radius: inherit; overflow: hidden; `, + fileAttachmentContainer: css` + border: 1px solid ${theme.colors.border}; + border-radius: inherit; + overflow: hidden; + `, pinnedContainer: css` max-width: 100%; `,