import { useState } from "react"; import { DndContext, KeyboardSensor, MouseSensor, TouchSensor, closestCenter, useSensor, useSensors } from "@dnd-kit/core"; import { SortableContext, sortableKeyboardCoordinates, useSortable, verticalListSortingStrategy } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import { mediaUrl } from "../api"; import Icon from "./Icon"; const verticalDrag = ({ transform }) => ({ ...transform, x: 0 }); function SortableAttachment({ file, busy, canSort, sorting, onRemove, formatSize }) { const { attributes, listeners, setNodeRef, setActivatorNodeRef, transform, transition, isDragging } = useSortable({ id: file.url, disabled: !canSort, transition: { duration: 180, easing: "ease" }, }); return
  • event.preventDefault()} onMouseDown={(event) => { // Keep touch scrolling available on the row, with dragging on the handle. if (!event.target.closest("button")) listeners?.onMouseDown?.(event); }}> {file.media_type.startsWith("image/") ? : }
    {file.name}{formatSize(file.size)} · Ready
  • ; } export default function AttachmentList({ attachments, busy, onMove, onRemove, formatSize }) { const [activeId, setActiveId] = useState(null); const sensors = useSensors( useSensor(MouseSensor, { activationConstraint: { distance: 6 } }), useSensor(TouchSensor, { activationConstraint: { delay: 150, tolerance: 5 } }), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates, scrollBehavior: "auto" }), ); const describe = (id) => attachments.find((file) => file.url === id)?.name ?? "File"; const position = (id) => attachments.findIndex((file) => file.url === id) + 1; return <> {attachments.length > 1 &&

    Drag to reorder. Keyboard: Space to pick up, ↑/↓ to move, Space to drop.

    } `Picked up ${describe(active.id)}, position ${position(active.id)} of ${attachments.length}.`, onDragOver: ({ active, over }) => over ? `${describe(active.id)}, position ${position(over.id)} of ${attachments.length}.` : undefined, onDragEnd: ({ active, over }) => over ? `Dropped ${describe(active.id)} at position ${position(over.id)} of ${attachments.length}.` : "Reordering canceled.", onDragCancel: () => "Reordering canceled. File order unchanged.", }, }} onDragStart={({ active }) => setActiveId(active.id)} onDragCancel={() => setActiveId(null)} onDragEnd={({ active, over }) => { setActiveId(null); if (!busy && over && active.id !== over.id) onMove(active.id, over.id); }}> file.url)} strategy={verticalListSortingStrategy}> ; }