feat(media): implement omnichannel media gallery with drag-and-drop, video support, and quick access podcast/video hub
This commit is contained in:
1 parent
32e40a820c
commit
d9c693c41b
13 files changed
+9449
-8874
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { Search, Filter, Box, Plus, Edit2, Trash2, Image as ImageIcon, X, HelpCircle, ArrowUpDown, ArrowUp, ArrowDown } from 'lucide-react';
|
||||
import { Search, Filter, Box, Plus, Edit2, Trash2, Image as ImageIcon, X, HelpCircle, ArrowUpDown, ArrowUp, ArrowDown, GripVertical, Film, Play, ChevronRight, ChevronLeft, Link2 } from 'lucide-react';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import api, { BASE_DOMAIN } from '../services/api';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
@@ -224,6 +224,85 @@ export default function Products() {
|
||||
preorderDeposit: '' as number | string
|
||||
});
|
||||
|
||||
const [draggedMediaIdx, setDraggedMediaIdx] = useState<number | null>(null);
|
||||
const [dragOverMediaIdx, setDragOverMediaIdx] = useState<number | null>(null);
|
||||
const [customMediaUrl, setCustomMediaUrl] = useState('');
|
||||
const [showCustomMediaInput, setShowCustomMediaInput] = useState(false);
|
||||
|
||||
const isVideoMedia = (url?: string) => {
|
||||
if (!url) return false;
|
||||
const clean = url.toLowerCase().trim();
|
||||
return (
|
||||
clean.endsWith('.mp4') ||
|
||||
clean.endsWith('.webm') ||
|
||||
clean.endsWith('.mov') ||
|
||||
clean.endsWith('.mkv') ||
|
||||
clean.endsWith('.m4v') ||
|
||||
clean.includes('aparat.com') ||
|
||||
clean.includes('youtube.com') ||
|
||||
clean.includes('youtu.be') ||
|
||||
clean.includes('vimeo.com') ||
|
||||
clean.includes('/video/') ||
|
||||
clean.startsWith('data:video')
|
||||
);
|
||||
};
|
||||
|
||||
const handleDragStart = (idx: number) => {
|
||||
setDraggedMediaIdx(idx);
|
||||
};
|
||||
|
||||
const handleDragOver = (e: React.DragEvent, idx: number) => {
|
||||
e.preventDefault();
|
||||
if (dragOverMediaIdx !== idx) {
|
||||
setDragOverMediaIdx(idx);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDragLeave = () => {
|
||||
setDragOverMediaIdx(null);
|
||||
};
|
||||
|
||||
const handleDrop = (e: React.DragEvent, dropIdx: number) => {
|
||||
e.preventDefault();
|
||||
if (draggedMediaIdx === null || draggedMediaIdx === dropIdx) {
|
||||
setDraggedMediaIdx(null);
|
||||
setDragOverMediaIdx(null);
|
||||
return;
|
||||
}
|
||||
const updated = [...(formData.images || [])];
|
||||
const [movedItem] = updated.splice(draggedMediaIdx, 1);
|
||||
updated.splice(dropIdx, 0, movedItem);
|
||||
setFormData(prev => ({ ...prev, images: updated }));
|
||||
setDraggedMediaIdx(null);
|
||||
setDragOverMediaIdx(null);
|
||||
};
|
||||
|
||||
const handleMoveMedia = (idx: number, direction: 'left' | 'right') => {
|
||||
const images = formData.images || [];
|
||||
// In RTL layout, 'left' button visually moves item forward (idx + 1), 'right' moves item backward (idx - 1)
|
||||
const targetIdx = direction === 'left' ? idx + 1 : idx - 1;
|
||||
if (targetIdx < 0 || targetIdx >= images.length) return;
|
||||
const updated = [...images];
|
||||
const temp = updated[idx];
|
||||
updated[idx] = updated[targetIdx];
|
||||
updated[targetIdx] = temp;
|
||||
setFormData(prev => ({ ...prev, images: updated }));
|
||||
};
|
||||
|
||||
const handleAddCustomMedia = () => {
|
||||
const trimmed = customMediaUrl.trim();
|
||||
if (!trimmed) return;
|
||||
const currentImages = formData.images || [];
|
||||
if (!currentImages.includes(trimmed)) {
|
||||
setFormData(prev => ({ ...prev, images: [...currentImages, trimmed] }));
|
||||
toast.success(isVideoMedia(trimmed) ? 'ویدیو به گالری اضافه شد' : 'تصویر به گالری اضافه شد');
|
||||
} else {
|
||||
toast.error('این لینک قبلاً در گالری ثبت شده است');
|
||||
}
|
||||
setCustomMediaUrl('');
|
||||
setShowCustomMediaInput(false);
|
||||
};
|
||||
|
||||
const openModal = (product?: Product) => {
|
||||
setMediaImageError(false);
|
||||
if (product) {
|
||||
@@ -1655,62 +1734,208 @@ export default function Products() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Product Gallery Images (Multiple Angles & Views) */}
|
||||
{/* Product Gallery Media (Images & Videos with Drag & Drop Reordering) */}
|
||||
<div className="bg-white p-5 rounded-2xl border border-gray-200 shadow-sm space-y-4">
|
||||
<div className="flex items-center justify-between border-b pb-2">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-3 border-b pb-3">
|
||||
<div>
|
||||
<h4 className="font-bold text-gray-800 text-sm flex items-center gap-2">
|
||||
<ImageIcon className="w-4 h-4 text-blue-600" />
|
||||
گالری تصاویر جانبی محصول (Gallery Images)
|
||||
<span>گالری چندرسانهای محصول (تصاویر و ویدیوها)</span>
|
||||
{formData.images && formData.images.length > 0 && (
|
||||
<span className="text-[10px] font-bold bg-blue-50 text-blue-700 px-2 py-0.5 rounded-full border border-blue-100 font-mono">
|
||||
{formData.images.length} آیتم
|
||||
</span>
|
||||
)}
|
||||
</h4>
|
||||
<p className="text-[11px] text-gray-400 font-medium mt-0.5">تصاویر زوایای مختلف، پشت بستهبندی، جدول ارزش غذایی و مشخصات فنی</p>
|
||||
<p className="text-[11px] text-gray-400 font-medium mt-0.5">
|
||||
ترتیب نمایش را با کشیدن و رها کردن (Drag & Drop) یا دکمههای جابهجایی تغییر دهید. پشتیبانی از انواع تصاویر و ویدیوها (MP4 / آپارات / یوتیوب).
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowCustomMediaInput(prev => !prev)}
|
||||
className="bg-gray-100 hover:bg-gray-200 text-gray-700 px-3 py-1.5 rounded-xl text-xs font-bold transition-all cursor-pointer flex items-center gap-1.5"
|
||||
>
|
||||
<Link2 className="w-3.5 h-3.5" />
|
||||
لینک ویدیو / تصویر
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setMediaTargetField('gallery');
|
||||
setIsMediaSelectorOpen(true);
|
||||
}}
|
||||
className="bg-blue-600 hover:bg-blue-700 text-white px-3.5 py-1.5 rounded-xl text-xs font-bold transition-all cursor-pointer flex items-center gap-1.5 shadow-xs"
|
||||
>
|
||||
<Plus className="w-3.5 h-3.5" />
|
||||
افزودن از رسانهها
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setMediaTargetField('gallery');
|
||||
setIsMediaSelectorOpen(true);
|
||||
}}
|
||||
className="bg-blue-600 hover:bg-blue-700 text-white px-4 py-1.5 rounded-xl text-xs font-bold transition-all cursor-pointer flex items-center gap-1.5"
|
||||
>
|
||||
<Plus className="w-3.5 h-3.5" />
|
||||
افزودن عکس به گالری
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Custom URL Form */}
|
||||
{showCustomMediaInput && (
|
||||
<div className="p-3 bg-blue-50/60 border border-blue-100 rounded-xl space-y-2 animate-in fade-in duration-150">
|
||||
<label className="text-xs font-bold text-blue-900 block">
|
||||
افزودن مستقیم آدرس اینترنتی رسانه (عکس یا ویدیو):
|
||||
</label>
|
||||
<div className="flex gap-2">
|
||||
<input
|
||||
type="text"
|
||||
value={customMediaUrl}
|
||||
onChange={(e) => setCustomMediaUrl(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
handleAddCustomMedia();
|
||||
}
|
||||
}}
|
||||
placeholder="https://... یا /uploads/video.mp4 یا لینک آپارات / یوتیوب"
|
||||
className="flex-1 px-3 py-2 rounded-xl border border-blue-200 bg-white font-mono text-xs outline-none focus:ring-2 focus:ring-blue-500"
|
||||
dir="ltr"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleAddCustomMedia}
|
||||
className="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-xl text-xs font-bold whitespace-nowrap cursor-pointer"
|
||||
>
|
||||
ثبت در گالری
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setCustomMediaUrl('');
|
||||
setShowCustomMediaInput(false);
|
||||
}}
|
||||
className="px-3 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 rounded-xl text-xs font-bold cursor-pointer"
|
||||
>
|
||||
انصراف
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Gallery Grid with Drag & Drop */}
|
||||
{formData.images && formData.images.length > 0 ? (
|
||||
<div className="grid grid-cols-2 sm:grid-cols-4 md:grid-cols-6 gap-3">
|
||||
{formData.images.map((imgUrl, idx) => (
|
||||
<div key={idx} className="relative group rounded-xl border border-gray-200 overflow-hidden bg-gray-50 aspect-square flex items-center justify-center p-2 shadow-xs">
|
||||
<img
|
||||
src={getProductImageUrl(imgUrl)}
|
||||
alt={`Gallery ${idx + 1}`}
|
||||
className="max-h-full max-w-full object-contain"
|
||||
/>
|
||||
<div className="absolute inset-0 bg-black/60 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center gap-1.5 p-1">
|
||||
<button
|
||||
type="button"
|
||||
title="حذف از گالری"
|
||||
onClick={() => {
|
||||
const updated = formData.images.filter((_, i) => i !== idx);
|
||||
setFormData({ ...formData, images: updated });
|
||||
}}
|
||||
className="w-7 h-7 rounded-lg bg-red-600 hover:bg-red-700 text-white flex items-center justify-center transition-colors"
|
||||
>
|
||||
<X className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-3">
|
||||
{formData.images.map((mediaUrl, idx) => {
|
||||
const isVideo = isVideoMedia(mediaUrl);
|
||||
const isDragging = draggedMediaIdx === idx;
|
||||
const isOver = dragOverMediaIdx === idx;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={idx}
|
||||
draggable
|
||||
onDragStart={() => handleDragStart(idx)}
|
||||
onDragOver={(e) => handleDragOver(e, idx)}
|
||||
onDragLeave={handleDragLeave}
|
||||
onDrop={(e) => handleDrop(e, idx)}
|
||||
onDragEnd={() => {
|
||||
setDraggedMediaIdx(null);
|
||||
setDragOverMediaIdx(null);
|
||||
}}
|
||||
className={`relative group rounded-2xl border transition-all aspect-square flex flex-col items-center justify-center p-2 select-none cursor-grab active:cursor-grabbing ${
|
||||
isOver
|
||||
? 'border-purple-600 bg-purple-50 scale-105 shadow-lg ring-2 ring-purple-400'
|
||||
: isDragging
|
||||
? 'opacity-40 border-dashed border-gray-400 bg-gray-100 scale-95'
|
||||
: 'border-gray-200 bg-gray-50/80 hover:border-purple-300 hover:shadow-md'
|
||||
}`}
|
||||
>
|
||||
{/* Grip Handle Icon */}
|
||||
<div className="absolute top-1.5 right-1.5 w-6 h-6 rounded-lg bg-black/40 backdrop-blur-xs text-white flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity z-10">
|
||||
<GripVertical className="w-3.5 h-3.5" />
|
||||
</div>
|
||||
|
||||
{/* Media Preview (Video or Image) */}
|
||||
{isVideo ? (
|
||||
<div className="w-full h-full flex flex-col items-center justify-center relative rounded-xl overflow-hidden bg-neutral-900 text-white">
|
||||
<Film className="w-8 h-8 text-blue-400 opacity-60 mb-1" />
|
||||
<div className="w-7 h-7 rounded-full bg-blue-600 text-white flex items-center justify-center shadow-md">
|
||||
<Play className="w-3.5 h-3.5 fill-white ml-0.5" />
|
||||
</div>
|
||||
<span className="absolute bottom-1 right-1 bg-blue-600/90 text-white text-[8px] font-bold px-1.5 py-0.5 rounded font-vazir">
|
||||
ویدیو
|
||||
</span>
|
||||
</div>
|
||||
) : (
|
||||
<img
|
||||
src={getProductImageUrl(mediaUrl)}
|
||||
alt={`Gallery ${idx + 1}`}
|
||||
className="max-h-full max-w-full object-contain pointer-events-none"
|
||||
onError={(e) => {
|
||||
(e.target as HTMLElement).style.display = 'none';
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Overlay on hover: Delete & Move Left/Right */}
|
||||
<div className="absolute inset-0 bg-black/70 backdrop-blur-2xs opacity-0 group-hover:opacity-100 transition-opacity rounded-2xl flex flex-col items-center justify-between p-2 z-20">
|
||||
<div className="w-full flex items-center justify-between">
|
||||
<span className="text-[10px] text-white/90 font-mono font-bold">
|
||||
#{idx + 1}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
title="حذف از گالری"
|
||||
onClick={() => {
|
||||
const updated = formData.images.filter((_, i) => i !== idx);
|
||||
setFormData(prev => ({ ...prev, images: updated }));
|
||||
}}
|
||||
className="w-6 h-6 rounded-lg bg-red-600 hover:bg-red-700 text-white flex items-center justify-center transition-colors cursor-pointer"
|
||||
>
|
||||
<X className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="text-center">
|
||||
<span className="text-[9px] text-white/70 block truncate max-w-[100px]" dir="ltr">
|
||||
{mediaUrl.split('/').pop()?.slice(0, 15) || 'رسانه'}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Reorder buttons (left/right for quick shift) */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<button
|
||||
type="button"
|
||||
disabled={idx === 0}
|
||||
title="انتقال به عقب"
|
||||
onClick={() => handleMoveMedia(idx, 'right')}
|
||||
className="w-6 h-6 rounded-md bg-white/20 hover:bg-white/40 disabled:opacity-30 disabled:cursor-not-allowed text-white flex items-center justify-center transition-colors cursor-pointer"
|
||||
>
|
||||
<ChevronRight className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
disabled={idx === formData.images.length - 1}
|
||||
title="انتقال به جلو"
|
||||
onClick={() => handleMoveMedia(idx, 'left')}
|
||||
className="w-6 h-6 rounded-md bg-white/20 hover:bg-white/40 disabled:opacity-30 disabled:cursor-not-allowed text-white flex items-center justify-center transition-colors cursor-pointer"
|
||||
>
|
||||
<ChevronLeft className="w-3.5 h-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Index badge when not hovered */}
|
||||
<span className="absolute bottom-1 right-1 bg-black/60 text-white text-[9px] px-1.5 py-0.5 rounded font-mono group-hover:opacity-0 transition-opacity">
|
||||
#{idx + 1}
|
||||
</span>
|
||||
</div>
|
||||
<span className="absolute bottom-1 right-1 bg-black/60 text-white text-[9px] px-1.5 py-0.5 rounded font-mono">
|
||||
#{idx + 1}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center py-6 text-gray-400 bg-gray-50/50 rounded-xl border border-dashed border-gray-200">
|
||||
<ImageIcon className="w-8 h-8 mx-auto mb-2 opacity-30 text-gray-400" />
|
||||
<p className="text-xs font-bold text-gray-500">هنوز عکس جانبی به گالری محصول اضافه نشده است.</p>
|
||||
<p className="text-[10px] text-gray-400 mt-1">با زدن دکمه «افزودن عکس به گالری» میتوانید چند عکس اضافه نمایید.</p>
|
||||
<div className="text-center py-8 text-gray-400 bg-gray-50/50 rounded-2xl border border-dashed border-gray-200 space-y-2">
|
||||
<div className="w-12 h-12 rounded-2xl bg-gray-100 flex items-center justify-center mx-auto text-gray-400">
|
||||
<ImageIcon className="w-6 h-6 opacity-40" />
|
||||
</div>
|
||||
<p className="text-xs font-bold text-gray-600">گالری محصول خالی است.</p>
|
||||
<p className="text-[11px] text-gray-400">
|
||||
با زدن دکمه «افزودن از رسانهها» یا «لینک ویدیو / تصویر» میتوانید تصاویر جانبی یا ویدیوهای محصول را اضافه کنید.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user