-
-
Notifications
You must be signed in to change notification settings - Fork 563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom File Preview #5
Comments
The tutorial project is indeed a great starting point, but it uses custom components for the Chat Message which lacks that kind of check... By having a look at the default react-chat-engine You could borrow or mimic those functions and implement them on both MyMessage: https://github.com/adrianhajdin/chat_application/blob/main/src/components/MyMessage.jsx Edit the above components in order to include the file type check. Within the // src/utils/file.js
const images = ['jpg', 'jpeg', 'png', 'gif', 'tiff']
export const isImage = (fileName) => {
const dotSplit = fileName.split('.')
return dotSplit.length > 0 && images.indexOf(dotSplit[dotSplit.length - 1].toLowerCase()) !== -1
}
export const getFileName = (fileUrl) => {
const slashSplit = fileUrl.split('/')
const nameAndHash = slashSplit[slashSplit.length - 1]
return nameAndHash.split('?')[0]
} Go ahead and edit As we're sending a single file or image, we're goint to check for it on the component itself. import { getFileName, isImage } from './utils/file.js';
const MyMessage = ({ message }) => {
const attachment = message.attachments[0];
const fileName = getFileName(attachment.file ? attachment.file : attachment.name);
if (isImage(fileName)) {
return (
<img
src={message.attachments[0].file}
alt="message-attachment"
className="attachment-image"
style={{ float: 'right' }}
/>
);
} else {
return (
<div
className="attachment-file"
style={{ float: 'right' }}
onClick={() => window.open(attachment.file)}
>
<FontAwesomeIcon icon="fa-solid fa-file-zipper" />{' '}{getFileName(attachment.file)}
</div>
);
}
return (
<div className="message" style={{ float: 'right', marginRight: '18px', color: 'white', backgroundColor: '#3B2A50' }}>
{message.text}
</div>
);
};
export default MyMessage; By now you'd be be able to style the atachments differetly depending on the file type. There's a lot to improve here though. I hope it helps. |
Hello, I would like the app to display a preview image for files that are not photos. How do we go about that? I appreciate your timely feedback. Thank you.
The text was updated successfully, but these errors were encountered: