import { useState, useCallback, useRef } from "react"; import { Upload, Check, Image as ImageIcon, ChevronDown, ChevronRight, FileArchive, X, AlertCircle } from "lucide-react"; import { motion, AnimatePresence } from "framer-motion"; import { Button } from "@/components/ui/button"; import JSZip from "jszip"; const ZONE_LABELS = [ "Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right", ]; const BLOCK_LABELS = [ "Top Left", "Top Center", "Top Right", "Middle Left", "Middle Center", "Middle Right", "Bottom Left", "Bottom Center", "Bottom Right", ]; const MIN_IMAGES_PER_ZONE = 36; // 4 per block × 9 blocks const MAX_IMAGES_PER_ZONE = 50; const BLOCKS_PER_ZONE = 9; interface BlockData { files: File[]; previews: string[]; } interface ZoneData { blocks: BlockData[]; } const emptyZone = (): ZoneData => ({ blocks: Array.from({ length: BLOCKS_PER_ZONE }, () => ({ files: [], previews: [] })), }); const getTotalImages = (zone: ZoneData) => zone.blocks.reduce((sum, b) => sum + b.files.length, 0); const FieldGrid = () => { const [zones, setZones] = useState( Array.from({ length: 9 }, emptyZone) ); const [expandedZone, setExpandedZone] = useState(null); const [uploading, setUploading] = useState(false); const zipInputRef = useRef(null); const handleBlockUpload = useCallback( (zoneIdx: number, blockIdx: number, e: React.ChangeEvent) => { const fileList = e.target.files; if (!fileList) return; const newFiles = Array.from(fileList).filter((f) => f.type.startsWith("image/")); const newPreviews = newFiles.map((f) => URL.createObjectURL(f)); setZones((prev) => { const updated = structuredClone(prev); const block = updated[zoneIdx].blocks[blockIdx]; block.files = [...block.files, ...newFiles]; block.previews = [...block.previews, ...newPreviews]; // Trim zone to max const total = getTotalImages(updated[zoneIdx]); if (total > MAX_IMAGES_PER_ZONE) { // Remove excess from this block const excess = total - MAX_IMAGES_PER_ZONE; block.files = block.files.slice(0, Math.max(0, block.files.length - excess)); block.previews = block.previews.slice(0, Math.max(0, block.previews.length - excess)); } return updated; }); }, [] ); const removeImage = (zoneIdx: number, blockIdx: number, imgIdx: number) => { setZones((prev) => { const updated = structuredClone(prev); const block = updated[zoneIdx].blocks[blockIdx]; URL.revokeObjectURL(block.previews[imgIdx]); block.files.splice(imgIdx, 1); block.previews.splice(imgIdx, 1); return updated; }); }; const handleZipUpload = useCallback(async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setUploading(true); try { const zip = await JSZip.loadAsync(file); const updated = Array.from({ length: 9 }, emptyZone); // Parse structure: */zone_N/block_N/image.jpg const imageEntries: { zoneIdx: number; blockIdx: number; entry: JSZip.JSZipObject }[] = []; zip.forEach((relativePath, entry) => { if (entry.dir) return; const ext = relativePath.toLowerCase(); if (!ext.match(/\.(jpg|jpeg|png|webp|bmp|tiff?)$/)) return; const parts = relativePath.split("/").filter(Boolean); // Find zone_N and block_N in path let zoneIdx = -1; let blockIdx = -1; for (const part of parts) { const zoneMatch = part.match(/zone[_\-\s]?(\d+)/i); if (zoneMatch) zoneIdx = parseInt(zoneMatch[1]) - 1; const blockMatch = part.match(/block[_\-\s]?(\d+)/i); if (blockMatch) blockIdx = parseInt(blockMatch[1]) - 1; } if (zoneIdx >= 0 && zoneIdx < 9 && blockIdx >= 0 && blockIdx < 9) { imageEntries.push({ zoneIdx, blockIdx, entry }); } }); // Process all images in parallel await Promise.all( imageEntries.map(async ({ zoneIdx, blockIdx, entry }) => { const blob = await entry.async("blob"); const fileName = entry.name.split("/").pop() || "image.jpg"; const imgFile = new File([blob], fileName, { type: "image/jpeg" }); const preview = URL.createObjectURL(imgFile); const block = updated[zoneIdx].blocks[blockIdx]; block.files.push(imgFile); block.previews.push(preview); }) ); // Trim each zone to max for (const zone of updated) { let total = getTotalImages(zone); let blockIdx = BLOCKS_PER_ZONE - 1; while (total > MAX_IMAGES_PER_ZONE && blockIdx >= 0) { const excess = total - MAX_IMAGES_PER_ZONE; const block = zone.blocks[blockIdx]; const toRemove = Math.min(excess, block.files.length); block.files = block.files.slice(0, block.files.length - toRemove); block.previews = block.previews.slice(0, block.previews.length - toRemove); total = getTotalImages(zone); blockIdx--; } } setZones(updated); } catch (err) { console.error("ZIP parse error:", err); } finally { setUploading(false); if (zipInputRef.current) zipInputRef.current.value = ""; } }, []); const totalImages = zones.reduce((s, z) => s + getTotalImages(z), 0); const filledZones = zones.filter((z) => getTotalImages(z) >= MIN_IMAGES_PER_ZONE).length; return (

Field Zone Upload Grid

9 Zones · 9 Blocks each · {MIN_IMAGES_PER_ZONE}–{MAX_IMAGES_PER_ZONE} images per zone
{totalImages} images · {filledZones}/9 zones ready
{/* ZIP structure hint */}

ZIP structure: field_name/ → zone_1/ → block_1/ → image1.jpg (4 imgs per block × 9 blocks × 9 zones)

{/* 3×3 zone grid */}
{zones.map((zone, zoneIdx) => { const total = getTotalImages(zone); const isComplete = total >= MIN_IMAGES_PER_ZONE; const isExpanded = expandedZone === zoneIdx; const filledBlocks = zone.blocks.filter((b) => b.files.length > 0).length; return ( 0 ? "border-primary/40 bg-primary/5" : "border-border bg-card" }`} > {/* Progress bar */}
{total > 0 && total < MIN_IMAGES_PER_ZONE && (
Need {MIN_IMAGES_PER_ZONE - total} more images
)} ); })}
{/* Expanded zone detail */} {expandedZone !== null && (

Zone {expandedZone + 1} — {ZONE_LABELS[expandedZone]}

{getTotalImages(zones[expandedZone])} images · Upload 4 images per block

{/* 3×3 block grid */}
{zones[expandedZone].blocks.map((block, blockIdx) => { const hasImages = block.files.length > 0; return (
= 4 ? "border-success/50 bg-success/5" : hasImages ? "border-primary/30 bg-primary/5" : "border-dashed border-border" }`} >
B{blockIdx + 1} · {BLOCK_LABELS[blockIdx]} {block.files.length} imgs
{/* Image thumbnails */} {hasImages && (
{block.previews.slice(0, 4).map((preview, imgIdx) => (
))} {block.files.length > 4 && (
+{block.files.length - 4} more
)}
)} {/* Upload button */}
); })}
)}
); }; export default FieldGrid;