import { Box, Button } from "@mui/material"; import { MediaViewer } from "@widgets"; import { PreviewMediaDialog, filterValidFiles, getAllAcceptString, } from "@shared"; import { X, Upload } from "lucide-react"; import { observer } from "mobx-react-lite"; import { useState, DragEvent, useRef } from "react"; import { toast } from "react-toastify"; interface MediaAreaProps { articleId: number; mediaIds: { id: string; media_type: number; filename: string }[]; deleteMedia: (id: number, media_id: string) => void; onFilesDrop?: (files: File[]) => void; setSelectMediaDialogOpen: (open: boolean) => void; } export const MediaArea = observer( ({ articleId, mediaIds, deleteMedia, onFilesDrop, setSelectMediaDialogOpen, }: MediaAreaProps) => { const [mediaModal, setMediaModal] = useState(false); const [mediaId, setMediaId] = useState(""); const [isDragging, setIsDragging] = useState(false); const fileInputRef = useRef(null); const handleMediaModal = (mediaId: string) => { setMediaModal(true); setMediaId(mediaId); }; const processFiles = (files: File[]) => { if (!files.length || !onFilesDrop) { return; } const { validFiles, errors } = filterValidFiles(files); if (errors.length > 0) { errors.forEach((error) => toast.error(error)); } if (validFiles.length > 0) { onFilesDrop(validFiles); } }; const handleDrop = (e: DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(false); const files = Array.from(e.dataTransfer.files); processFiles(files); }; 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 || []); processFiles(files); if (event.target) { event.target.value = ""; } }; return ( <>
Перетащите медиа файлы сюда или нажмите для выбора
или
{mediaIds.length > 0 && (
{mediaIds.map((m) => ( ))}
)}
setMediaModal(false)} mediaId={mediaId} /> ); } );