import { Box, Button } from "@mui/material"; import { editSightStore, SelectMediaDialog, UploadMediaDialog, filterValidFiles, getAllAcceptString, } from "@shared"; import { Upload } from "lucide-react"; import { observer } from "mobx-react-lite"; import { useState, DragEvent, useRef } from "react"; import { toast } from "react-toastify"; export const MediaAreaForSight = observer( ({ onFilesDrop, // 👈 Проп для обработки загруженных файлов onFinishUpload, contextObjectName, contextType, isArticle, articleName, }: { onFilesDrop?: (files: File[]) => void; onFinishUpload?: (mediaId: string) => void; contextObjectName?: string; contextType?: | "sight" | "city" | "carrier" | "country" | "vehicle" | "station"; isArticle?: boolean; articleName?: string; }) => { const [selectMediaDialogOpen, setSelectMediaDialogOpen] = useState(false); const [uploadMediaDialogOpen, setUploadMediaDialogOpen] = useState(false); const [isDragging, setIsDragging] = useState(false); const fileInputRef = useRef(null); const { setFileToUpload } = editSightStore; const handleDrop = (e: DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(false); const files = Array.from(e.dataTransfer.files); if (files.length) { const { validFiles, errors } = filterValidFiles(files); if (errors.length > 0) { errors.forEach((error: string) => toast.error(error)); } if (validFiles.length > 0 && onFilesDrop) { setFileToUpload(validFiles[0]); setUploadMediaDialogOpen(true); } } }; const handleDragOver = (e: DragEvent) => { e.preventDefault(); setIsDragging(true); }; const handleDragLeave = () => { setIsDragging(false); }; const handleClick = () => { fileInputRef.current?.click(); }; const handleFileSelect = (event: React.ChangeEvent) => { const files = Array.from(event.target.files || []); if (files.length) { const { validFiles, errors } = filterValidFiles(files); if (errors.length > 0) { errors.forEach((error: string) => toast.error(error)); } if (validFiles.length > 0 && onFilesDrop) { setFileToUpload(validFiles[0]); onFilesDrop(validFiles); setUploadMediaDialogOpen(true); } } // Сбрасываем значение input, чтобы можно было выбрать тот же файл снова event.target.value = ""; }; return ( <>
Перетащите медиа файлы сюда или нажмите для выбора
или
setUploadMediaDialogOpen(false)} contextObjectName={contextObjectName} contextType={contextType} isArticle={isArticle} articleName={articleName} afterUploadSight={onFinishUpload} /> setSelectMediaDialogOpen(false)} onSelectForSightMedia={onFinishUpload} /> ); } );