From f9586441fe4fc8ea41cb14f00efa6b954b59bd65 Mon Sep 17 00:00:00 2001 From: Kushagra Gour Date: Tue, 23 Apr 2024 18:58:06 +0530 Subject: [PATCH] improve assets ux --- src/components/Assets.jsx | 43 ++++++++++++++++++++++++++++++++------- src/style.css | 18 +++++++++++++--- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/components/Assets.jsx b/src/components/Assets.jsx index fd9961a..5566866 100644 --- a/src/components/Assets.jsx +++ b/src/components/Assets.jsx @@ -9,6 +9,14 @@ import { LoaderWithText } from './Loader'; import { Text } from './Text'; import { Icon } from './Icons'; +function getFileType(url) { + // get extension from a url using URL API + const ext = new URL(url).pathname.split('.').pop(); + if (['jpg', 'jpeg', 'png', 'gif', 'svg'].includes(ext)) { + return 'image'; + } + return ext; +} const Assets = ({ onProBtnClick, onLoginBtnClick }) => { const [files, setFiles] = useState([]); const [isFetchingFiles, setIsFetchingFiles] = useState(false); @@ -21,9 +29,9 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => { const storageRef = firebase.storage().ref(`assets/${window.user?.uid}`); const uploadFile = file => { - if (file.size > 300 * 1024) { - // 5MB limit - alert('File size must be less than 300KB'); + if (file.size > 1024 * 1024) { + // 1MB limit + alert('File size must be less than 1MB'); return; } @@ -47,6 +55,7 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => { // Handle unsuccessful uploads setIsUploading(false); console.error('File upload error:', error); + alertsService.add('⚠️ File upload failed'); }, () => { // uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => { @@ -78,7 +87,10 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => { }); }); - Promise.all(filePromises).then(setFiles); + Promise.all(filePromises).then(files => { + files.forEach(f => (f.ext = getFileType(f.url))); + setFiles(files); + }); setIsFetchingFiles(false); }) .catch(error => { @@ -128,7 +140,6 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => { const handleDrop = e => { e.preventDefault(); - console.log('drop'); setIsDropTarget(false); if (e.dataTransfer.items) { @@ -139,7 +150,6 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => { const [lastCopiedFile, setLastCopiedFile] = useState({ name: '', count: 0 }); const copyFileUrl = url => { - console.log(lastCopiedFile, url); let copyContent = url; if (lastCopiedFile.name === url) { lastCopiedFile.count = (lastCopiedFile.count + 1) % 3; @@ -300,7 +310,26 @@ const Assets = ({ onProBtnClick, onLoginBtnClick }) => { }`} > {/* */} - {' '} + {file.ext === 'image' ? ( + + ) : ( +
+ + + + {file.ext} +
+ )}