Skip to content

Commit

Permalink
fix: [GSW-2032] Reduce cognitive complexity
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tfrg committed Dec 18, 2024
1 parent bc3c92a commit c8e7c81
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/web/src/components/common/bar-graph/BarGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,24 @@ const BarGraph: React.FC<BarGraphProps> = ({
return getGraphPoints()[currentTick];
}, [currentTick, getGraphPoints]);

const getEventCoordinates = (
event: React.MouseEvent<HTMLDivElement, MouseEvent> | React.TouchEvent<HTMLDivElement>,
) => {
const isTouch = event.type.startsWith("touch");
const touch = isTouch ? (event as React.TouchEvent<HTMLDivElement>).touches[0] : null;

return {
clientX: isTouch ? touch?.clientX : (event as React.MouseEvent<HTMLDivElement, MouseEvent>).clientX,
clientY: isTouch ? touch?.clientY : (event as React.MouseEvent<HTMLDivElement, MouseEvent>).clientY,
};
};

const onMouseMove = (event: React.MouseEvent<HTMLDivElement, MouseEvent> | React.TouchEvent<HTMLDivElement>) => {
event.preventDefault();
event.stopPropagation();
const isTouch = event.type.startsWith("touch");
const touch = isTouch ? (event as React.TouchEvent<HTMLDivElement>).touches[0] : null;
const clientX = isTouch ? touch?.clientX : (event as React.MouseEvent<HTMLDivElement, MouseEvent>).clientX;
const clientY = isTouch ? touch?.clientY : (event as React.MouseEvent<HTMLDivElement, MouseEvent>).clientY;

const { clientX, clientY } = getEventCoordinates(event);

if (!activated) {
setCurrentPointIndex(-1);
return;
Expand Down

0 comments on commit c8e7c81

Please sign in to comment.