import { IconPhotoUp, IconPlus, IconTrash } from "@tabler/icons-react"; import { useRef, useState } from "react"; import { MAX_UPLOAD_BYTES } from "@/lib/upload"; /** * แก้รายการรูปโรงหนัง (อาร์เรย์ `photos`) * * รองรับ 3 ทาง: * 1) อัปโหลดไฟล์จากเครื่อง (ปุ่ม / ลากมาวาง / Ctrl+V) → เก็บที่ data/uploads/cinemas * 2) วางลิงก์หรือพาธเอง (`/images/cinemas/x.webp` หรือ URL ภายนอก) * 3) สลับลำดับด้วย ↑/↓ · รูปแรก = ภาพหลักของแกลเลอรี */ type Props = { photos: string[]; onChange: (next: string[]) => void; inputClass: string; labelClass: string; /** slug ของโรงที่กำลังแก้ — ใช้ตั้งชื่อไฟล์อัปโหลด */ cinemaSlug?: string; }; const smallButtonClass = "flex min-h-9 items-center gap-1 rounded-lg border border-[#303030] px-2 text-xs text-[#999] transition hover:border-[#A92626] hover:text-[#D47272] disabled:opacity-50"; const uploadButtonClass = "flex min-h-10 w-fit items-center gap-2 rounded-lg border border-[#A92626]/60 bg-[#1B1B1B] px-3 text-sm text-white transition hover:border-[#A92626] hover:text-[#D47272] disabled:opacity-50"; export function PhotosEditor({ photos, onChange, inputClass, labelClass, cinemaSlug, }: Props) { const fileRef = useRef(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [dragging, setDragging] = useState(false); function updateAt(index: number, value: string) { onChange(photos.map((item, i) => (i === index ? value : item))); } async function uploadFiles(files: FileList | File[]) { const list = Array.from(files).filter((file) => file.size > 0); if (list.length === 0) return; const tooBig = list.find((file) => file.size > MAX_UPLOAD_BYTES); if (tooBig) { setError( `ไฟล์ "${tooBig.name}" ใหญ่เกิน ${Math.round(MAX_UPLOAD_BYTES / 1024 / 1024)} MB`, ); return; } setBusy(true); setError(null); const uploaded: string[] = []; try { for (const file of list) { const form = new FormData(); form.append("file", file); if (cinemaSlug) form.append("slug", cinemaSlug); const res = await fetch("/api/admin/uploads", { method: "POST", body: form, }); if (!res.ok) { const message = await res.text(); throw new Error(message || `อัปโหลดไม่สำเร็จ (${res.status})`); } const data = (await res.json()) as { url?: string }; if (!data.url) throw new Error("เซิร์ฟเวอร์ไม่ได้คืน URL ของรูป"); uploaded.push(data.url); } if (uploaded.length > 0) onChange([...photos, ...uploaded]); } catch (uploadError) { setError( uploadError instanceof Error ? uploadError.message : "อัปโหลดไม่สำเร็จ", ); } finally { setBusy(false); if (fileRef.current) fileRef.current.value = ""; } } return (
{ if (!event.dataTransfer?.types.includes("Files")) return; event.preventDefault(); setDragging(true); }} onDragLeave={() => setDragging(false)} onDrop={(event) => { if (!event.dataTransfer?.files?.length) return; event.preventDefault(); setDragging(false); void uploadFiles(event.dataTransfer.files); }} onPaste={(event) => { const files = Array.from(event.clipboardData?.files ?? []); if (files.length === 0) return; event.preventDefault(); void uploadFiles(files); }} > รูปภาพโรงหนัง {photos.length === 0 ? ( ยังไม่มีรูป — อัปโหลดจากเครื่อง หรือวาง URL ด้านล่าง (ภาพที่ 1/2/3) ) : null} {photos.map((photo, index) => (
{photo ? ( // eslint-disable-next-line jsx-a11y/alt-text ) : null}
))}
ลากไฟล์มาวาง หรือ Ctrl+V ในกล่องนี้ · JPG/PNG/WebP/AVIF ≤{" "} {Math.round(MAX_UPLOAD_BYTES / 1024 / 1024)} MB
{ if (event.target.files) void uploadFiles(event.target.files); }} /> {error ? ( {error} ) : null}
); }