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

100% Statements 24/24
100% Branches 2/2
100% Functions 4/4
100% Lines 23/23

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                4x   4x 4x             4x       4x 4x 4x 4x 4x     4x   4x 4x             4x                                 4x                             4x 4x   4x 4x 4x       4x           4x         4x                                                                 4x                                                                        
import React, { useEffect, useRef } from 'react';
import { Animated, Dimensions, Easing, View } from 'react-native';
 
import { makeStyles } from '@repo/ui/themes/makeStyles';
import { useTheme } from '@repo/ui/themes/ThemeContext';
 
import { useFabHidden } from '@/contexts/FabContext';
 
const { width: screenWidth } = Dimensions.get('window');
 
const TRACK_WIDTH = screenWidth * 0.7;
const DEFAULT_SKATE_SIZE = 120;
 
interface LoadingSliderProps {
  width?: number;
  height?: number;
}
 
export const LoadingSlider = ({
  width = DEFAULT_SKATE_SIZE,
  height = DEFAULT_SKATE_SIZE,
}: LoadingSliderProps) => {
  const styles = useStyles();
  const { theme } = useTheme();
  const translateX = useRef(new Animated.Value(-width)).current;
  const bounce = useRef(new Animated.Value(0)).current;
  const tilt = useRef(new Animated.Value(0)).current;
 
  // Hide the chat FAB while the loading slider is visible
  useFabHidden(true);
 
  useEffect(() => {
    const move = Animated.timing(translateX, {
      toValue: TRACK_WIDTH - width / 2,
      duration: 3500,
      easing: Easing.linear,
      useNativeDriver: true,
    });
 
    const bounceAnim = Animated.loop(
      Animated.sequence([
        Animated.timing(bounce, {
          toValue: -4,
          duration: 270,
          easing: Easing.inOut(Easing.ease),
          useNativeDriver: true,
        }),
        Animated.timing(bounce, {
          toValue: 0,
          duration: 270,
          easing: Easing.inOut(Easing.ease),
          useNativeDriver: true,
        }),
      ]),
    );
 
    const tiltAnim = Animated.loop(
      Animated.sequence([
        Animated.timing(tilt, {
          toValue: 1,
          duration: 300,
          useNativeDriver: true,
        }),
        Animated.timing(tilt, {
          toValue: -1,
          duration: 300,
          useNativeDriver: true,
        }),
      ]),
    );
 
    bounceAnim.start();
    tiltAnim.start();
 
    move.start(() => {
      bounceAnim.stop();
      tiltAnim.stop();
    });
  }, [bounce, tilt, translateX, width]);
 
  const progressScale = translateX.interpolate({
    inputRange: [-width, TRACK_WIDTH - width / 2],
    outputRange: [0, 1],
    extrapolate: 'clamp',
  });
 
  const rotateDeg = tilt.interpolate({
    inputRange: [-1, 1],
    outputRange: ['-2deg', '2deg'],
  });
 
  return (
    <View style={styles.container} accessibilityRole="progressbar" accessibilityLabel="Loading">
      <View style={styles.wrapper}>
        <View style={styles.track}>
          <Animated.View
            style={[
              styles.progress,
              {
                transform: [{ scaleX: progressScale }],
              },
            ]}
          />
        </View>
 
        <Animated.Image
          testID="loading-mascot"
          source={theme.assets.loadingMascot}
          style={[
            styles.mascot,
            {
              width,
              height,
              transform: [{ translateX }, { translateY: bounce }, { rotate: rotateDeg }],
            },
          ]}
          resizeMode="contain"
          accessible={false}
        />
      </View>
    </View>
  );
};
 
const useStyles = makeStyles(theme => ({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
 
  wrapper: {
    width: TRACK_WIDTH,
    height: 140,
  },
 
  track: {
    position: 'absolute',
    bottom: 22,
    width: '100%',
    height: 8,
    backgroundColor: theme.colors.background.sliderTrack,
    borderRadius: 4,
    overflow: 'hidden',
  },
 
  progress: {
    height: '100%',
    width: '100%',
    backgroundColor: theme.colors.background.sliderProgress,
    transform: [{ scaleX: 0 }],
    transformOrigin: 'left',
  },
 
  mascot: {
    position: 'absolute',
    bottom: 30,
    left: 0,
  },
}));