import { type FC, useState } from "react"; import { Trash } from "iconsax-react"; import UploadBoxDraggble from "@/components/UploadBoxDraggble"; import { useEditorStore } from "@/pages/editor/store/editorStore"; import { useSingleUpload } from "@/pages/uploader/hooks/useUploaderData"; import { createScopedId } from "@/pages/editor/store/editorStore.helpers"; import { GALLERY_DRAG_MIME } from "@/pages/editor/types/GalleryTypes"; const ImageGallery: FC = () => { const { gallery, addGalleryItem, removeGalleryItem } = useEditorStore(); const { mutateAsync: uploadFile } = useSingleUpload(); const [isUploading, setIsUploading] = useState(false); const [uploadProgress, setUploadProgress] = useState(0); const handleFileChange = async (files: File[]) => { if (files.length === 0) return; setIsUploading(true); setUploadProgress(0); try { for (let i = 0; i < files.length; i++) { const file = files[i]; const result = await uploadFile({ file, onProgress: (fileProgress) => { const overall = Math.round(((i + fileProgress / 100) / files.length) * 100); setUploadProgress(overall); }, }); const imageUrl = result?.data?.url; if (!imageUrl) continue; const dimensions = await loadImageDimensions(imageUrl); addGalleryItem({ id: createScopedId("gallery"), url: imageUrl, width: dimensions.width, height: dimensions.height, }); } } catch { // TODO: show error toast } finally { setIsUploading(false); setUploadProgress(0); } }; const handleDragStart = (e: React.DragEvent, url: string) => { e.dataTransfer.setData(GALLERY_DRAG_MIME, url); e.dataTransfer.effectAllowed = "copy"; }; return (
گالری

تصویر را آپلود کنید، سپس از گالری بکشید و روی صفحه رها کنید

{gallery.length === 0 && !isUploading && (

گالری خالی است

)} {gallery.length > 0 && (
{gallery.map((item) => (
handleDragStart(e, item.url)} className="max-h-full max-w-full object-contain cursor-grab active:cursor-grabbing" />
))}
)}
); }; const loadImageDimensions = (url: string): Promise<{ width: number; height: number }> => new Promise((resolve) => { const img = new Image(); img.onload = () => { resolve({ width: img.naturalWidth, height: img.naturalHeight }); }; img.onerror = () => { resolve({ width: 100, height: 100 }); }; img.src = url; }); export default ImageGallery;