"use client"; import { useCallback, useEffect, useRef } from "react"; import { useDraggable } from "@dnd-kit/core"; import { CSS } from "@dnd-kit/utilities"; import { Resource } from "@/payload-types"; import { getItemFootprint, type GridItem as GridItemType } from "@/lib/logistics"; import { cn } from "@/lib/utils"; import { GripVerticalIcon } from "lucide-react"; interface GridItemProps { item: GridItemType; cellSize: number; onSelect: (item: GridItemType) => void; selected: boolean; } export function GridItemComponent({ item, cellSize, onSelect, selected }: GridItemProps) { const resource = item.resource as Resource; const { width, height } = getItemFootprint(resource, item.rotated); const didDrag = useRef(false); const { attributes, listeners, setNodeRef, transform, isDragging } = useDraggable({ id: `grid-item-${item.id}`, data: { item }, }); useEffect(() => { if (isDragging) { didDrag.current = true; } }, [isDragging]); const handleClick = useCallback( (e: React.MouseEvent) => { if (!didDrag.current) { e.stopPropagation(); onSelect(item); } didDrag.current = false; }, [item, onSelect], ); const style: React.CSSProperties = { position: "absolute", left: item.gridX * cellSize, top: item.gridY * cellSize, width: width * cellSize - 2, height: height * cellSize - 2, zIndex: 10, transition: isDragging ? "none" : "left 0.15s ease, top 0.15s ease", transform: CSS.Translate.toString(transform), opacity: isDragging ? 0 : 1, }; const bgColor = resource.color ?? "oklch(0.269 0 0)"; return (
{resource.icon && typeof resource.icon === "object" && "url" in resource.icon && ( {resource.name} )} {resource.name} {item.amount > 1 && ( {item.amount.toLocaleString()} )}
); }