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

100% Statements 79/79
100% Branches 45/45
100% Functions 9/9
100% Lines 76/76

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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                                                                              3x                       30x 30x 30x   30x     30x 22x 4x 4x 4x 4x     18x         30x   1x         30x   2x         30x   5x         30x   30x       10x   9x   9x 9x   9x 9x 9x 9x     9x 1x 1x       8x 2x       8x 4x 4x 4x 4x       4x 4x 4x     10x   9x   9x 9x 9x     9x           9x 1x 1x 1x 1x 1x 1x     8x     8x 2x 2x 2x 2x       2x       6x 3x 3x 3x       3x         3x 1x   2x   3x 3x 3x       3x     2x 1x     30x                    
import { useCallback, useEffect, useState } from 'react';
import { Gesture } from 'react-native-gesture-handler';
import {
  interpolate,
  runOnJS,
  SharedValue,
  useSharedValue,
  withSpring,
  withTiming,
} from 'react-native-reanimated';
 
import { AnimatedPendingRequestCardData, SwipeDirection } from '@/types/card';
 
import {
  CARD_TYPE_NEW_REQUEST,
  EXIT_SPRING,
  EXIT_X_MULTIPLIER,
  HORIZONTAL_LIMIT,
  MIN_THRESHOLD,
  SNAP_BACK_SPRING,
  STEP_UP_SPRING,
  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>;
  listZIndex: SharedValue<number>;
  onSwipeLeftEnd?: (item: AnimatedPendingRequestCardData) => void;
  onSwipeRightEnd?: (item: AnimatedPendingRequestCardData) => void;
  onMoveToBack?: (item: AnimatedPendingRequestCardData) => void;
}
 
export const useSwipeGesture = ({
  index,
  currentIndex,
  data,
  width,
  height,
  animatedValue,
  listZIndex,
  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)
    .onBegin(() => {})
    .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;
      }
 
      // Confirmed swipe intent — raise list above mascot now (not on mere touch)
      if (listZIndex.value === 0) {
        listZIndex.value = 100;
      }
 
      // 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 = withSpring(0, { ...SNAP_BACK_SPRING, velocity: velocityX });
        translateY.value = withSpring(0, { ...SNAP_BACK_SPRING, velocity: velocityY });
        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) {
        runOnJS(onMoveToBackHandler)?.(data);
        animatedValue.value = withSpring(index + 1, STEP_UP_SPRING);
        translateX.value = withSpring(0, SNAP_BACK_SPRING);
        translateY.value = withSpring(height * (direction.value >= 0 ? 0.5 : -0.5), {
          ...EXIT_SPRING,
          velocity: velocityY,
        });
        return;
      }
 
      // Handle vertical swipe
      if (isVertical) {
        runOnJS(onMoveToBackHandler)?.(data);
        animatedValue.value = withSpring(index + 1, STEP_UP_SPRING);
        translateY.value = withSpring(height * (direction.value >= 0 ? 0.5 : -0.5), {
          ...EXIT_SPRING,
          velocity: velocityY,
        });
        return;
      }
 
      // Handle horizontal swipe (left/right) — fire callback immediately so data
      // updates while animation plays independently (Slack-style decoupling)
      if (direction.value < 0) {
        runOnJS(onSwipeLeftHandler)?.(data);
      } else {
        runOnJS(onSwipeRightHandler)(data);
      }
      const exitX = width * direction.value * EXIT_X_MULTIPLIER;
      animatedValue.value = withSpring(index + 1, STEP_UP_SPRING);
      translateY.value = withSpring(translateY.value + velocityY * 0.03, {
        ...EXIT_SPRING,
        velocity: velocityY,
      });
      translateX.value = withSpring(exitX, { ...EXIT_SPRING, velocity: velocityX });
    })
    .onFinalize(() => {
      if (currentIndex !== index) return;
      listZIndex.value = 0;
    });
 
  return {
    pan,
    scrollGesture,
    swipeDirection,
    setSwipeDirection,
    translateX,
    translateY,
    direction,
  };
};