All files / apps/host/src/components/RequestResolvedCard index.tsx

91.78% Statements 67/73
58.33% Branches 7/12
94.11% Functions 16/17
92.64% Lines 63/68

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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342                                                                      3x                 3x   3x 240x     240x 240x 240x   240x 240x 240x 240x   240x 240x 240x   240x 240x 240x                     240x 240x                             240x               3x 8x 8x 8x 8x       8x 8x         8x       8x 8x 8x 8x     8x     8x 8x 8x 8x 8x 8x   8x 8x                   8x 8x           8x   8x 8x     8x 8x         8x         8x   8x         8x             8x           8x                 8x   8x 8x 8x     8x       240x                                                                                         3x 248x   248x                                                                                                                                                                        
import { memo, useEffect, useMemo, useState } from 'react';
import Animated, {
  Easing,
  interpolateColor,
  useAnimatedStyle,
  useSharedValue,
  withDelay,
  withRepeat,
  withSequence,
  withSpring,
  withTiming,
} from 'react-native-reanimated';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
 
import { TickCircleIcon } from '@repo/ui/icons/TickCircle';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
import { textStyles } from '@repo/ui/themes/typography';
 
import { useBalloonContainerMetrics } from '@/hooks/useBalloonContainerMetrics';
import { useScaling } from '@/hooks/useScaling';
 
import { SCREEN_HEIGHT, SCREEN_WIDTH } from '@/utils/dimensions';
 
import {
  BUTTON_SIZE,
  LINE_HEIGHT_DIVISOR,
  LINE_MARGIN_TOP,
  MIN_IMAGE_BUTTON_SPACING,
  SUCCESS_MASCOT_ASPECT_RATIO,
  SUCCESS_MASCOT_WIDTH_RATIO,
} from '@/constants/layout';
 
import { createBalloonShellStyles } from '../balloonShellStyles';
 
const CONFETTI_COLORS = [
  '#3498db', // Blue
  '#f1c40f', // Yellow
  '#e67e22', // Orange
  '#2ecc71', // Green
  '#9b59b6', // Purple
  '#e74c3c', // Red
];
 
const CONFETTI_COUNT = 30;
 
const ConfettiPiece = memo(({ index, trigger }: { index: number; trigger: boolean }) => {
  const styles = useStyles();
 
  // Randomize trajectories
  const targetX = useMemo(() => (Math.random() - 0.5) * SCREEN_WIDTH * 1.5, []);
  const targetY = useMemo(() => (Math.random() - 1.2) * SCREEN_HEIGHT * 0.6, []);
  const targetRotate = useMemo(() => (Math.random() - 0.5) * 360, []);
 
  const x = useSharedValue(0);
  const y = useSharedValue(0);
  const rotation = useSharedValue(0);
  const opacity = useSharedValue(0);
 
  const colorProgress = useSharedValue(0);
  const baseColor = CONFETTI_COLORS[index % CONFETTI_COLORS.length];
  const nextColor = CONFETTI_COLORS[(index + 1) % CONFETTI_COLORS.length];
 
  const animatedStyle = useAnimatedStyle(() => {
    const backgroundColor = interpolateColor(colorProgress.value, [0, 1], [baseColor, nextColor]);
    return {
      transform: [
        { translateX: x.value },
        { translateY: y.value },
        { rotate: `${rotation.value}deg` },
      ],
      opacity: opacity.value,
      backgroundColor,
    };
  }, [colorProgress, baseColor, nextColor, x, y, rotation, opacity]);
 
  useEffect(() => {
    Iif (trigger) {
      opacity.value = 1;
      x.value = withSpring(targetX, { damping: 20, stiffness: 40 });
      y.value = withSpring(targetY, { damping: 20, stiffness: 40 });
      rotation.value = withSpring(targetRotate, { damping: 20, stiffness: 40 });
 
      // Loop the color shift progress indefinitely
      colorProgress.value = withRepeat(
        withTiming(1, { duration: 1500 }),
        -1, // infinite repetitions
        true, // reverse direction on each repeat
      );
    }
  }, [trigger, targetX, targetY, targetRotate, x, y, rotation, opacity, colorProgress]);
 
  return <Animated.View style={[styles.confettiPiece, animatedStyle]} />;
});
 
interface BalloonRequestProps {
  text?: string;
  onPress?: () => void;
}
 
export const RequestResolvedCard = ({ text, onPress }: BalloonRequestProps) => {
  const { theme } = useTheme();
  const styles = useStyles();
  const { scale } = useScaling();
  const { containerHeight, highlightBorderRadius } = useBalloonContainerMetrics();
 
  // Size the mascot per the Figma "Done" design: its width tracks the screen
  // width at the design ratio, keeping the design aspect ratio.
  const designMascotWidth = SCREEN_WIDTH * SUCCESS_MASCOT_WIDTH_RATIO;
  const designMascotHeight = designMascotWidth * SUCCESS_MASCOT_ASPECT_RATIO;
  // Bottom edge of the tick button, measured from the top of the container:
  // full-width square balloon container + connection line (with its negative
  // top margin) + the button itself.
  const buttonBottom =
    SCREEN_WIDTH + LINE_MARGIN_TOP + SCREEN_WIDTH / LINE_HEIGHT_DIVISOR + BUTTON_SIZE;
  // On screens too short for the design size, clamp the mascot to the space
  // left below the button so it never overlaps the tick button.
  const availableMascotHeight =
    containerHeight - buttonBottom - theme.metrics.spacing[8] - MIN_IMAGE_BUTTON_SPACING;
  const mascotHeight = Math.max(0, Math.min(designMascotHeight, availableMascotHeight));
  const mascotWidth = mascotHeight / SUCCESS_MASCOT_ASPECT_RATIO;
  const mascotSizeStyle = { width: mascotWidth, height: mascotHeight };
 
  const finalButtonIcon = (
    <TickCircleIcon color={theme.colors.icon.primary} width={50} height={50} />
  );
 
  const titleFontSize = 48 * scale;
  const titleLineHeight = 55 * scale;
  const balloonTranslateY = useSharedValue(SCREEN_HEIGHT);
  const balloonTranslateX = useSharedValue(0);
  const balloonRotation = useSharedValue(0);
  const mascotTranslateX = useSharedValue(-SCREEN_WIDTH);
 
  const balloonAnimatedStyle = useAnimatedStyle(
    () => ({
      transform: [
        { translateY: balloonTranslateY.value },
        { translateX: balloonTranslateX.value },
        { rotate: `${balloonRotation.value}deg` },
      ],
    }),
    [balloonTranslateY, balloonTranslateX, balloonRotation],
  );
 
  const mascotAnimatedStyle = useAnimatedStyle(
    () => ({
      transform: [{ translateX: mascotTranslateX.value }, { rotate: '-20deg' }],
    }),
    [mascotTranslateX],
  );
 
  const bgOpacity = useSharedValue(0);
 
  useEffect(() => {
    bgOpacity.value = withTiming(1, { duration: 1000 });
  }, [bgOpacity]);
 
  const animatedContainerStyle = useAnimatedStyle(() => {
    const backgroundColor = interpolateColor(
      bgOpacity.value,
      [0, 1],
      ['rgba(0, 0, 0, 0.15)', 'transparent'],
    );
    return {
      backgroundColor,
    };
  }, [bgOpacity]);
 
  useEffect(() => {
    // Vertical ascent
    balloonTranslateY.value = withTiming(0, {
      duration: 2000,
    });
 
    // Horizontal wobble during ascent only
    balloonTranslateX.value = withSequence(
      withTiming(-15, { duration: 500 }),
      withTiming(15, { duration: 1000 }),
      withTiming(0, { duration: 500 }),
    );
 
    // Rotational sway during ascent only
    balloonRotation.value = withSequence(
      withTiming(-4, { duration: 500 }),
      withTiming(4, { duration: 1000 }),
      withTiming(0, { duration: 500 }),
    );
 
    mascotTranslateX.value = withDelay(
      800,
      withTiming(0, {
        duration: 1500,
        easing: Easing.out(Easing.cubic),
      }),
    );
  }, [balloonTranslateY, balloonTranslateX, balloonRotation, mascotTranslateX]);
 
  const [triggerConfetti, setTriggerConfetti] = useState(false);
 
  useEffect(() => {
    const timer = setTimeout(() => setTriggerConfetti(true), 1500);
    return () => clearTimeout(timer);
  }, []);
 
  return (
    <Animated.View style={[styles.container, { height: containerHeight }, animatedContainerStyle]}>
      <View style={styles.confettiContainer} pointerEvents="none">
        {Array.from({ length: CONFETTI_COUNT }).map((_, i) => (
          <ConfettiPiece key={i} index={i} trigger={triggerConfetti} />
        ))}
      </View>
      <Animated.View style={[styles.balloonAnimationWrapper, balloonAnimatedStyle]}>
        <View style={styles.balloonContainer}>
          <View style={styles.balloonWrapper}>
            <View style={styles.balloonBackground} testID="balloon-background" />
            <View style={styles.balloonBorder} />
            <View style={[styles.highlight, { borderRadius: highlightBorderRadius }]} />
            <View style={[styles.textContainer]}>
              <Text
                style={[styles.title, { fontSize: titleFontSize, lineHeight: titleLineHeight }]}
              >
                {text?.toUpperCase()}
              </Text>
            </View>
          </View>
        </View>
 
        <View style={styles.connectionLine} />
 
        <TouchableOpacity
          onPress={onPress}
          activeOpacity={theme.metrics.opacity[80]}
          testID="balloon-action-button"
          accessibilityRole="button"
          accessibilityLabel={text || 'Confirm'}
        >
          <View style={styles.actionButton} testID="action-button">
            {finalButtonIcon}
          </View>
        </TouchableOpacity>
      </Animated.View>
 
      <Animated.Image
        source={theme.assets.successMascot}
        style={[styles.mascotImage, mascotSizeStyle, mascotAnimatedStyle]}
        resizeMode="contain"
        testID="mascot-image"
        accessible={false}
      />
    </Animated.View>
  );
};
 
const useStyles = makeStyles(theme => {
  const shellStyles = createBalloonShellStyles(theme.metrics.borderWidth.default);
 
  return {
    ...shellStyles,
    container: {
      width: SCREEN_WIDTH,
      flex: 1,
      alignItems: 'center',
    },
    balloonAnimationWrapper: {
      alignItems: 'center',
      width: SCREEN_WIDTH,
    },
    confettiContainer: {
      ...StyleSheet.absoluteFillObject,
      justifyContent: 'center',
      alignItems: 'center',
      zIndex: 1,
    },
    confettiPiece: {
      position: 'absolute',
      width: 12,
      height: 6,
      borderRadius: 2,
    },
    balloonBackground: {
      ...shellStyles.balloonBackground,
      backgroundColor: theme.isNewTheme ? theme.colors.border.primary : theme.colors.badge.light,
    },
    balloonBorder: {
      ...shellStyles.balloonBorder,
      borderColor: theme.colors.text.onBehalf,
    },
    highlight: {
      ...shellStyles.highlight,
      backgroundColor: theme.colors.background.secondary,
      opacity: theme.metrics.opacity[90],
    },
    textContainer: {
      alignItems: 'center',
      justifyContent: 'center',
      width: SCREEN_WIDTH * 0.7,
    },
    title: {
      color: theme.isNewTheme ? theme.colors.text.white : theme.colors.text.primary,
      ...textStyles.title,
      fontWeight: theme.metrics.fontWeight.normal,
      textAlign: 'center',
      letterSpacing: 0,
      maxWidth: '81%',
      ...(theme.isNewTheme && {
        textShadowColor: theme.colors.text.white,
        textShadowOffset: { width: 1, height: 1 },
      }),
    },
    subtitle: {
      color: theme.colors.text.primary,
      textAlign: 'center',
      ...textStyles.title,
      maxWidth: '75%',
    },
    connectionLine: {
      width: theme.metrics.spacing[0.25],
      height: SCREEN_WIDTH / LINE_HEIGHT_DIVISOR,
      backgroundColor: theme.colors.text.onBehalf,
      marginTop: LINE_MARGIN_TOP,
      zIndex: 1,
    },
    actionButton: {
      justifyContent: 'center',
      alignItems: 'center',
      width: 60,
      height: 60,
      borderRadius: 40,
      backgroundColor: theme.isNewTheme ? theme.colors.badge.brand : theme.colors.badge.button,
    },
    mascotImage: {
      position: 'absolute',
      bottom: theme.metrics.spacing[8],
      zIndex: 0,
      transform: [{ rotate: '-20deg' }],
    },
  };
});
 
export default RequestResolvedCard;