"use client"; import { useRef, useState } from "react"; import { Alert, Button, EmptyState, IconBtn, Select, Spinner } from "../UI"; import { useCarImages } from "@/src/hooks/useCars"; import { STAGE_MAP } from "@/src/types/car"; import type { Car, CarImage, ImageStage } from "@/src/types/car"; // ── Props ───────────────────────────────────────────────────────────────────── interface CarImageGalleryProps { car: Car; onClose: () => void; } // ── Component ───────────────────────────────────────────────────────────────── export function CarImageGallery({ car, onClose }: CarImageGalleryProps) { const [stage, setStage] = useState("GENERAL"); const [sortBy, setSortBy] = useState<"asc" | "desc">("desc"); const [lightbox, setLightbox] = useState(null); const [lightboxLoaded, setLightboxLoaded] = useState(false); const [loadedIds, setLoadedIds] = useState>(new Set()); const fileRef = useRef(null); const { images, loading, uploading, deleting, error, setError, uploadImages, deleteImage, } = useCarImages(car.id, sortBy); const handleUpload = async (e: React.ChangeEvent) => { const files = Array.from(e.target.files ?? []); if (!files.length) return; await uploadImages(files, stage); if (fileRef.current) fileRef.current.value = ""; }; const handleDelete = async (imageId: string) => { const ok = await deleteImage(imageId); if (ok && lightbox?.id === imageId) setLightbox(null); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Escape") { if (lightbox) setLightbox(null); else onClose(); } }; const imgSrc = (img: CarImage) => img.url ?? `/api/proxy/car-photos/${img.image}`; const markLoaded = (id: string) => setLoadedIds(prev => { const next = new Set(prev); next.add(id); return next; }); const openLightbox = (img: CarImage) => { setLightbox(img); setLightboxLoaded(false); }; return (
{ if (e.target === e.currentTarget) onClose(); }} style={{ position: "fixed", inset: 0, zIndex: 70, background: "rgba(15,23,42,0.7)", backdropFilter: "blur(6px)", display: "flex", flexDirection: "column", }} > {/* ── Header ── */}

معرض الصور

{car.manufacturer} {car.model} — {car.plateLetters} {car.plateNumber}

{/* Sort */} {/* Stage selector for upload */} {/* Upload button */}