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

100% Statements 29/29
100% Branches 13/13
100% Functions 3/3
100% Lines 27/27

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                                                                                                        5x                     12x 12x 12x 12x 12x 12x 12x 12x     12x   12x 12x   12x 12x   12x 12x 12x 12x 12x 12x     12x         12x   12x 12x 11x               12x                                                                                       12x                                                                                                                                                                  
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import {
  Image,
  ImageSourcePropType,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
  ViewStyle,
} from 'react-native';
 
import { PlusCircleIcon } from '@repo/ui/icons/PlusCircle';
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
import { textStyles } from '@repo/ui/themes/typography';
 
import { isAndroid, isIOS } from '@repo/utils/platform';
 
import { useScaling } from '@/hooks/useScaling';
 
import { DEFAULT_NAVIGATION_BOTTOM, SCREEN_HEIGHT, SCREEN_WIDTH } from '@/utils/dimensions';
 
import {
  BALLOON_SIZE_MULTIPLIER,
  BUTTON_SIZE,
  LINE_HEIGHT_DIVISOR,
  LINE_MARGIN_TOP,
} from '@/constants/layout';
 
type BalloonContentType =
  | {
      title: string;
      subtitle: string;
      customNode?: never;
    }
  | {
      title?: never;
      subtitle?: never;
      customNode: React.ReactNode;
    };
 
interface BalloonRequestBaseProps {
  image: ImageSourcePropType;
  onPress?: () => void;
  buttonIcon?: React.ReactNode;
  buttonSize?: number;
  buttonBackgroundColor?: string;
  customTextContainerStyle?: ViewStyle;
}
 
type BalloonRequestProps = BalloonContentType & BalloonRequestBaseProps;
 
export const BalloonRequest = ({
  title,
  subtitle,
  customNode,
  image,
  onPress,
  buttonIcon,
  buttonSize = BUTTON_SIZE,
  buttonBackgroundColor,
  customTextContainerStyle,
}: BalloonRequestProps) => {
  const { theme } = useTheme();
  const styles = useStyles();
  const { scale } = useScaling();
  const insets = useSafeAreaInsets();
  const balloonOverflowTop = (SCREEN_WIDTH * (BALLOON_SIZE_MULTIPLIER - 1)) / 2;
  const minimumTopPadding = balloonOverflowTop + theme.metrics.spacing[1];
  const containerPaddingTop = Math.max(insets.top / 2, minimumTopPadding);
  const containerHeight = isAndroid()
    ? SCREEN_HEIGHT - insets.top / 2 - insets.bottom - DEFAULT_NAVIGATION_BOTTOM
    : SCREEN_HEIGHT - insets.top;
  const highlightBorderRadius = isIOS() ? '40%' : '50%';
 
  const finalButtonBackgroundColor = buttonBackgroundColor ?? theme.colors.badge.button;
  const finalButtonIcon = buttonIcon ?? <PlusCircleIcon color={theme.colors.icon.primary} />;
 
  const titleFontSize = 58 * scale;
  const subtitleFontSize = 26 * scale;
 
  const balloonHeight = SCREEN_WIDTH;
  const connectionLineHeight = SCREEN_WIDTH / LINE_HEIGHT_DIVISOR;
  const connectionLineTop = balloonHeight + LINE_MARGIN_TOP;
  const connectionLineBottom = connectionLineTop + connectionLineHeight;
  const buttonBottomPosition = connectionLineBottom + buttonSize;
  const imageToButtonSpacing = theme.metrics.spacing[1.5];
 
  const maxImageHeight =
    containerHeight -
    containerPaddingTop -
    buttonBottomPosition -
    imageToButtonSpacing -
    insets.bottom;
  const imageStyle = maxImageHeight > 0 ? { maxHeight: maxImageHeight } : undefined;
 
  const renderBalloonContent = () => {
    if (customNode) return customNode;
    return (
      <>
        <Text style={[styles.title, { fontSize: titleFontSize }]}>{title}</Text>
        <Text style={[styles.subtitle, { fontSize: subtitleFontSize }]}>{subtitle}</Text>
      </>
    );
  };
 
  return (
    <View style={[styles.container, { height: containerHeight, paddingTop: containerPaddingTop }]}>
      <View style={styles.balloonContainer}>
        <View style={styles.balloonWrapper}>
          <View style={styles.balloonBackground} />
          <View style={styles.balloonBorder} />
          <View style={[styles.highlight, { borderRadius: highlightBorderRadius }]} />
          <View style={[styles.textContainer, customTextContainerStyle]}>
            {renderBalloonContent()}
          </View>
        </View>
      </View>
 
      <View style={styles.connectionLine} />
 
      <TouchableOpacity
        onPress={onPress}
        activeOpacity={theme.metrics.opacity[80]}
        testID="balloon-action-button"
      >
        <View
          style={[
            styles.actionButton,
            {
              width: buttonSize,
              height: buttonSize,
              borderRadius: buttonSize / 2,
              backgroundColor: finalButtonBackgroundColor,
            },
          ]}
        >
          {finalButtonIcon}
        </View>
      </TouchableOpacity>
 
      <Image
        source={image}
        style={[styles.mascotImage, { bottom: insets.bottom }, imageStyle]}
        resizeMode="contain"
      />
    </View>
  );
};
 
const useStyles = makeStyles(theme => ({
  container: {
    width: SCREEN_WIDTH,
    alignItems: 'center',
    flex: 1,
  },
  balloonContainer: {
    width: SCREEN_WIDTH,
    aspectRatio: 1,
    justifyContent: 'center',
    alignItems: 'center',
    zIndex: 2,
  },
  balloonWrapper: {
    width: SCREEN_WIDTH * BALLOON_SIZE_MULTIPLIER,
    height: SCREEN_WIDTH * BALLOON_SIZE_MULTIPLIER,
    justifyContent: 'center',
    alignItems: 'center',
  },
  balloonBackground: {
    ...StyleSheet.absoluteFillObject,
    borderRadius: (SCREEN_WIDTH * BALLOON_SIZE_MULTIPLIER) / 2,
    backgroundColor: theme.colors.badge.light,
    transform: [{ translateX: -10 }],
  },
  balloonBorder: {
    ...StyleSheet.absoluteFillObject,
    borderRadius: (SCREEN_WIDTH * BALLOON_SIZE_MULTIPLIER) / 2,
    borderWidth: theme.metrics.borderWidth.default,
    borderColor: theme.colors.border.primary,
    backgroundColor: 'transparent',
    transform: [{ translateX: 10 }],
  },
  highlight: {
    position: 'absolute',
    top: '17%',
    left: '19%',
    width: SCREEN_WIDTH * 0.08,
    height: SCREEN_WIDTH * 0.045,
    backgroundColor: theme.colors.background.secondary,
    transform: [{ rotate: '-45deg' }],
    opacity: theme.metrics.opacity[90],
  },
  textContainer: {
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: '40%',
  },
  title: {
    color: theme.colors.text.primary,
    marginBottom: theme.metrics.spacing[1.25],
    ...textStyles.title,
    textAlign: 'center',
    letterSpacing: 2,
    maxWidth: '80%',
  },
  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.border.primary,
    marginTop: LINE_MARGIN_TOP,
    zIndex: 1,
  },
  actionButton: {
    justifyContent: 'center',
    alignItems: 'center',
  },
  mascotImage: {
    position: 'absolute',
    bottom: theme.metrics.spacing[0],
    zIndex: 0,
  },
}));
 
export default BalloonRequest;