From 0a0e0383b3d90f4814ffd02a6418ba9828dccc2b Mon Sep 17 00:00:00 2001 From: ash1shkumar Date: Sat, 6 Jun 2026 00:54:31 +0530 Subject: [PATCH] refactor: optimize kanban task ordering reconciliation --- .../app/components/kanban/KanbanBoard.tsx | 50 +++++++++++++------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/frontend/app/components/kanban/KanbanBoard.tsx b/frontend/app/components/kanban/KanbanBoard.tsx index 9568da5..a1ef4ac 100644 --- a/frontend/app/components/kanban/KanbanBoard.tsx +++ b/frontend/app/components/kanban/KanbanBoard.tsx @@ -211,6 +211,31 @@ export function KanbanBoard() { ); } + function reconcileTaskPositions( + tasks: Task[], + status: TaskStatus + ) { + return tasks + .filter((task) => task.status === status) + .sort((a, b) => a.position - b.position) + .map((task, index) => ({ + ...task, + position: index, + })); + } + + function shouldSyncTaskUpdate( + originalTask: Task, + updatedTask: Task + ) { + return ( + originalTask.status !== + updatedTask.status || + originalTask.position !== + updatedTask.position + ); + } + const handleDragEnd = async (event: DragEndEvent) => { if (isSyncingRef.current) return; @@ -258,27 +283,24 @@ export function KanbanBoard() { ); // Get tasks of affected column - const columnTasks = updatedTasks - .filter((task) => task.status === newStatus) - .sort((a, b) => a.position - b.position); - - // Reassign positions sequentially - const reorderedTasks = columnTasks - .map((task, index) => ({ - ...task, - position: index, - })) - .filter((task) => { + const reconciledTasks = + reconcileTaskPositions( + updatedTasks, + newStatus + ); + + const reorderedTasks = + reconciledTasks.filter( + (task) => { const originalTask = tasks.find( (t) => t.id === task.id ); return Boolean( originalTask && - hasTaskPositionChanged( + shouldSyncTaskUpdate( originalTask, - task.status, - task.position + task ) ); });