import React, { useCallback, useRef, useState } from 'react'; import './FileImportModal.css'; import { FaTimes } from 'react-icons/fa'; import { showAlert } from '../../util/dialogUtil'; interface FileImportModalProps { isVisible: boolean; onClose: () => void; onFileImport: (file: File) => void; acceptedTypes?: string[]; title?: string; description?: string; } const FileImportModal: React.FC = ({ isVisible, onClose, onFileImport, acceptedTypes = ['.json'], title = 'Import Project', description = 'Drag and drop your project file here' }) => { const [isDragOver, setIsDragOver] = useState(false); const [isClosing, setIsClosing] = useState(false); const mouseDownOnOverlay = useRef(false); const startClose = useCallback(() => setIsClosing(true), []); const handleAnimationEnd = useCallback((e: React.AnimationEvent) => { if (e.target !== e.currentTarget) return; if (!isClosing) return; setIsClosing(false); onClose(); }, [isClosing, onClose]); const handleDragEnter = useCallback((e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragOver(true); }, []); const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); if (e.currentTarget === e.target) { setIsDragOver(false); } }, []); const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); }, []); const handleDrop = useCallback(async (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragOver(false); const files = Array.from(e.dataTransfer.files); if (files.length > 0) { const file = files[0]; const fileExtension = '.' + file.name.split('.').pop()?.toLowerCase(); if (acceptedTypes.includes(fileExtension)) { onFileImport(file); startClose(); } else { await showAlert(`Invalid file type. Please select a file with one of these extensions: ${acceptedTypes.join(', ')}`); } } }, [acceptedTypes, onFileImport, startClose]); const handleFileSelect = useCallback((e: React.ChangeEvent) => { const files = e.target.files; if (files && files.length > 0) { onFileImport(files[0]); startClose(); } }, [onFileImport, startClose]); const handleOverlayMouseDown = useCallback((e: React.MouseEvent) => { mouseDownOnOverlay.current = e.target === e.currentTarget; }, []); const handleOverlayClick = useCallback((e: React.MouseEvent) => { if (e.target === e.currentTarget && mouseDownOnOverlay.current) { startClose(); } }, [startClose]); if (!isVisible && !isClosing) { return null; } return (

{title}

📁

{description}

Supported formats: {acceptedTypes.join(', ')}

or
); }; export default FileImportModal;