From c8e7c815a50ad48a5c9221d003c41fa23a410bd2 Mon Sep 17 00:00:00 2001 From: yjin Date: Wed, 18 Dec 2024 18:24:19 +0900 Subject: [PATCH] fix: [GSW-2032] Reduce cognitive complexity Refactor this function to reduce its Cognitive Complexity from 60 to the 15 allowed. Cognitive Complexity of functions should not be too high typescript:S3776 Software qualities impacted: Maintainability --- .../components/common/bar-graph/BarGraph.tsx | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/web/src/components/common/bar-graph/BarGraph.tsx b/packages/web/src/components/common/bar-graph/BarGraph.tsx index b2fae77da..1624e541b 100644 --- a/packages/web/src/components/common/bar-graph/BarGraph.tsx +++ b/packages/web/src/components/common/bar-graph/BarGraph.tsx @@ -162,13 +162,24 @@ const BarGraph: React.FC = ({ return getGraphPoints()[currentTick]; }, [currentTick, getGraphPoints]); + const getEventCoordinates = ( + event: React.MouseEvent | React.TouchEvent, + ) => { + const isTouch = event.type.startsWith("touch"); + const touch = isTouch ? (event as React.TouchEvent).touches[0] : null; + + return { + clientX: isTouch ? touch?.clientX : (event as React.MouseEvent).clientX, + clientY: isTouch ? touch?.clientY : (event as React.MouseEvent).clientY, + }; + }; + const onMouseMove = (event: React.MouseEvent | React.TouchEvent) => { event.preventDefault(); event.stopPropagation(); - const isTouch = event.type.startsWith("touch"); - const touch = isTouch ? (event as React.TouchEvent).touches[0] : null; - const clientX = isTouch ? touch?.clientX : (event as React.MouseEvent).clientX; - const clientY = isTouch ? touch?.clientY : (event as React.MouseEvent).clientY; + + const { clientX, clientY } = getEventCoordinates(event); + if (!activated) { setCurrentPointIndex(-1); return;