Skip to content

Commit

Permalink
Add download function and file name to messages attachments (#489)
Browse files Browse the repository at this point in the history
* Add download function and file name to messages attachments

* Fix linting errors
  • Loading branch information
JeffreytheCoder authored Mar 8, 2024
1 parent da8b930 commit a6cd592
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
39 changes: 39 additions & 0 deletions packages/react/src/components/Attachments/AttachmentMetadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { ActionButton } from '../ActionButton';
import { Box } from '../Box';

const AttachmentMetadata = ({ attachment, url }) => {
const handleDownload = () => {
const anchor = document.createElement('a');
anchor.href = url;
anchor.download = attachment.title;

document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
};

return (
<>
<p style={{ margin: 0 }}>{attachment.description}</p>
<Box
style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}
>
<p style={{ margin: 0, color: 'grey' }}>{attachment.title}</p>
<ActionButton
ghost
icon="download"
size="small"
onClick={handleDownload}
style={{
marginLeft: '10px',
marginTop: '5px',
color: 'grey',
}}
/>
</Box>
</>
);
};

export default AttachmentMetadata;
6 changes: 5 additions & 1 deletion packages/react/src/components/Attachments/AudioAttachment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Box } from '../Box';
import AttachmentMetadata from './AttachmentMetadata';

const AudioAttachment = ({ attachment, host }) => (
<Box>
<p>{attachment?.description}</p>
<AttachmentMetadata
attachment={attachment}
url={host + attachment.audio_url}
/>
<audio src={host + attachment.audio_url} width="100%" controls />
</Box>
);
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/components/Attachments/ImageAttachment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Box } from '../Box';
import AttachmentMetadata from './AttachmentMetadata';

const ImageAttachment = ({ attachment, host }) => (
<Box>
<p>{attachment?.description}</p>
<AttachmentMetadata
attachment={attachment}
url={host + attachment.image_url}
/>
<img
src={host + attachment.image_url}
style={{ maxWidth: '100%', objectFit: 'contain' }}
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/components/Attachments/VideoAttachment.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Box } from '../Box';
import AttachmentMetadata from './AttachmentMetadata';

const VideoAttachment = ({ attachment, host }) => (
<Box>
<p>{attachment?.description}</p>
<AttachmentMetadata
attachment={attachment}
url={host + attachment.video_url}
/>
<video width={300} controls>
<source src={host + attachment.video_url} type={attachment.video_type} />
</video>
Expand Down
14 changes: 14 additions & 0 deletions packages/react/src/components/Icon/icons/Download.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/react/src/components/Icon/icons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import PinFilled from './PinFilled';
import VideoRecorder from './VideoRecoder';
import DisabledRecorder from './DisableRecorder';
import Clipboard from './Clipboard';
import Download from './Download';
import At from './At';

const icons = {
Expand Down Expand Up @@ -83,6 +84,7 @@ const icons = {
'arrow-down': ArrowDown,
'pin-filled': PinFilled,
clipboard: Clipboard,
download: Download,
at: At,
};

Expand Down

0 comments on commit a6cd592

Please sign in to comment.