All files / apps/host/src/components/AnimatedPendingRequestList AnimatedPendingRequestItem.tsx

100% Statements 35/35
100% Branches 22/22
100% Functions 6/6
100% Lines 35/35

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                                                                                3x                                 11x 11x 11x                   11x                       11x               11x   11x                   11x 11x 8x     3x   1x 1x   2x 2x 2x 2x 1x     2x     3x         11x 11x 3x 3x 3x 3x     8x         11x   11x   11x           2x 2x 1x                 1x              
import { JSX, useEffect } from 'react';
import { GestureDetector } from 'react-native-gesture-handler';
import Animated, {
  runOnJS,
  SharedValue,
  useSharedValue,
  withTiming,
} from 'react-native-reanimated';
import { useWindowDimensions } from 'react-native';
 
import { useTheme } from '@repo/ui/themes/ThemeContext';
 
import { CARD_ACTIONS } from '@/constants/card';
 
import { AnimatedPendingRequestCardData, CardAction } from '@/types/card';
 
import { useImageAnimation } from './animations/useImageAnimation';
import { useStackAnimation } from './animations/useStackAnimation';
import { useSwipeGesture } from './animations/useSwipeGesture';
import { CARD_TYPE_NEW_REQUEST, getShadowStyle } from './constants';
 
interface AnimatedItemProps {
  data: AnimatedPendingRequestCardData;
  component: (item: AnimatedPendingRequestCardData) => JSX.Element;
  index: number;
  dataLength: number;
  currentIndex: number;
  animatedValue: SharedValue<number>;
  onSwipeLeftEnd?: (item: AnimatedPendingRequestCardData) => void;
  onSwipeRightEnd?: (item: AnimatedPendingRequestCardData) => void;
  action?: CardAction;
  onMoveToBack?: (item: AnimatedPendingRequestCardData) => void;
  onMoveToNewRequest?: () => void;
  onPressCard?: (item: AnimatedPendingRequestCardData) => void;
  imageAnimationProgress?: SharedValue<number>;
  isNextItemNewRequest?: boolean;
  isCurrentItemNewRequest?: boolean;
  onActionComplete?: () => void;
}
 
export const AnimatedPendingRequestItem = ({
  index,
  data,
  component: Component,
  dataLength: _dataLength,
  currentIndex,
  animatedValue,
  onSwipeLeftEnd,
  onSwipeRightEnd,
  action,
  onMoveToBack,
  onMoveToNewRequest,
  onPressCard,
  imageAnimationProgress,
  isNextItemNewRequest = false,
  onActionComplete,
}: AnimatedItemProps) => {
  const { theme } = useTheme();
  const { width, height } = useWindowDimensions();
  const shadowStyle = getShadowStyle(theme);
 
  const {
    pan,
    scrollGesture,
    swipeDirection,
    setSwipeDirection,
    translateX,
    translateY,
    direction,
  } = useSwipeGesture({
    index,
    currentIndex,
    data,
    width,
    height,
    animatedValue,
    onSwipeLeftEnd,
    onSwipeRightEnd,
    onMoveToBack,
  });
 
  useImageAnimation({
    imageAnimationProgress,
    translateY,
    currentIndex,
    index,
    isNextItemNewRequest,
  });
 
  const itemHeight = useSharedValue(0);
 
  const animatedStyle = useStackAnimation({
    index,
    currentIndex,
    animatedValue,
    translateX,
    translateY,
    direction,
    width,
  });
 
  useEffect(() => {
    if (!action || action.type === CARD_ACTIONS.IDLE || action.id !== data.id) {
      return;
    }
 
    switch (action.type) {
      case CARD_ACTIONS.SUBMIT:
        translateX.value = withTiming(-width);
        break;
      case CARD_ACTIONS.CANCEL:
        translateX.value = withTiming(0);
        translateY.value = withTiming(0);
        animatedValue.value = withTiming(currentIndex, {}, () => {
          if (onActionComplete) {
            runOnJS(onActionComplete)();
          }
        });
        break;
    }
 
    setSwipeDirection(null);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [action, data.id, currentIndex, width, setSwipeDirection, onActionComplete]);
 
  // 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 relativeIndex = index - currentIndex;
  const zIndex =
    relativeIndex === 0 ? 100 : relativeIndex === 1 ? 50 : relativeIndex === 2 ? 25 : 0;
 
  return (
    <GestureDetector gesture={pan}>
      <Animated.View
        className="absolute w-full h-full"
        style={[{ zIndex, ...shadowStyle }, animatedStyle]}
        onLayout={e => {
          const h = e.nativeEvent.layout.height;
          if (itemHeight.value === 0) {
            itemHeight.value = h;
          }
        }}
      >
        <Component
          {...data}
          swipeDirection={swipeDirection}
          scrollGesture={scrollGesture}
          onPress={
            data.type === CARD_TYPE_NEW_REQUEST ? onMoveToNewRequest : () => onPressCard?.(data)
          }
        />
      </Animated.View>
    </GestureDetector>
  );
};