import { useLayoutEffect, useRef, useState, useCallback } from 'react'; import { useLanguage } from '../contexts/LanguageContext'; import { motion, AnimatePresence } from 'framer-motion'; import { FileUp, ShieldCheck, FileText, Image as ImageIcon } from 'lucide-react'; interface GlobalDragDropProps { onFilesSelected: (files: FileList) => void; isAdmin: boolean; } let isDragDropEnabled = true; let forceHideCallback: (() => void) | null = null; export const setDragDropEnabled = (enabled: boolean) => { isDragDropEnabled = enabled; if (!enabled && forceHideCallback) { forceHideCallback(); } }; export const GlobalDragDropOverlay: React.FC = ({ onFilesSelected, isAdmin }) => { const { t } = useLanguage(); const overlayRef = useRef(null); const [isVisible, setIsVisible] = useState(false); const dragCounterRef = useRef(0); const isDragActiveRef = useRef(false); const hasFiles = useCallback((dt: DataTransfer | null) => { if (!dt) return false; const hasFileType = dt.types && dt.types.includes('Files'); if (!hasFileType) return false; if (dt.items && dt.items.length > 0) { for (let i = 0; i < dt.items.length; i++) { if (dt.items[i].kind === 'file') return true; } return false; } return hasFileType; }, []); const handleDragEnter = useCallback((e: DragEvent) => { if (!isDragDropEnabled || !hasFiles(e.dataTransfer)) return; e.preventDefault(); e.stopPropagation(); dragCounterRef.current++; if (dragCounterRef.current === 1) { setIsVisible(true); isDragActiveRef.current = true; } }, [hasFiles]); const handleDragOver = useCallback((e: DragEvent) => { if (!isDragDropEnabled || !hasFiles(e.dataTransfer)) return; e.preventDefault(); e.stopPropagation(); e.dataTransfer!.dropEffect = 'copy'; }, [hasFiles]); const handleDragLeave = useCallback((e: DragEvent) => { if (!isDragDropEnabled || !hasFiles(e.dataTransfer)) return; e.preventDefault(); e.stopPropagation(); dragCounterRef.current = Math.max(0, dragCounterRef.current - 1); if (dragCounterRef.current === 0 && isDragActiveRef.current) { setIsVisible(false); isDragActiveRef.current = false; } }, [hasFiles]); const handleDrop = useCallback((e: DragEvent) => { if (!isDragDropEnabled || !hasFiles(e.dataTransfer)) return; e.preventDefault(); e.stopPropagation(); dragCounterRef.current = 0; setIsVisible(false); isDragActiveRef.current = false; if (e.dataTransfer?.files && e.dataTransfer.files.length > 0) { onFilesSelected(e.dataTransfer.files); } }, [hasFiles, onFilesSelected]); useLayoutEffect(() => { if (!isAdmin) return; dragCounterRef.current = 0; isDragActiveRef.current = false; forceHideCallback = () => { dragCounterRef.current = 0; isDragActiveRef.current = false; setIsVisible(false); }; document.addEventListener('dragenter', handleDragEnter); document.addEventListener('dragover', handleDragOver); document.addEventListener('dragleave', handleDragLeave); document.addEventListener('drop', handleDrop); return () => { document.removeEventListener('dragenter', handleDragEnter); document.removeEventListener('dragover', handleDragOver); document.removeEventListener('dragleave', handleDragLeave); document.removeEventListener('drop', handleDrop); forceHideCallback = null; dragCounterRef.current = 0; isDragActiveRef.current = false; setIsVisible(false); }; }, [isAdmin, handleDragEnter, handleDragOver, handleDragLeave, handleDrop]); if (!isAdmin || typeof window === 'undefined') return null; return ( {isVisible && (

{t('dragDropUploadTitle')}

{t('dropAnywhere')}

{t('secureProcessing')}
{t('allFormats')}
{t('visualVision')}
{t('releaseToIngest')}
)}
); };