import React, { useEffect, useState } from 'react'; // Third Party Libraries import { observer } from 'mobx-react-lite'; import type { StoreType } from 'polotno/model/store'; import { ImagesGrid, UploadSection as DefaultUploadSection, SectionTab, } from 'polotno/side-panel'; import { getImageSize, getCrop } from 'polotno/utils/image'; // Hooks import { useDispatch, useSelector } from 'react-redux'; // Actions import { uploadFile } from '../../../redux/actions/templateActions'; import { failure } from '../../../redux/actions/snackbarActions'; import { SET_UPLOADED_IMAGES } from '../../../redux/actions/action-types'; import { AppDispatch, RootState } from '../../../redux/store'; // Utils import { getType } from '../../../utils/helper'; import { allowedImageTypes } from '../../../utils/constants'; import { MESSAGES } from '../../../utils/message'; // V2 Component import CustomUploadsV2 from './V2'; interface UploadPanelProps { store: StoreType; } export const UploadPanel = observer(({ store }: UploadPanelProps) => { const [images, setImages] = useState< Array<{ url: string; type: string; }> >([]); const [isUploading, setUploading] = useState(false); const [isLoading, setLoading] = useState(false); const dispatch: AppDispatch = useDispatch(); const uploadedImages = useSelector( (state: RootState) => state.templates.uploadedImages ); const load = async () => { if (uploadedImages) { setLoading(true); setImages(uploadedImages); setLoading(false); } }; const validateFile = (file: File) => { if (!file || !allowedImageTypes.includes(file?.type)) { dispatch( failure(MESSAGES.TEMPLATE.CUSTOM_UPLOAD_SECTION.TYPE_VALIDATION) ); return false; } if (file.size >= 7300000) { // 5MB limit dispatch( failure(MESSAGES.TEMPLATE.CUSTOM_UPLOAD_SECTION.SIZE_VALIDATION) ); return false; } return true; }; const handleFileInput = async (e: any) => { try { const { target } = e; for (const file of target.files) { const type = getType(file); const validate = validateFile(file); if (!validate) { target.value = null; return; } setUploading(true); const uploadedFile = await uploadFile(file); const allImages = [...images, { url: uploadedFile, type }]; setImages(allImages); dispatch({ type: SET_UPLOADED_IMAGES, payload: allImages }); } setUploading(false); target.value = null; } catch (error) { return error; } finally { setUploading(false); } }; useEffect(() => { load(); }, []); return (