102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
"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 (
|
|
<div
|
|
ref={setNodeRef}
|
|
style={style}
|
|
className={cn(
|
|
"group rounded-md border cursor-grab active:cursor-grabbing select-none overflow-hidden",
|
|
"flex flex-col items-center justify-center gap-0.5",
|
|
"transition-shadow duration-150",
|
|
selected && "ring-2 ring-primary",
|
|
!selected && "hover:ring-1 hover:ring-primary/50",
|
|
)}
|
|
{...listeners}
|
|
{...attributes}
|
|
onClick={handleClick}
|
|
suppressHydrationWarning
|
|
>
|
|
<div className="absolute inset-0 opacity-20" style={{ backgroundColor: bgColor }} />
|
|
<div className="relative flex flex-col items-center justify-center gap-0.5 p-1 w-full h-full">
|
|
{resource.icon && typeof resource.icon === "object" && "url" in resource.icon && (
|
|
<img
|
|
src={resource.icon.url ?? ""}
|
|
alt={resource.name}
|
|
className="size-6 object-contain opacity-80"
|
|
/>
|
|
)}
|
|
<span className="text-[10px] leading-tight font-medium text-center text-foreground/90 line-clamp-2 px-0.5">
|
|
{resource.name}
|
|
</span>
|
|
{item.amount > 1 && (
|
|
<span
|
|
className="text-[9px] font-bold leading-none px-1 py-0.5 rounded-sm"
|
|
style={{ backgroundColor: bgColor, color: "white" }}
|
|
>
|
|
{item.amount.toLocaleString()}
|
|
</span>
|
|
)}
|
|
</div>
|
|
<div className="absolute top-0.5 right-0.5 opacity-0 group-hover:opacity-100 transition-opacity">
|
|
<GripVerticalIcon className="size-3 text-foreground/50" />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|