import { useRef } from 'react'; import { useDrag, useDrop } from 'react-dnd'; import type { Identifier, XYCoord } from 'dnd-core'; interface DraggableScenarioProps { id: string; text: string; index: number; moveScenario: (dragIndex: number, hoverIndex: number) => void; isSelected: boolean; } interface DragItem { index: number; id: string; type: string; } function DraggableScenario({ id, text, index, moveScenario, isSelected }: DraggableScenarioProps) { const ref = useRef(null); const [{ handlerId }, drop] = useDrop({ accept: 'scenario', collect(monitor: any) { return { handlerId: monitor.getHandlerId(), }; }, hover(item: DragItem, monitor: any) { if (!ref.current) { return; } const dragIndex = item.index; const hoverIndex = index; if (dragIndex === hoverIndex) { return; } const hoverBoundingRect = ref.current?.getBoundingClientRect(); const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2; const clientOffset = monitor.getClientOffset(); const hoverClientY = (clientOffset as XYCoord).y - hoverBoundingRect.top; if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) { return; } if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) { return; } moveScenario(dragIndex, hoverIndex); item.index = hoverIndex; }, }); const [{ isDragging }, drag] = useDrag({ type: 'scenario', item: () => { return { id, index }; }, collect: (monitor: any) => ({ isDragging: monitor.isDragging(), }), }); const opacity = isDragging ? 0 : 1; drag(drop(ref)); return (
drag_indicator {text}
); } export default DraggableScenario;