"use client"; import { useEffect, useRef, useState } from "react"; import { ArrowRight, Copy, Loader2, Trash2, UploadCloud } from "lucide-react"; import { useRouter } from "next/navigation"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { api } from "@/lib/api"; import type * as Types from "@/lib/types"; import { resolveErrorMessage } from "@/lib/utils"; import { useToast } from "@/hooks/use-toast"; type Props = { postId: number; }; export default function AdminBlogAssets({ postId }: Props) { const router = useRouter(); const { toast } = useToast(); const fileInputRef = useRef(null); const [post, setPost] = useState(null); const [assets, setAssets] = useState([]); const [loading, setLoading] = useState(true); const [uploading, setUploading] = useState(false); const [deletingId, setDeletingId] = useState(null); const loadData = async () => { if (!Number.isFinite(postId)) { setLoading(false); return; } setLoading(true); try { const [postData, assetData] = await Promise.all([ api.getAdminBlogPost(postId), api.listBlogPostAssets(postId), ]); setPost(postData); setAssets(assetData); } catch (error) { toast({ title: "دریافت مرکز آپلود ناموفق بود", description: resolveErrorMessage(error, "دوباره تلاش کنید"), variant: "destructive", }); } finally { setLoading(false); } }; useEffect(() => { void loadData(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [postId]); const uploadAsset = async (file: File) => { setUploading(true); try { const asset = await api.uploadBlogPostAsset(postId, file, { title: file.name }); setAssets((prev) => [asset, ...prev]); toast({ title: "فایل آپلود شد", variant: "success" }); } catch (error) { toast({ title: "آپلود ناموفق بود", description: resolveErrorMessage(error, "دوباره تلاش کنید"), variant: "destructive", }); } finally { setUploading(false); } }; const onFileChange = (event: React.ChangeEvent) => { const file = event.target.files?.[0]; event.currentTarget.value = ""; if (file) void uploadAsset(file); }; const copySnippet = async (asset: Types.PostAssetSchema) => { const snippet = asset.markdown_image || asset.markdown_link || asset.absolute_file_url || ""; await navigator.clipboard.writeText(snippet); toast({ title: "کد مارک‌داون کپی شد", variant: "success" }); }; const deleteAsset = async (assetId: number) => { setDeletingId(assetId); try { await api.deleteBlogPostAsset(postId, assetId); setAssets((prev) => prev.filter((asset) => asset.id !== assetId)); toast({ title: "فایل حذف شد", variant: "success" }); } catch (error) { toast({ title: "حذف فایل ناموفق بود", description: resolveErrorMessage(error, "دوباره تلاش کنید"), variant: "destructive", }); } finally { setDeletingId(null); } }; if (loading) { return (
); } return (

مرکز آپلود نوشته

{post?.title ? `فایل‌های عمومی مرتبط با «${post.title}»` : "فایل‌های عمومی این نوشته را مدیریت کنید."}

آپلود فایل تصاویر، ویدئوها، اسناد و فایل‌های فشرده مجاز هستند. لینک مارک‌داون هر فایل بعد از آپلود قابل کپی است. {assets.length ? (
{assets.map((asset) => (
{asset.file_type}

{asset.title}

{asset.mime_type || "file"} · {Math.ceil(asset.size / 1024)} KB

{asset.absolute_preview_url ? ( {asset.alt_text ) : asset.absolute_file_url ? ( {asset.absolute_file_url} ) : null}
))}
) : (
هنوز فایلی برای این نوشته آپلود نشده است.
)}
); }