From 0b8c6ee5368e5ffd227f22801161a40370a1c491 Mon Sep 17 00:00:00 2001 From: one Date: Sun, 17 Aug 2025 21:34:37 +0800 Subject: [PATCH] perf(DraggableList): skip update if dnd to the same position (#9255) --- .../DraggableList/__tests__/DraggableList.test.tsx | 6 ++---- src/renderer/src/components/DraggableList/list.tsx | 6 ++++-- src/renderer/src/components/DraggableList/virtual-list.tsx | 6 ++++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/renderer/src/components/DraggableList/__tests__/DraggableList.test.tsx b/src/renderer/src/components/DraggableList/__tests__/DraggableList.test.tsx index a570f58bcf..ed53736ed5 100644 --- a/src/renderer/src/components/DraggableList/__tests__/DraggableList.test.tsx +++ b/src/renderer/src/components/DraggableList/__tests__/DraggableList.test.tsx @@ -157,8 +157,7 @@ describe('DraggableList', () => { // 模拟拖拽到自身 window.triggerOnDragEnd({ source: { index: 1 }, destination: { index: 1 } }, {}) - expect(onUpdate).toHaveBeenCalledTimes(1) - expect(onUpdate.mock.calls[0][0]).toEqual(list) + expect(onUpdate).toHaveBeenCalledTimes(0) }) }) @@ -175,8 +174,7 @@ describe('DraggableList', () => { // 拖拽自身 window.triggerOnDragEnd({ source: { index: 0 }, destination: { index: 0 } }, {}) - expect(onUpdate).toHaveBeenCalledTimes(1) - expect(onUpdate.mock.calls[0][0]).toEqual(list) + expect(onUpdate).toHaveBeenCalledTimes(0) }) it('should not crash if callbacks are undefined', () => { diff --git a/src/renderer/src/components/DraggableList/list.tsx b/src/renderer/src/components/DraggableList/list.tsx index 0f23a69978..18bf7a6d20 100644 --- a/src/renderer/src/components/DraggableList/list.tsx +++ b/src/renderer/src/components/DraggableList/list.tsx @@ -38,8 +38,10 @@ const DraggableList: FC> = ({ if (result.destination) { const sourceIndex = result.source.index const destIndex = result.destination.index - const reorderAgents = droppableReorder(list, sourceIndex, destIndex) - onUpdate(reorderAgents) + if (sourceIndex !== destIndex) { + const reorderAgents = droppableReorder(list, sourceIndex, destIndex) + onUpdate(reorderAgents) + } } } diff --git a/src/renderer/src/components/DraggableList/virtual-list.tsx b/src/renderer/src/components/DraggableList/virtual-list.tsx index b8020aa051..69b0ced667 100644 --- a/src/renderer/src/components/DraggableList/virtual-list.tsx +++ b/src/renderer/src/components/DraggableList/virtual-list.tsx @@ -82,8 +82,10 @@ function DraggableVirtualList({ if (onUpdate && result.destination) { const sourceIndex = result.source.index const destIndex = result.destination.index - const reorderAgents = droppableReorder(list, sourceIndex, destIndex) - onUpdate(reorderAgents) + if (sourceIndex !== destIndex) { + const reorderAgents = droppableReorder(list, sourceIndex, destIndex) + onUpdate(reorderAgents) + } } }