All files / apps/host/src/components/AnimatedPendingRequestList/animations useSwipeGesture.ts

100% Statements 69/69
100% Branches 41/41
100% Functions 10/10
100% Lines 67/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                                                  3x                     26x 26x 26x   26x     26x 19x 3x 3x 3x 3x     16x         26x   1x         26x   2x         26x   5x         26x   26x     9x   8x   8x 8x   8x 8x 8x 8x     8x 1x 1x       7x 4x 4x 4x 4x       3x 3x 3x     10x   9x   9x 9x 9x     9x           9x 1x 1x 1x 1x 1x 1x     8x     8x 2x 2x 2x   2x       6x 3x 3x   3x       3x 3x 1x   2x         26x                    
import { useCallback, useEffect, useState } from 'react';
import { Gesture } from 'react-native-gesture-handler';
import {
  interpolate,
  runOnJS,
  SharedValue,
  useSharedValue,
  withTiming,
} from 'react-native-reanimated';
 
import { AnimatedPendingRequestCardData, SwipeDirection } from '@/types/card';
 
import {
  CARD_TYPE_NEW_REQUEST,
  HORIZONTAL_LIMIT,
  MIN_THRESHOLD,
  SWIPE_X_THRESHOLD,
  SWIPE_Y_THRESHOLD,
  VELOCITY_THRESHOLD,
} from '../constants';
 
export interface UseSwipeGestureProps {
  index: number;
  currentIndex: number;
  data: AnimatedPendingRequestCardData;
  width: number;
  height: number;
  animatedValue: SharedValue<number>;
  onSwipeLeftEnd?: (item: AnimatedPendingRequestCardData) => void;
  onSwipeRightEnd?: (item: AnimatedPendingRequestCardData) => void;
  onMoveToBack?: (item: AnimatedPendingRequestCardData) => void;
}
 
export const useSwipeGesture = ({
  index,
  currentIndex,
  data,
  width,
  height,
  animatedValue,
  onSwipeLeftEnd,
  onSwipeRightEnd,
  onMoveToBack,
}: UseSwipeGestureProps) => {
  const translateX = useSharedValue(0);
  const translateY = useSharedValue(0);
  const direction = useSharedValue(0);
 
  const [swipeDirection, setSwipeDirection] = useState<SwipeDirection>(null);
 
  // Reset all values when currentIndex changes (e.g., when looping back to start)
  useEffect(() => {
    if (currentIndex !== index) {
      translateX.value = 0;
      translateY.value = 0;
      direction.value = 0;
      setSwipeDirection(null);
    } else {
      // When item becomes current, ensure animatedValue is synced
      animatedValue.value = currentIndex;
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [currentIndex, index, animatedValue]);
 
  const onSwipeLeftHandler = useCallback(
    (item: AnimatedPendingRequestCardData) => {
      onSwipeLeftEnd?.(item);
    },
    [onSwipeLeftEnd],
  );
 
  const onSwipeRightHandler = useCallback(
    (item: AnimatedPendingRequestCardData) => {
      onSwipeRightEnd?.(item);
    },
    [onSwipeRightEnd],
  );
 
  const onMoveToBackHandler = useCallback(
    (item: AnimatedPendingRequestCardData) => {
      onMoveToBack?.(item);
    },
    [onMoveToBack],
  );
 
  const scrollGesture = Gesture.Pan();
 
  const pan = Gesture.Pan()
    .simultaneousWithExternalGesture(scrollGesture)
    .onUpdate(event => {
      if (currentIndex !== index) return;
 
      const { translationX: dx, translationY: dy } = event;
 
      translateX.value = dx;
      translateY.value = dy;
 
      const absDx = Math.abs(dx);
      const absDy = Math.abs(dy);
      const isVertical = absDy > absDx && absDy > MIN_THRESHOLD && absDx < HORIZONTAL_LIMIT;
      const isHorizontal = absDx > MIN_THRESHOLD;
 
      // Handle no clear direction - reset swipe indicator
      if (!isVertical && !isHorizontal) {
        runOnJS(setSwipeDirection)(null);
        return;
      }
 
      // Handle vertical swipe
      if (isVertical) {
        direction.value = dy > 0 ? 2 : -2;
        runOnJS(setSwipeDirection)(direction.value > 0 ? 'down' : 'up');
        animatedValue.value = interpolate(absDy, [0, height], [index, index + 1]);
        return;
      }
 
      // Handle horizontal swipe
      direction.value = dx > 0 ? 1 : -1;
      runOnJS(setSwipeDirection)(direction.value > 0 ? 'right' : 'left');
      animatedValue.value = interpolate(absDx, [0, width], [index, index + 1]);
    })
    .onEnd(event => {
      if (currentIndex !== index) return;
 
      const { translationX: dx, translationY: dy, velocityX, velocityY } = event;
 
      const absDx = Math.abs(dx);
      const absDy = Math.abs(dy);
      const isVertical = absDy > absDx && absDx < HORIZONTAL_LIMIT;
 
      const shouldSwipe =
        absDx > SWIPE_X_THRESHOLD ||
        absDy > SWIPE_Y_THRESHOLD ||
        Math.abs(velocityX) > VELOCITY_THRESHOLD ||
        Math.abs(velocityY) > VELOCITY_THRESHOLD;
 
      // Early return: Reset if swipe threshold not met
      if (!shouldSwipe) {
        translateX.value = withTiming(0);
        translateY.value = withTiming(0);
        animatedValue.value = withTiming(currentIndex);
        direction.value = 0;
        runOnJS(setSwipeDirection)(null);
        return;
      }
 
      const isLoopCard = data.type === CARD_TYPE_NEW_REQUEST;
 
      // Handle loop card swipe
      if (isLoopCard) {
        translateX.value = withTiming(0);
        translateY.value = withTiming(height * (direction.value >= 0 ? 0.5 : -0.5), {}, () => {
          runOnJS(onMoveToBackHandler)?.(data);
        });
        return;
      }
 
      // Handle vertical swipe
      if (isVertical) {
        translateY.value = withTiming(height * (direction.value >= 0 ? 0.5 : -0.5), {}, () => {
          runOnJS(onMoveToBackHandler)?.(data);
        });
        return;
      }
 
      // Handle horizontal swipe (left/right)
      translateX.value = withTiming(width * direction.value, {}, () => {
        if (direction.value < 0) {
          runOnJS(onSwipeLeftHandler)?.(data);
        } else {
          runOnJS(onSwipeRightHandler)(data);
        }
      });
    });
 
  return {
    pan,
    scrollGesture,
    swipeDirection,
    setSwipeDirection,
    translateX,
    translateY,
    direction,
  };
};