const POSITION_GAP = 1000 export function computeReorderPositions< T extends { identifier: string; position: number }, >( items: T[], activeIdentifier: string, overIdentifier: string ): { itemIdentifiers: string[]; newPositions: number[] } | null { const oldIndex = items.findIndex((item) => item.identifier === activeIdentifier) const newIndex = items.findIndex((item) => item.identifier === overIdentifier) if (oldIndex === -1 || newIndex === -1 || oldIndex === newIndex) return null const reordered = [...items] const [moved] = reordered.splice(oldIndex, 1) reordered.splice(newIndex, 0, moved) const itemIdentifiers: string[] = [] const newPositions: number[] = [] for (let index = 0; index < reordered.length; index++) { const newPosition = (index + 1) * POSITION_GAP if (reordered[index].position !== newPosition) { itemIdentifiers.push(reordered[index].identifier) newPositions.push(newPosition) } } if (itemIdentifiers.length === 0) return null return { itemIdentifiers, newPositions } }