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

94.44% Statements 34/36
95.34% Branches 41/43
75% Functions 6/8
94.44% Lines 34/36

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                                                                                  3x                                     11x 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     8x             11x                       11x           2x 2x 1x                     1x                                                            
import { ComponentType, memo, useEffect } from 'react';
import { GestureDetector } from 'react-native-gesture-handler';
import Animated, {
  runOnJS,
  SharedValue,
  useSharedValue,
  withTiming,
} from 'react-native-reanimated';
import { StyleSheet, useWindowDimensions, View } 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: ComponentType<AnimatedPendingRequestCardData>;
  index: number;
  dataLength: number;
  currentIndex: number;
  animatedValue: SharedValue<number>;
  onSwipeLeftEnd?: (item: AnimatedPendingRequestCardData) => void;
  onSwipeRightEnd?: (item: AnimatedPendingRequestCardData) => void;
  action?: CardAction;
  listZIndex: SharedValue<number>;
  onMoveToBack?: (item: AnimatedPendingRequestCardData) => void;
  onMoveToNewRequest?: () => void;
  onPressCard?: (item: AnimatedPendingRequestCardData) => void;
  imageAnimationProgress?: SharedValue<number>;
  isNextItemNewRequest?: boolean;
  isCurrentItemNewRequest?: boolean;
  onActionComplete?: () => void;
}
 
export const AnimatedPendingRequestItem = memo(
  ({
    index,
    data,
    component: Component,
    currentIndex,
    animatedValue,
    listZIndex,
    onSwipeLeftEnd,
    onSwipeRightEnd,
    action,
    onMoveToBack,
    onMoveToNewRequest,
    onPressCard,
    imageAnimationProgress,
    isNextItemNewRequest: _isNextItemNewRequest = false,
    isCurrentItemNewRequest = false,
    onActionComplete,
  }: AnimatedItemProps) => {
    const { theme } = useTheme();
    const { width, height } = useWindowDimensions();
    const relativeIndex = index - currentIndex;
    // Top card gets full shadow; back cards get weaker shadows to separate visible page edges
    const shadowStyle =
      relativeIndex === 0
        ? getShadowStyle(theme)
        : {
            shadowOffset: { width: 0, height: 3 },
            shadowColor: theme.colors.shadow.black,
            shadowOpacity: 0.12,
            shadowRadius: 4,
            elevation: relativeIndex === 1 ? 15 : relativeIndex === 2 ? 8 : 3,
          };
 
    const { pan, scrollGesture, swipeDirection, setSwipeDirection, translateX, translateY } =
      useSwipeGesture({
        index,
        currentIndex,
        data,
        width,
        height,
        animatedValue,
        listZIndex,
        onSwipeLeftEnd,
        onSwipeRightEnd,
        onMoveToBack,
      });
 
    useImageAnimation({
      imageAnimationProgress,
      translateX,
      translateY,
      currentIndex,
      index,
      isCurrentItemNewRequest,
    });
 
    const itemHeight = useSharedValue(0);
 
    const animatedStyle = useStackAnimation({
      index,
      currentIndex,
      animatedValue,
      translateX,
      translateY,
      width,
      itemHeight,
    });
 
    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;
        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]);
 
    // Boost above mascot (zIndex 999) only while actively swiping
    const zIndex =
      relativeIndex === 0
        ? swipeDirection !== null
          ? 1000
          : 100
        : relativeIndex === 1
          ? 50
          : relativeIndex === 2
            ? 25
            : relativeIndex === 3
              ? 10
              : 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}
            translateX={translateX}
            translateY={translateY}
            swipeDirection={swipeDirection}
            scrollGesture={scrollGesture}
            onPress={
              data.type === CARD_TYPE_NEW_REQUEST ? onMoveToNewRequest : () => onPressCard?.(data)
            }
            onAccessibilityApprove={
              relativeIndex === 0 && !isCurrentItemNewRequest
                ? () => onSwipeRightEnd?.(data)
                : undefined
            }
            onAccessibilityReject={
              relativeIndex === 0 && !isCurrentItemNewRequest
                ? () => onSwipeLeftEnd?.(data)
                : undefined
            }
          />
          {relativeIndex > 0 && (
            <View
              pointerEvents="none"
              style={[
                StyleSheet.absoluteFillObject,
                {
                  backgroundColor: `rgba(0,0,0,${relativeIndex * 0.04})`,
                  borderRadius: theme.metrics.borderRadius[6],
                },
              ]}
            />
          )}
        </Animated.View>
      </GestureDetector>
    );
  },
);