import {useState} from 'react'; import {createPortal} from 'react-dom'; import {DndContext, DragOverlay} from '@dnd-kit/core'; import {arrayMove, SortableContext, verticalListSortingStrategy} from '@dnd-kit/sortable'; import {SamplingRule} from 'sentry/types/sampling'; import {DraggableRuleListItem, DraggableRuleListItemProps} from './draggableRuleListItem'; import { DraggableRuleListSortableItem, SortableItemProps, } from './draggableRuleListSortableItem'; import {isUniformRule} from './utils'; export type DraggableRuleListUpdateItemsProps = { activeIndex: string; overIndex: string; reorderedItems: Array; }; type Props = Pick & Pick & { items: Array< Omit & { id: string; } >; onUpdateItems: (props: DraggableRuleListUpdateItemsProps) => void; }; type State = { activeId?: string; }; export function DraggableRuleList({ items, onUpdateItems, renderItem, disabled = false, wrapperStyle = () => ({}), }: Props) { const [state, setState] = useState({}); const itemIds = items.map(item => item.id); const getIndex = itemIds.indexOf.bind(itemIds); const activeIndex = state.activeId ? getIndex(state.activeId) : -1; return ( { if (!active) { return; } setState({activeId: active.id}); }} onDragEnd={({over}) => { setState({activeId: undefined}); if (over) { const overIndex = getIndex(over.id); if (activeIndex !== overIndex) { onUpdateItems({ activeIndex, overIndex, reorderedItems: arrayMove(itemIds, activeIndex, overIndex), }); } } }} onDragCancel={() => setState({activeId: undefined})} > {itemIds.map((itemId, index) => ( ))} {createPortal( {state.activeId ? ( ) : null} , document.body )} ); }