- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
691 lines
29 KiB
TypeScript
691 lines
29 KiB
TypeScript
import { CheckCircle2,Copy,FileText,FolderOpen,Image as ImageIcon,Music,Play,Search,Trash2,Upload,X } from 'lucide-react';
|
|
import { useCallback,useEffect,useRef,useState } from 'react';
|
|
import { toast } from 'react-hot-toast';
|
|
import Button from '../components/ui/Button';
|
|
import ConfirmModal from '../components/ui/ConfirmModal';
|
|
import ImagePreviewModal from '../components/ui/ImagePreviewModal';
|
|
import Modal from '../components/ui/Modal';
|
|
import Pagination from '../components/ui/Pagination';
|
|
import Spinner from '../components/ui/Spinner';
|
|
import api,{ BASE_DOMAIN } from '../services/api';
|
|
|
|
|
|
|
|
|
|
|
|
interface Media {
|
|
id: string;
|
|
url: string;
|
|
filename: string;
|
|
mimetype: string;
|
|
size: number;
|
|
altText?: string;
|
|
title?: string;
|
|
description?: string;
|
|
caption?: string;
|
|
createdAt: string;
|
|
}
|
|
|
|
function getFileType(filename: string, mimetype?: string): 'image' | 'video' | 'audio' | 'pdf' | 'other' {
|
|
const ext = filename.split('.').pop()?.toLowerCase() || '';
|
|
if (mimetype?.startsWith('video/') || ['mp4', 'webm', 'ogg', 'mov', 'mkv', 'avi'].includes(ext)) return 'video';
|
|
if (mimetype?.startsWith('audio/') || ['mp3', 'wav', 'ogg', 'm4a', 'aac'].includes(ext)) return 'audio';
|
|
if (mimetype === 'application/pdf' || ext === 'pdf') return 'pdf';
|
|
if (mimetype?.startsWith('image/') || ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg'].includes(ext)) return 'image';
|
|
return 'other';
|
|
}
|
|
|
|
export default function MediaManager() {
|
|
const [mediaList, setMediaList] = useState<Media[]>([]);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [isUploading, setIsUploading] = useState(false);
|
|
const [uploadProgress, setUploadProgress] = useState<number | null>(null);
|
|
const [filterType, setFilterType] = useState<'all' | 'image' | 'video' | 'audio' | 'pdf'>('all');
|
|
const [search, setSearch] = useState('');
|
|
const [page, setPage] = useState(1);
|
|
const limit = 12;
|
|
const [copiedId, setCopiedId] = useState<string | null>(null);
|
|
const [isDragOver, setIsDragOver] = useState(false);
|
|
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
|
|
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
|
|
const [isBulkDeleting, setIsBulkDeleting] = useState(false);
|
|
const [isBulkDeleteModalOpen, setIsBulkDeleteModalOpen] = useState(false);
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
|
|
const fetchMedia = useCallback(async () => {
|
|
try {
|
|
const res = await api.get('/admin/media');
|
|
setMediaList(Array.isArray(res.data) ? res.data : res.data?.data || []);
|
|
setSelectedIds(new Set());
|
|
} catch (error) {
|
|
console.error('Failed to fetch media', error);
|
|
toast.error('خطا در دریافت رسانهها');
|
|
}
|
|
}, []);
|
|
|
|
const toggleSelect = (id: string, e?: React.MouseEvent) => {
|
|
if (e) e.stopPropagation();
|
|
setSelectedIds(prev => {
|
|
const next = new Set(prev);
|
|
if (next.has(id)) {
|
|
next.delete(id);
|
|
} else {
|
|
next.add(id);
|
|
}
|
|
return next;
|
|
});
|
|
};
|
|
|
|
const selectAllCurrentPage = (items: Media[]) => {
|
|
setSelectedIds(prev => {
|
|
const allSelected = items.every(m => prev.has(m.id));
|
|
const next = new Set(prev);
|
|
if (allSelected) {
|
|
items.forEach(m => next.delete(m.id));
|
|
} else {
|
|
items.forEach(m => next.add(m.id));
|
|
}
|
|
return next;
|
|
});
|
|
};
|
|
|
|
const handleBulkDelete = async () => {
|
|
if (selectedIds.size === 0) return;
|
|
try {
|
|
setIsBulkDeleting(true);
|
|
await api.delete('/admin/media/bulk', {
|
|
data: { ids: Array.from(selectedIds) }
|
|
});
|
|
toast.success(`${selectedIds.size} فایل با موفقیت حذف شد`);
|
|
setSelectedIds(new Set());
|
|
setIsBulkDeleteModalOpen(false);
|
|
fetchMedia();
|
|
} catch (error) {
|
|
console.error('Bulk delete failed', error);
|
|
toast.error('خطا در حذف گروهی فایلها');
|
|
} finally {
|
|
setIsBulkDeleting(false);
|
|
}
|
|
};
|
|
|
|
const uploadFiles = useCallback(async (files: File[]) => {
|
|
if (!files || files.length === 0) return;
|
|
try {
|
|
setIsUploading(true);
|
|
setUploadProgress(10);
|
|
let successCount = 0;
|
|
const promises = files.map(async (file) => {
|
|
const formData = new FormData();
|
|
formData.append('file', file);
|
|
return api.post('/admin/media/upload', formData, {
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
onUploadProgress: (progressEvent) => {
|
|
const total = progressEvent.total || file.size;
|
|
const percent = Math.round((progressEvent.loaded * 100) / total);
|
|
setUploadProgress(percent);
|
|
}
|
|
});
|
|
});
|
|
const results = await Promise.allSettled(promises);
|
|
results.forEach(res => {
|
|
if (res.status === 'fulfilled') successCount++;
|
|
});
|
|
if (successCount > 0) {
|
|
toast.success(`${successCount} فایل با موفقیت آپلود شد`);
|
|
await fetchMedia();
|
|
}
|
|
if (successCount < files.length) {
|
|
toast.error(`خطا در آپلود ${files.length - successCount} فایل`);
|
|
}
|
|
} catch (error) {
|
|
console.error('Upload failed', error);
|
|
toast.error('خطا در آپلود فایلها');
|
|
} finally {
|
|
setIsUploading(false);
|
|
setUploadProgress(null);
|
|
if (fileInputRef.current) fileInputRef.current.value = '';
|
|
}
|
|
}, [fetchMedia]);
|
|
|
|
useEffect(() => {
|
|
let isSubscribed = true;
|
|
api.get('/admin/media').then(res => {
|
|
if (isSubscribed) setMediaList(Array.isArray(res.data) ? res.data : res.data?.data || []);
|
|
}).catch(error => {
|
|
console.error('Failed to fetch media', error);
|
|
toast.error('خطا در دریافت رسانهها');
|
|
}).finally(() => {
|
|
if (isSubscribed) setIsLoading(false);
|
|
});
|
|
return () => {
|
|
isSubscribed = false;
|
|
};
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const handlePaste = (e: ClipboardEvent) => {
|
|
const items = e.clipboardData?.items;
|
|
if (!items) return;
|
|
const files: File[] = [];
|
|
for (const item of Array.from(items)) {
|
|
const file = item.getAsFile();
|
|
if (file) files.push(file);
|
|
}
|
|
if (files.length > 0) {
|
|
e.preventDefault();
|
|
uploadFiles(files);
|
|
}
|
|
};
|
|
document.addEventListener('paste', handlePaste);
|
|
return () => document.removeEventListener('paste', handlePaste);
|
|
}, [uploadFiles]);
|
|
|
|
const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);
|
|
const [editingSeoMedia, setEditingSeoMedia] = useState<Media | null>(null);
|
|
const [seoFormData, setSeoFormData] = useState({
|
|
altText: '',
|
|
title: '',
|
|
description: '',
|
|
caption: ''
|
|
});
|
|
|
|
const handleSaveSeo = async () => {
|
|
if (!editingSeoMedia) return;
|
|
try {
|
|
await api.put(`/admin/media/${editingSeoMedia.id}`, seoFormData);
|
|
toast.success('تنظیمات سئوی تصویر با موفقیت ذخیره شد');
|
|
fetchMedia();
|
|
setEditingSeoMedia(null);
|
|
} catch {
|
|
toast.error('خطا در ذخیره تنظیمات سئوی تصویر');
|
|
}
|
|
};
|
|
|
|
const handleUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
if (!e.target.files || e.target.files.length === 0) return;
|
|
await uploadFiles(Array.from(e.target.files));
|
|
};
|
|
|
|
const handleDrop = async (e: React.DragEvent) => {
|
|
e.preventDefault();
|
|
setIsDragOver(false);
|
|
if (!e.dataTransfer.files || e.dataTransfer.files.length === 0) return;
|
|
const files = Array.from(e.dataTransfer.files);
|
|
if (files.length > 0) {
|
|
await uploadFiles(files);
|
|
}
|
|
};
|
|
|
|
const confirmDelete = async () => {
|
|
if (!deleteTargetId) return;
|
|
try {
|
|
await api.delete(`/admin/media/${deleteTargetId}`);
|
|
toast.success('فایل با موفقیت حذف شد');
|
|
setMediaList(prev => prev.filter(m => m.id !== deleteTargetId));
|
|
} catch (error) {
|
|
console.error('Delete failed', error);
|
|
toast.error('خطا در حذف فایل');
|
|
} finally {
|
|
setDeleteTargetId(null);
|
|
}
|
|
};
|
|
|
|
const handleCopyUrl = async (media: Media) => {
|
|
const fullUrl = media.url.startsWith('http') ? media.url : `${BASE_DOMAIN}${media.url}`;
|
|
try {
|
|
await navigator.clipboard.writeText(fullUrl);
|
|
setCopiedId(media.id);
|
|
setTimeout(() => setCopiedId(null), 2000);
|
|
} catch {
|
|
const textarea = document.createElement('textarea');
|
|
textarea.value = fullUrl;
|
|
document.body.appendChild(textarea);
|
|
textarea.select();
|
|
document.execCommand('copy');
|
|
document.body.removeChild(textarea);
|
|
setCopiedId(media.id);
|
|
setTimeout(() => setCopiedId(null), 2000);
|
|
}
|
|
};
|
|
|
|
const formatSize = (bytes: number) => {
|
|
if (bytes < 1024) return bytes + ' B';
|
|
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
|
|
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
|
|
};
|
|
|
|
const filtered = mediaList.filter(m => {
|
|
const matchesSearch = !search || m.filename.toLowerCase().includes(search.toLowerCase());
|
|
const matchesType = filterType === 'all' || getFileType(m.filename, m.mimetype) === filterType;
|
|
return matchesSearch && matchesType;
|
|
});
|
|
|
|
const totalSize = mediaList.reduce((sum, m) => sum + m.size, 0);
|
|
|
|
return (
|
|
<div
|
|
className="space-y-6 font-vazir relative min-h-[80vh]"
|
|
dir="rtl"
|
|
onDragOver={(e) => { e.preventDefault(); setIsDragOver(true); }}
|
|
onDragLeave={(e) => {
|
|
if (!e.currentTarget.contains(e.relatedTarget as Node)) {
|
|
setIsDragOver(false);
|
|
}
|
|
}}
|
|
onDrop={handleDrop}
|
|
>
|
|
{/* Full Page Drag & Drop Overlay */}
|
|
{isDragOver && (
|
|
<div className="fixed inset-0 z-50 bg-purple-600/90 backdrop-blur-xs flex flex-col items-center justify-center text-white pointer-events-none animate-in fade-in">
|
|
<Upload className="w-16 h-16 mb-4 animate-bounce" />
|
|
<p className="text-2xl font-black mb-2">فایلها را در هر جای صفحه رها کنید تا آپلود شوند</p>
|
|
<p className="text-sm font-medium opacity-80">پشتیبانی از تصاویر، ویدئوها، پادکستهای صوتی و اسناد PDF</p>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
|
|
<div>
|
|
<h2 className="text-2xl font-black text-gray-900 flex items-center gap-2">
|
|
<FolderOpen className="w-6 h-6 text-purple-600" />
|
|
مدیریت رسانه و فایلها
|
|
</h2>
|
|
<p className="text-gray-500 font-medium mt-1">
|
|
{mediaList.length} فایل · {formatSize(totalSize)} فضا اشغال شده
|
|
</p>
|
|
</div>
|
|
<div className="flex items-center gap-3">
|
|
<span className="text-xs text-gray-400 hidden sm:flex items-center gap-1">
|
|
Ctrl+V برای چسباندن
|
|
</span>
|
|
<input
|
|
type="file"
|
|
ref={fileInputRef}
|
|
onChange={handleUpload}
|
|
accept="image/*,video/*,audio/*,application/pdf"
|
|
multiple
|
|
className="hidden"
|
|
/>
|
|
<Button
|
|
variant="primary"
|
|
size="sm"
|
|
startIcon={Upload}
|
|
onClick={() => fileInputRef.current?.click()}
|
|
disabled={isUploading}
|
|
isLoading={isUploading}
|
|
>
|
|
{isUploading ? `در حال آپلود ${uploadProgress ? `(${uploadProgress}٪)` : ''}...` : 'آپلود فایل جدید'}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
{/* Upload Progress Bar if active */}
|
|
{isUploading && uploadProgress !== null && (
|
|
<div className="w-full bg-gray-100 rounded-full h-2 overflow-hidden shadow-inner">
|
|
<div
|
|
className="bg-purple-600 h-full transition-all duration-300 rounded-full"
|
|
style={{ width: `${uploadProgress}%` }}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{/* Filter and Search Bar */}
|
|
<div className="flex flex-col sm:flex-row gap-3 items-center justify-between">
|
|
<div className="flex items-center gap-2 flex-wrap w-full sm:w-auto">
|
|
{[
|
|
{ id: 'all', label: 'همه فایلها' },
|
|
{ id: 'image', label: 'تصاویر' },
|
|
{ id: 'video', label: 'ویدئوها' },
|
|
{ id: 'audio', label: 'پادکست/صوت' },
|
|
{ id: 'pdf', label: 'اسناد PDF' },
|
|
].map(tab => (
|
|
<button
|
|
key={tab.id}
|
|
type="button"
|
|
onClick={() => { setFilterType(tab.id as 'all' | 'image' | 'video' | 'audio' | 'pdf'); setPage(1); }}
|
|
className={`px-3 py-1.5 rounded-xl text-xs font-bold transition-all cursor-pointer ${
|
|
filterType === tab.id
|
|
? 'bg-purple-600 text-white shadow-xs'
|
|
: 'bg-white border border-gray-200 text-gray-600 hover:bg-gray-50'
|
|
}`}
|
|
>
|
|
{tab.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
<div className="relative w-full sm:w-72">
|
|
<Search className="absolute right-4 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
|
|
<input
|
|
type="text"
|
|
placeholder="جستجو در نام فایلها..."
|
|
value={search}
|
|
onChange={e => setSearch(e.target.value)}
|
|
className="w-full bg-white border border-gray-200 rounded-xl py-2.5 pr-11 pl-4 text-sm focus:outline-none focus:ring-2 focus:ring-purple-500/20 font-bold"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div
|
|
onDragOver={e => { e.preventDefault(); setIsDragOver(true); }}
|
|
onDragLeave={() => setIsDragOver(false)}
|
|
onDrop={handleDrop}
|
|
className={`rounded-3xl border-2 border-dashed transition-colors ${
|
|
isDragOver
|
|
? 'border-purple-400 bg-purple-50'
|
|
: 'border-gray-200 bg-white'
|
|
}`}
|
|
>
|
|
{isLoading ? (
|
|
<div className="flex justify-center items-center h-40">
|
|
<Spinner size="lg" className="text-purple-600" />
|
|
</div>
|
|
) : filtered.length === 0 ? (
|
|
<div className="flex flex-col items-center justify-center h-60 text-gray-400">
|
|
<ImageIcon className="w-16 h-16 mb-4 opacity-20" />
|
|
<p className="font-medium text-gray-500">
|
|
{search ? 'فایلی با این نام یافت نشد' : 'گالری در این دستهبندی خالی است. اولین فایل را آپلود کنید!'}
|
|
</p>
|
|
</div>
|
|
) : (() => {
|
|
const totalPages = Math.ceil(filtered.length / limit) || 1;
|
|
const paginatedList = filtered.slice((page - 1) * limit, page * limit);
|
|
const isAllPageSelected = paginatedList.length > 0 && paginatedList.every(m => selectedIds.has(m.id));
|
|
|
|
return (
|
|
<div className="space-y-4 p-6">
|
|
{/* Multi-select Toolbar */}
|
|
<div className="flex flex-wrap items-center justify-between gap-3 bg-purple-50/60 p-3 rounded-2xl border border-purple-100 font-vazir">
|
|
<div className="flex items-center gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={() => selectAllCurrentPage(paginatedList)}
|
|
className="flex items-center gap-2 px-3 py-1.5 bg-white border border-purple-200 rounded-xl text-xs font-bold text-purple-700 hover:bg-purple-100/50 transition-all cursor-pointer shadow-xs"
|
|
>
|
|
<div className={`w-4 h-4 rounded border flex items-center justify-center transition-colors ${
|
|
isAllPageSelected ? 'bg-purple-600 border-purple-600 text-white' : 'border-purple-300 bg-white'
|
|
}`}>
|
|
{isAllPageSelected && <CheckCircle2 className="w-3.5 h-3.5" />}
|
|
</div>
|
|
<span>{isAllPageSelected ? 'لغو انتخاب این صفحه' : 'انتخاب همه در این صفحه'}</span>
|
|
</button>
|
|
|
|
{selectedIds.size > 0 && (
|
|
<span className="text-xs font-black text-purple-900 bg-purple-100 px-2.5 py-1 rounded-lg">
|
|
{selectedIds.size} مورد انتخاب شده
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{selectedIds.size > 0 && (
|
|
<button
|
|
type="button"
|
|
onClick={() => setIsBulkDeleteModalOpen(true)}
|
|
className="flex items-center gap-1.5 px-4 py-1.5 bg-red-600 hover:bg-red-700 text-white text-xs font-bold rounded-xl transition-all shadow-md shadow-red-200 cursor-pointer"
|
|
>
|
|
<Trash2 className="w-3.5 h-3.5" />
|
|
<span>حذف موارد انتخابشده ({selectedIds.size})</span>
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-4">
|
|
{paginatedList.map(media => {
|
|
const fileUrl = media.url.startsWith('http') ? media.url : `${BASE_DOMAIN}${media.url}`;
|
|
const isSelected = selectedIds.has(media.id);
|
|
const fileType = getFileType(media.filename, media.mimetype);
|
|
|
|
return (
|
|
<div
|
|
key={media.id}
|
|
className={`group relative bg-gray-100 rounded-2xl overflow-hidden border transition-all cursor-pointer ${
|
|
isSelected ? 'border-purple-600 ring-2 ring-purple-400/50 shadow-md bg-purple-50/20' : 'border-gray-200 hover:border-purple-400 hover:shadow-lg hover:shadow-purple-100'
|
|
}`}
|
|
onClick={() => setPreviewUrl(fileUrl)}
|
|
>
|
|
<div className="aspect-square bg-white p-2 flex items-center justify-center border-b border-gray-100 relative overflow-hidden">
|
|
{/* Checkbox badge */}
|
|
<button
|
|
type="button"
|
|
onClick={(e) => toggleSelect(media.id, e)}
|
|
className={`absolute top-2 right-2 z-20 w-6 h-6 rounded-lg flex items-center justify-center transition-all ${
|
|
isSelected
|
|
? 'bg-purple-600 text-white shadow-md'
|
|
: 'bg-white/80 border border-gray-300 text-transparent hover:border-purple-400 opacity-80 group-hover:opacity-100'
|
|
}`}
|
|
title="انتخاب"
|
|
>
|
|
<CheckCircle2 className={`w-4 h-4 ${isSelected ? 'text-white' : 'text-gray-400'}`} />
|
|
</button>
|
|
|
|
{fileType === 'image' ? (
|
|
<img
|
|
src={fileUrl}
|
|
alt={media.filename}
|
|
className="max-w-full max-h-full object-contain object-center"
|
|
loading="lazy"
|
|
/>
|
|
) : fileType === 'video' ? (
|
|
<div className="relative w-full h-full flex items-center justify-center bg-gray-900 rounded-xl overflow-hidden">
|
|
<video
|
|
src={fileUrl}
|
|
preload="metadata"
|
|
className="w-full h-full object-cover opacity-75"
|
|
/>
|
|
<div className="absolute inset-0 flex items-center justify-center">
|
|
<div className="w-8 h-8 rounded-full bg-purple-600/90 text-white flex items-center justify-center shadow-lg">
|
|
<Play className="w-4 h-4 fill-white ml-0.5" />
|
|
</div>
|
|
</div>
|
|
<span className="absolute bottom-1 right-1 bg-black/70 text-white text-[9px] px-1.5 py-0.5 rounded font-mono font-bold">
|
|
VIDEO
|
|
</span>
|
|
</div>
|
|
) : fileType === 'audio' ? (
|
|
<div className="flex flex-col items-center justify-center text-amber-600 gap-1">
|
|
<Music className="w-10 h-10" />
|
|
<span className="text-[10px] font-black uppercase text-amber-700">Audio / Podcast</span>
|
|
</div>
|
|
) : fileType === 'pdf' ? (
|
|
<div className="flex flex-col items-center justify-center text-rose-600 gap-1">
|
|
<FileText className="w-10 h-10" />
|
|
<span className="text-[10px] font-black uppercase text-rose-700">PDF Document</span>
|
|
</div>
|
|
) : (
|
|
<div className="flex flex-col items-center justify-center text-gray-400 gap-1">
|
|
<FileText className="w-10 h-10" />
|
|
<span className="text-[10px] font-black uppercase text-gray-500">File</span>
|
|
</div>
|
|
)}
|
|
|
|
<div className="absolute inset-0 bg-black/40 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center gap-2">
|
|
<button
|
|
onClick={e => { e.stopPropagation(); handleCopyUrl(media); }}
|
|
className="w-9 h-9 rounded-full bg-blue-500 text-white flex items-center justify-center hover:bg-blue-600 transform hover:scale-110 transition-transform"
|
|
title="کپی لینک"
|
|
>
|
|
{copiedId === media.id ? <CheckCircle2 className="w-4 h-4" /> : <Copy className="w-4 h-4" />}
|
|
</button>
|
|
<button
|
|
onClick={e => {
|
|
e.stopPropagation();
|
|
setEditingSeoMedia(media);
|
|
setSeoFormData({
|
|
altText: media.altText || '',
|
|
title: media.title || '',
|
|
description: media.description || '',
|
|
caption: media.caption || ''
|
|
});
|
|
}}
|
|
className="w-9 h-9 rounded-full bg-purple-600 text-white flex items-center justify-center hover:bg-purple-700 transform hover:scale-110 transition-transform text-xs font-bold"
|
|
title="تنظیمات سئو"
|
|
>
|
|
SEO
|
|
</button>
|
|
<button
|
|
onClick={e => { e.stopPropagation(); setDeleteTargetId(media.id); }}
|
|
className="w-9 h-9 rounded-full bg-red-500 text-white flex items-center justify-center hover:bg-red-600 transform hover:scale-110 transition-transform"
|
|
title="حذف"
|
|
>
|
|
<Trash2 className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div className="p-2 bg-white border-t border-gray-100" dir="ltr">
|
|
<p className="text-[11px] text-gray-500 truncate font-medium" title={media.filename}>
|
|
{media.filename}
|
|
</p>
|
|
<div className="flex justify-between items-center mt-0.5">
|
|
<p className="text-[10px] text-gray-400">
|
|
{formatSize(media.size)}
|
|
</p>
|
|
<p className="text-[10px] text-gray-400">
|
|
{new Date(media.createdAt).toLocaleDateString('fa-IR')}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
<Pagination currentPage={page} totalPages={totalPages} onPageChange={setPage} />
|
|
</div>
|
|
);
|
|
})()}
|
|
</div>
|
|
|
|
{/* Bulk Delete Confirm Modal */}
|
|
<ConfirmModal
|
|
isOpen={isBulkDeleteModalOpen}
|
|
title="حذف گروهی تصاویر"
|
|
message={`آیا از حذف دستهجمعی ${selectedIds.size} تصویر انتخاب شده مطمئن هستید؟ این عملیات غیرقابل بازگشت است.`}
|
|
onConfirm={handleBulkDelete}
|
|
onCancel={() => setIsBulkDeleteModalOpen(false)}
|
|
isLoading={isBulkDeleting}
|
|
/>
|
|
|
|
{previewUrl && (
|
|
<div
|
|
className="fixed inset-0 z-[200] flex items-center justify-center p-4 bg-gray-900/80 backdrop-blur-sm"
|
|
onClick={() => setPreviewUrl(null)}
|
|
>
|
|
<div className="relative max-w-4xl max-h-[90vh]">
|
|
<img
|
|
src={previewUrl}
|
|
alt="Preview"
|
|
className="max-w-full max-h-[85vh] rounded-2xl shadow-2xl object-contain"
|
|
/>
|
|
<button
|
|
onClick={() => setPreviewUrl(null)}
|
|
className="absolute top-3 left-3 w-10 h-10 rounded-full bg-black/50 text-white flex items-center justify-center hover:bg-black/70 transition-colors"
|
|
>
|
|
<X className="w-5 h-5" />
|
|
</button>
|
|
<button
|
|
onClick={() => {
|
|
navigator.clipboard.writeText(previewUrl);
|
|
setCopiedId('preview');
|
|
setTimeout(() => setCopiedId(null), 2000);
|
|
}}
|
|
className="absolute bottom-3 right-3 bg-black/50 text-white px-4 py-2 rounded-xl flex items-center gap-2 text-sm font-bold hover:bg-black/70 transition-colors"
|
|
>
|
|
{copiedId === 'preview' ? <CheckCircle2 className="w-4 h-4" /> : <Copy className="w-4 h-4" />}
|
|
کپی URL
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Edit SEO Modal */}
|
|
{editingSeoMedia && (
|
|
<Modal
|
|
isOpen={!!editingSeoMedia}
|
|
onClose={() => setEditingSeoMedia(null)}
|
|
title="تنظیمات سئوی تصویر"
|
|
icon={ImageIcon}
|
|
maxWidth="md"
|
|
footer={
|
|
<div className="flex justify-end gap-2.5 w-full">
|
|
<Button
|
|
variant="secondary"
|
|
size="sm"
|
|
onClick={() => setEditingSeoMedia(null)}
|
|
>
|
|
انصراف
|
|
</Button>
|
|
<Button
|
|
variant="primary"
|
|
size="sm"
|
|
onClick={handleSaveSeo}
|
|
>
|
|
ذخیره سئو
|
|
</Button>
|
|
</div>
|
|
}
|
|
>
|
|
<div className="space-y-3 text-right font-vazir">
|
|
<div>
|
|
<label className="text-xs font-bold text-gray-700 block mb-1">متن جایگزین (Alt Text) *</label>
|
|
<input
|
|
type="text"
|
|
value={seoFormData.altText}
|
|
onChange={(e) => setSeoFormData({ ...seoFormData, altText: e.target.value })}
|
|
placeholder="مثال: عکس مکمل کانیدروکس گپ کنینا"
|
|
className="w-full px-3 py-2 border rounded-xl text-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="text-xs font-bold text-gray-700 block mb-1">عنوان تصویر (Title)</label>
|
|
<input
|
|
type="text"
|
|
value={seoFormData.title}
|
|
onChange={(e) => setSeoFormData({ ...seoFormData, title: e.target.value })}
|
|
placeholder="عنوان تصویر برای تولتیپ هور"
|
|
className="w-full px-3 py-2 border rounded-xl text-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="text-xs font-bold text-gray-700 block mb-1">توضیحات (Description)</label>
|
|
<textarea
|
|
rows={2}
|
|
value={seoFormData.description}
|
|
onChange={(e) => setSeoFormData({ ...seoFormData, description: e.target.value })}
|
|
placeholder="توضیحات کامل سئو..."
|
|
className="w-full px-3 py-2 border rounded-xl text-sm"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="text-xs font-bold text-gray-700 block mb-1">زیرنویس (Caption)</label>
|
|
<input
|
|
type="text"
|
|
value={seoFormData.caption}
|
|
onChange={(e) => setSeoFormData({ ...seoFormData, caption: e.target.value })}
|
|
placeholder="متن کپشن زیر عکس در مقالات"
|
|
className="w-full px-3 py-2 border rounded-xl text-sm"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
)}
|
|
|
|
|
|
<ConfirmModal
|
|
isOpen={!!deleteTargetId}
|
|
title="حذف تصویر"
|
|
message="آیا از حذف این تصویر مطمئن هستید؟ این عملیات قابل بازگشت نیست."
|
|
onConfirm={confirmDelete}
|
|
onCancel={() => setDeleteTargetId(null)}
|
|
/>
|
|
|
|
<ImagePreviewModal
|
|
isOpen={!!previewUrl}
|
|
imageUrl={previewUrl || ''}
|
|
onClose={() => setPreviewUrl(null)}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|