|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { useRef } from 'react'; |
| 4 | +import { useDrag, useDrop } from 'react-dnd'; |
| 5 | +import { DragDropItem } from '../../utils/dragDropUtils'; |
| 6 | + |
| 7 | +export const DRAG_ITEM_TYPE = 'COURSE_CONTENT_ITEM'; |
| 8 | + |
| 9 | +interface SortableListProps { |
| 10 | + zoneId: string; |
| 11 | + items: DragDropItem[]; |
| 12 | + onReorder: (zoneId: string, fromIndex: number, toIndex: number) => void; |
| 13 | + onMoveToZone: (itemId: string, fromZoneId: string, toZoneId: string, toIndex?: number) => void; |
| 14 | + emptyText?: string; |
| 15 | +} |
| 16 | + |
| 17 | +interface DragPayload { |
| 18 | + id: string; |
| 19 | + fromZoneId: string; |
| 20 | + index: number; |
| 21 | + title: string; |
| 22 | +} |
| 23 | + |
| 24 | +const SortableRow = ({ |
| 25 | + item, |
| 26 | + index, |
| 27 | + zoneId, |
| 28 | + onReorder, |
| 29 | + onMoveToZone, |
| 30 | +}: { |
| 31 | + item: DragDropItem; |
| 32 | + index: number; |
| 33 | + zoneId: string; |
| 34 | + onReorder: (zoneId: string, fromIndex: number, toIndex: number) => void; |
| 35 | + onMoveToZone: (itemId: string, fromZoneId: string, toZoneId: string, toIndex?: number) => void; |
| 36 | +}) => { |
| 37 | + const ref = useRef<HTMLDivElement>(null); |
| 38 | + |
| 39 | + const [{ isDragging }, drag] = useDrag(() => ({ |
| 40 | + type: DRAG_ITEM_TYPE, |
| 41 | + item: { |
| 42 | + id: item.id, |
| 43 | + fromZoneId: zoneId, |
| 44 | + index, |
| 45 | + title: item.title, |
| 46 | + } satisfies DragPayload, |
| 47 | + collect: (monitor) => ({ |
| 48 | + isDragging: monitor.isDragging(), |
| 49 | + }), |
| 50 | + }), [index, item.id, item.title, zoneId]); |
| 51 | + |
| 52 | + const [, drop] = useDrop( |
| 53 | + () => ({ |
| 54 | + accept: DRAG_ITEM_TYPE, |
| 55 | + hover: (dragged: DragPayload, monitor) => { |
| 56 | + if (!ref.current) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (dragged.fromZoneId !== zoneId) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (dragged.index === index) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const hoverRect = ref.current.getBoundingClientRect(); |
| 69 | + const hoverMiddleY = (hoverRect.bottom - hoverRect.top) / 2; |
| 70 | + const clientOffset = monitor.getClientOffset(); |
| 71 | + if (!clientOffset) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const hoverClientY = clientOffset.y - hoverRect.top; |
| 76 | + |
| 77 | + if (dragged.index < index && hoverClientY < hoverMiddleY) { |
| 78 | + return; |
| 79 | + } |
| 80 | + if (dragged.index > index && hoverClientY > hoverMiddleY) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + onReorder(zoneId, dragged.index, index); |
| 85 | + dragged.index = index; |
| 86 | + }, |
| 87 | + drop: (dragged: DragPayload) => { |
| 88 | + if (dragged.fromZoneId !== zoneId) { |
| 89 | + onMoveToZone(dragged.id, dragged.fromZoneId, zoneId, index); |
| 90 | + dragged.fromZoneId = zoneId; |
| 91 | + dragged.index = index; |
| 92 | + } |
| 93 | + }, |
| 94 | + }), |
| 95 | + [index, onMoveToZone, onReorder, zoneId], |
| 96 | + ); |
| 97 | + |
| 98 | + drag(drop(ref)); |
| 99 | + |
| 100 | + return ( |
| 101 | + <div |
| 102 | + ref={ref} |
| 103 | + className={`mb-2 rounded-md border bg-white px-3 py-2 text-sm shadow-sm transition ${ |
| 104 | + isDragging ? 'opacity-50' : 'opacity-100' |
| 105 | + }`} |
| 106 | + > |
| 107 | + <div className="font-medium text-slate-800">{item.title}</div> |
| 108 | + <div className="mt-1 text-xs text-slate-500">#{item.order + 1}</div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +}; |
| 112 | + |
| 113 | +export const SortableList = ({ |
| 114 | + zoneId, |
| 115 | + items, |
| 116 | + onReorder, |
| 117 | + onMoveToZone, |
| 118 | + emptyText = 'Drop content here', |
| 119 | +}: SortableListProps) => { |
| 120 | + if (items.length === 0) { |
| 121 | + return ( |
| 122 | + <div className="rounded-md border border-dashed border-slate-300 bg-slate-50 p-4 text-center text-sm text-slate-500"> |
| 123 | + {emptyText} |
| 124 | + </div> |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + return ( |
| 129 | + <div> |
| 130 | + {items.map((item, index) => ( |
| 131 | + <SortableRow |
| 132 | + key={item.id} |
| 133 | + item={item} |
| 134 | + index={index} |
| 135 | + zoneId={zoneId} |
| 136 | + onReorder={onReorder} |
| 137 | + onMoveToZone={onMoveToZone} |
| 138 | + /> |
| 139 | + ))} |
| 140 | + </div> |
| 141 | + ); |
| 142 | +}; |
0 commit comments