Skip to content

Commit 04acec7

Browse files
fix: resolve mention highlighting issue for "application" and "text" file types and improve design (#836)
* fix: resolve mention highlighting issue for "application" and "text" file types and improve design * fix: ensure mentioned users in file description appear in mentions list * fix: ensure user mentions in files are not gloabalized * fix download icon alignment and quoting issues --------- Co-authored-by: Zishan Ahmad <zishan.barun@gmail.com>
1 parent f555531 commit 04acec7

7 files changed

Lines changed: 350 additions & 225 deletions

File tree

packages/react/src/views/AttachmentHandler/Attachment.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { css } from '@emotion/react';
4-
import { Box, Icon } from '@embeddedchat/ui-elements';
53
import ImageAttachment from './ImageAttachment';
64
import AudioAttachment from './AudioAttachment';
75
import VideoAttachment from './VideoAttachment';
8-
import TextAttachment from './TextAttachment';
6+
import FileAttachment from './TextAttachment';
97

108
const Attachment = ({ attachment, host, type, variantStyles = {}, msg }) => {
119
const author = {
@@ -45,16 +43,6 @@ const Attachment = ({ attachment, host, type, variantStyles = {}, msg }) => {
4543
/>
4644
);
4745
}
48-
if (attachment && attachment.text) {
49-
return (
50-
<TextAttachment
51-
attachment={attachment}
52-
type={type}
53-
author={author}
54-
variantStyles={variantStyles}
55-
/>
56-
);
57-
}
5846
if (
5947
attachment.attachments &&
6048
Array.isArray(attachment.attachments) &&
@@ -101,16 +89,14 @@ const Attachment = ({ attachment, host, type, variantStyles = {}, msg }) => {
10189
);
10290
}
10391
return (
104-
<Box
105-
css={css`
106-
display: flex;
107-
`}
108-
>
109-
{attachment?.description}
110-
111-
<Icon name="file" size="20px" />
112-
<a href={`${host}${attachment.title_link}`}>{attachment.title}</a>
113-
</Box>
92+
<FileAttachment
93+
attachment={attachment}
94+
type={type}
95+
host={host}
96+
msg={msg}
97+
author={author}
98+
variantStyles={variantStyles}
99+
/>
114100
);
115101
};
116102

packages/react/src/views/AttachmentHandler/AttachmentMetadata.js

Lines changed: 74 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const AttachmentMetadata = ({
1818
const downloadUrl = URL.createObjectURL(data);
1919
const anchor = document.createElement('a');
2020
anchor.href = downloadUrl;
21-
anchor.download = attachment.title || 'download';
21+
anchor.download = attachment?.title || 'download';
2222

2323
document.body.appendChild(anchor);
2424
anchor.click();
@@ -29,6 +29,25 @@ const AttachmentMetadata = ({
2929
}
3030
};
3131

32+
const getFormattedFileSize = () => {
33+
let sizeInBytes;
34+
35+
if (attachment?.image_type && attachment?.image_size) {
36+
sizeInBytes = attachment.image_size;
37+
} else if (attachment?.video_type && attachment?.video_size) {
38+
sizeInBytes = attachment.video_size;
39+
} else if (attachment?.audio_type && attachment?.audio_size) {
40+
sizeInBytes = attachment.audio_size;
41+
} else if (attachment?.size) {
42+
sizeInBytes = attachment.size;
43+
} else {
44+
sizeInBytes = 0;
45+
}
46+
47+
const sizeInKB = (sizeInBytes / 1024).toFixed(2);
48+
return `${sizeInKB} kB`;
49+
};
50+
3251
return (
3352
<Box
3453
css={[
@@ -43,36 +62,29 @@ const AttachmentMetadata = ({
4362
variantStyles.attachmentMetaContainer,
4463
]}
4564
>
46-
<div
47-
css={
48-
attachment.description !== ''
49-
? [
50-
css`
51-
margin: 10px 0px;
52-
@media (max-width: 420px) {
53-
margin: 5px 0px;
54-
}
55-
`,
56-
]
57-
: css`
58-
margin: -7px 0px;
59-
@media (max-width: 420px) {
60-
margin: -5px 0px;
61-
}
62-
`
63-
}
64-
>
65-
{msg ? (
66-
<Markdown body={msg} md={attachment.descriptionMd} />
67-
) : (
68-
attachment.description
69-
)}
70-
</div>
65+
{attachment?.description && (
66+
<div
67+
css={css`
68+
margin: 10px 0px;
69+
@media (max-width: 420px) {
70+
margin: 5px 0px;
71+
}
72+
`}
73+
>
74+
{msg ? (
75+
<Markdown body={msg} md={attachment?.descriptionMd} />
76+
) : (
77+
attachment?.description
78+
)}
79+
</div>
80+
)}
81+
7182
<Box
7283
css={css`
7384
display: flex;
7485
flex-direction: row;
7586
align-items: center;
87+
gap: 8px;
7688
@media (max-width: 420px) {
7789
flex-direction: column;
7890
align-items: flex-start;
@@ -84,125 +96,66 @@ const AttachmentMetadata = ({
8496
display: flex;
8597
flex-direction: row;
8698
align-items: center;
99+
gap: 4px;
87100
@media (max-width: 420px) {
88101
flex-direction: column;
89102
align-items: flex-start;
90103
}
91104
`}
92105
>
93-
<Tooltip text={attachment.title} position="down">
106+
<Tooltip text={attachment?.title} position="down">
94107
<p
95-
css={
96-
attachment.description
97-
? [
98-
css`
99-
margin: 3px 0 0 0;
100-
font-size: 12px;
101-
opacity: 0.7;
102-
@media (max-width: 420px) {
103-
margin: 8px 0 0 0;
104-
}
105-
`,
106-
]
107-
: css`
108-
margin: 22px 0 15px 0;
109-
font-size: 12px;
110-
opacity: 0.7;
111-
@media (max-width: 420px) {
112-
margin: 10px 0 10px 0;
113-
}
114-
`
115-
}
108+
css={css`
109+
margin: 0;
110+
font-size: 12px;
111+
opacity: 0.7;
112+
`}
116113
>
117-
{attachment.title.length > 22
114+
{attachment?.title?.length > 22
118115
? `${attachment.title.substring(0, 22)}...`
119-
: attachment.title}
116+
: attachment?.title}
120117
</p>
121118
</Tooltip>
122119
<Box
123-
css={
124-
attachment.description
125-
? [
126-
css`
127-
font-size: 12px;
128-
opacity: 0.7;
129-
margin-left: 3px;
130-
margin-top: 2px;
131-
@media (max-width: 420px) {
132-
display: none;
133-
}
134-
`,
135-
]
136-
: css`
137-
font-size: 12px;
138-
opacity: 0.7;
139-
margin-left: 3px;
140-
margin-top: 7px;
141-
@media (max-width: 420px) {
142-
margin-left: 0;
143-
margin-top: 5px;
144-
}
145-
`
146-
}
120+
css={css`
121+
font-size: 12px;
122+
opacity: 0.7;
123+
@media (max-width: 420px) {
124+
margin-left: 0;
125+
}
126+
`}
147127
>
148-
(
149-
{attachment.image_size
150-
? (attachment.image_size / 1024).toFixed(2)
151-
: 0}{' '}
152-
kB)
128+
({getFormattedFileSize()})
153129
</Box>
154130
</Box>
131+
155132
<Box
156133
css={css`
157134
display: flex;
158135
flex-direction: row;
159136
align-items: center;
137+
gap: 8px;
160138
@media (max-width: 420px) {
161-
flex-direction: row;
162-
align-items: flex-start;
139+
margin-top: 5px;
163140
}
164141
`}
165142
>
166-
<Box
167-
css={css`
168-
margin-left: 10px;
169-
margin-top: ${attachment.description ? '3px' : '10px'};
170-
@media (max-width: 420px) {
171-
margin-left: 0;
172-
margin-top: 5px;
173-
}
174-
`}
175-
>
176-
<Tooltip text={isExpanded ? 'Collapse' : 'Expand'} position="top">
177-
<ActionButton
178-
ghost
179-
icon={isExpanded ? 'chevron-down' : 'chevron-left'}
180-
size="small"
181-
onClick={() => {
182-
onExpandCollapseClick();
183-
}}
184-
/>
185-
</Tooltip>
186-
</Box>
187-
<Box
188-
css={css`
189-
margin-left: 10px;
190-
margin-top: 5px;
191-
@media (max-width: 420px) {
192-
margin-left: 0;
193-
margin-top: 5px;
194-
}
195-
`}
196-
>
197-
<Tooltip text="Download" position="top">
198-
<ActionButton
199-
ghost
200-
icon="download"
201-
size="small"
202-
onClick={handleDownload}
203-
/>
204-
</Tooltip>
205-
</Box>
143+
<Tooltip text={isExpanded ? 'Collapse' : 'Expand'} position="top">
144+
<ActionButton
145+
ghost
146+
icon={isExpanded ? 'chevron-down' : 'chevron-left'}
147+
size="small"
148+
onClick={onExpandCollapseClick}
149+
/>
150+
</Tooltip>
151+
<Tooltip text="Download" position="top">
152+
<ActionButton
153+
ghost
154+
icon="download"
155+
size="small"
156+
onClick={handleDownload}
157+
/>
158+
</Tooltip>
206159
</Box>
207160
</Box>
208161
</Box>

0 commit comments

Comments
 (0)