All files / src/components/search-bar RouteRow.tsx

100% Statements 22/22
62.5% Branches 5/8
100% Functions 7/7
100% Lines 20/20

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                          1x                                                       16x 16x 16x   16x       1x     3x 3x           4x 4x 4x 4x 2x       1x 1x 1x     16x             16x       16x                                                                     1x                                                            
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
import Animated, {
  interpolate,
  SharedValue,
  useAnimatedStyle,
  useSharedValue,
  withSpring,
} from "react-native-reanimated";
import { Ionicons } from "@expo/vector-icons";
import { useTheme } from "../../hooks/useTheme";
 
const SWAP_THRESHOLD = 42;
 
interface RouteRowProps {
  readonly label: "From" | "To";
  readonly value: string;
  readonly onSwap: () => void;
  readonly trailingIcon?: React.ComponentProps<typeof Ionicons>["name"];
  /**
   * Shared value owned by RouteCard.
   * This row WRITES to it while being dragged (0 → 1).
   * The sibling row READS from it to grey itself out.
   */
  readonly dragProgress: SharedValue<number>;
  /**
   * Shared value owned by RouteCard, driven by the *sibling* row.
   * This row READS from it to know when to fade out.
   */
  readonly siblingDragProgress: SharedValue<number>;
}
 
export default function RouteRow({
  label,
  value,
  onSwap,
  trailingIcon,
  dragProgress,
  siblingDragProgress,
}: RouteRowProps) {
  const { colors } = useTheme();
  const translateY = useSharedValue(0);
  const scale = useSharedValue(1);
 
  const pan = Gesture.Pan()
    .enabled(true)
    .runOnJS(true)
    .onBegin(() => {
      scale.value = withSpring(1.02);
    })
    .onUpdate((e) => {
      translateY.value = Math.max(-60, Math.min(60, e.translationY));
      dragProgress.value = Math.min(
        1,
        Math.abs(e.translationY) / SWAP_THRESHOLD,
      );
    })
    .onEnd((e) => {
      translateY.value = withSpring(0, { damping: 20, stiffness: 300 });
      scale.value = withSpring(1);
      dragProgress.value = withSpring(0);
      if (Math.abs(e.translationY) >= SWAP_THRESHOLD) {
        onSwap(); // safe to call directly now
      }
    })
    .onFinalize(() => {
      translateY.value = withSpring(0, { damping: 20, stiffness: 300 });
      scale.value = withSpring(1);
      dragProgress.value = withSpring(0);
    });
 
  const rowAnimStyle = useAnimatedStyle(() => ({
    transform: [{ translateY: translateY.value }, { scale: scale.value }],
    zIndex: Math.abs(translateY.value) > 4 ? 10 : 0,
    elevation: Math.abs(translateY.value) > 4 ? 4 : 0,
  }));
 
  // Greyed out when the *sibling* is being dragged toward the threshold
  const dimAnimStyle = useAnimatedStyle(() => ({
    opacity: interpolate(siblingDragProgress.value, [0, 1], [1, 0.35]),
  }));
 
  return (
    <GestureDetector gesture={pan}>
      <Animated.View style={[styles.row, rowAnimStyle]}>
        <Animated.View style={[styles.inner, dimAnimStyle]}>
          <Ionicons
            name="reorder-two-outline"
            size={16}
            color={colors.border}
            style={styles.handle}
          />
          <View style={styles.textGroup}>
            <Text style={[styles.label, { color: colors.textMuted }]}>
              {label}
            </Text>
            <Text
              style={[styles.value, { color: colors.text }]}
              numberOfLines={1}
            >
              {value}
            </Text>
          </View>
          {trailingIcon ? (
            <Ionicons
              name={trailingIcon}
              size={18}
              color={colors.textMuted}
              style={styles.trailingIcon}
            />
          ) : null}
        </Animated.View>
      </Animated.View>
    </GestureDetector>
  );
}
 
const styles = StyleSheet.create({
  row: {
    flexDirection: "row",
    alignItems: "center",
    paddingVertical: 5,
    backgroundColor: "transparent",
  },
  inner: {
    flex: 1,
    flexDirection: "row",
    alignItems: "center",
  },
  handle: {
    marginRight: 6,
  },
  textGroup: {
    flex: 1,
  },
  label: {
    fontSize: 11,
    fontWeight: "500",
    marginBottom: 1,
  },
  value: {
    fontSize: 15,
  },
  trailingIcon: {
    marginLeft: 6,
  },
});