|
| 1 | +import { useTaskStore } from "@/stores"; |
| 2 | +import { GripVertical, Trash } from "lucide-react"; |
| 3 | +import { useRef } from "react"; |
| 4 | +import { useDrag, useDrop } from "react-dnd"; |
| 5 | + |
| 6 | +const DraggableTask = ({ task, index, moveTask }) => { |
| 7 | + const removeTask = useTaskStore((state) => state.removeTask); |
| 8 | + const ref = useRef(null); |
| 9 | + |
| 10 | + const [{ isDragging }, drag] = useDrag({ |
| 11 | + type: "TASK", |
| 12 | + item: { index }, |
| 13 | + collect: (monitor) => ({ |
| 14 | + isDragging: monitor.isDragging(), |
| 15 | + }), |
| 16 | + }); |
| 17 | + |
| 18 | + const [, drop] = useDrop({ |
| 19 | + accept: "TASK", |
| 20 | + hover: (draggedItem) => { |
| 21 | + if (draggedItem.index !== index) { |
| 22 | + moveTask(draggedItem.index, index); |
| 23 | + draggedItem.index = index; |
| 24 | + } |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + drag(drop(ref)); |
| 29 | + |
| 30 | + return ( |
| 31 | + <div |
| 32 | + ref={ref} |
| 33 | + className={`relative group h-10 transition-opacity duration-300 ${ |
| 34 | + isDragging ? "opacity-40" : "opacity-100" |
| 35 | + }`} |
| 36 | + > |
| 37 | + {/* <div className="absolute -left-5 hidden group-hover:flex h-full items-center"> |
| 38 | + <GripVertical className="size-5 text-white/50 hover:cursor-grab" /> |
| 39 | + </div> */} |
| 40 | + <div className="flex items-center justify-between gap-2 bg-white/5 rounded-lg px-3 py-2 h-full cursor-grab"> |
| 41 | + <div className="font-medium">{task.title}</div> |
| 42 | + <button |
| 43 | + className="text-white/50 hidden group-hover:block" |
| 44 | + onClick={() => removeTask(task.id)} |
| 45 | + > |
| 46 | + <Trash className="size-4" /> |
| 47 | + </button> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + ); |
| 51 | +}; |
| 52 | + |
| 53 | +export default DraggableTask; |
0 commit comments