/* eslint-disable react-native/no-inline-styles */ import { FlatList, PanResponder, StyleSheet, View } from "react-native"; import React, { createRef, RefObject, useEffect, useRef, useState, } from "react"; import { useSelector } from "react-redux"; import Button from "../atoms/Button"; import { RootState } from "../../redux/store"; import Circle from "../atoms/Circle"; import Scale, { ScaleAnimationHandle } from "../transitions/Scale"; import Fade, { FadeAnimationHandle } from "../transitions/Fade"; import { Slot } from "../../types/AOP"; import { useAOPSlot } from "../../utils/stateUtilHooks"; /** * * @param {number} slotHeight * @param {number} margin * @returns {number} height * @description * used to calculate the height * of the container containing * the slots * (also accounts for space between the slot items) */ const slotsContainerHeightCalculator = ( slotHeight: number, margin: number ): number => { return 9 * slotHeight + 7 * margin; }; /** * * @param {number} slotHeight * @param {number} margin * @returns {number} offset * @description * used to calculate the start * and end offset to position * active element at the center * of the container */ const offsetToPositionCenter = (slotHeight: number, margin: number) => { return slotHeight * 4 + 5 * margin; }; /** * * @param {number} activeIndex * @param {number} currentIndex * @returns {number} scale * @description * used to calculate scale value of list item based on a * comparison between active index in the list * and current index of given item */ const scaleFactor = (activeIndex: number, currentIndex: number) => { if (activeIndex - currentIndex === 0) { return 1; } else if (Math.abs(activeIndex - currentIndex) < 5) { return 1 - 0.1 * Math.abs(activeIndex - currentIndex); } else { return 0.5; } }; /** * * @param {number} activeIndex * @param {number} currentIndex * @returns {number} opacity * @description * used to calculate opacity value of list item based on a * comparison between active index in the list * and current index of given item */ const opacityFactor = (activeIndex: number, currentIndex: number): number => { if (activeIndex - currentIndex === 0) { return 1; } else if (Math.abs(activeIndex - currentIndex) < 5) { return 1 - 0.2 * Math.abs(activeIndex - currentIndex); } else { return 0.2; } }; /** * @interface * @param {number} slotItemHeight * @param {number} slotItemSpacing */ export interface SafecamSlotList { slotItemHeight?: number; slotItemSpacing?: number; } /** * @component * Represents a slotList component instance * @param {number} slotItemHeight * @param {number} slotItemSpacing * @description * displays the list of available slots * and lets the user change the slot * using a "slide" gesture * @example * * @returns {React.FC} SlotList */ const SlotList: React.FC = ({ slotItemHeight = 30, slotItemSpacing = 4, }) => { const { slotList, activeSlot, activeSlotIndex, incrementSlot, decrementSlot, } = useAOPSlot(); const { orientation } = useSelector((appState: RootState) => appState.Util); // to record and set the list container height // used for calculating initial and final item position offsets const [containerHeight, setContainerHeight] = useState(0); // listView ref used for triggering declarative scroll actions const ref = useRef(null); // contains the actual gesture value ("right" | "left") // with a timestamp applied to allow for re-setting of same // type of gesture const [gesture, setGesture] = useState(""); // contains width value for each button, (since due to different word length) // center position via offset requires unique calculation // for each button const [buttonWidths, setButtonWidths] = useState([]); // swipe sensitivity used, this is used to trigger swipe // only after the finger has moved across a certain threshold of distance const sensitivity = 30; // contains animation handle refs for each animated Component wrapper on list item // to allow for transitions to be triggered declaratively const [scaleRefs, setScaleRefs] = useState[]>( [] ); const [fadeRefs, setFadeRefs] = useState[]>( [] ); const panResponder = useRef( PanResponder.create({ onMoveShouldSetPanResponder: () => true, onMoveShouldSetPanResponderCapture: () => true, onPanResponderRelease: (_, gestureState) => { // used to calculate if the user has swiped up/down if (gestureState.dy > sensitivity) { setGesture(`down%SEPARATE%${Date.now()}`); } else if (gestureState.dy < -sensitivity) { setGesture(`up%SEPARATE%${Date.now()}`); } }, onPanResponderTerminationRequest: () => true, }) ).current; // used to set the ref handle placeholders // for animation on each item to be triggered on component mount useEffect(() => { setScaleRefs([ ...Array(slotList.length) .fill(null) .map((_, __) => createRef()), ]); setFadeRefs([ ...Array(slotList.length) .fill(null) .map((_, __) => createRef()), ]); setButtonWidths([ ...Array(slotList.length) .fill(null) .map((_, __) => 0), ]); }, [slotList]); // used to run a effect, based on the gesture user has selected useEffect(() => { const g = gesture.split("%SEPARATE%")[0]; // edge case to allow up swipe to move to next slot only if its not the last slot in state list // edge case to allow down swipe to move to first slot only if its not the first slot in state list if (g === "up") { incrementSlot(); } else if (g === "down") { decrementSlot(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [gesture]); // effect to display on the UI which slot is active and in center useEffect(() => { if (containerHeight > 0 && slotList.length > 0) { // makes the listView declaratively scroll onto the given index // with a calculated offset if (activeSlotIndex) { ref.current?.scrollToIndex({ index: activeSlotIndex, viewOffset: offsetToPositionCenter(slotItemHeight, slotItemSpacing), }); } else if (activeSlotIndex === 0) { ref.current?.scrollToIndex({ index: activeSlotIndex, viewOffset: offsetToPositionCenter(slotItemHeight, slotItemSpacing), }); } // triggers animation on each slot declaratively based on its index scaleRefs.forEach((scaleRef, i) => { scaleRef.current?.triggerAnimation( scaleFactor(activeSlotIndex as number, i) ); }); fadeRefs.forEach((fadeRef, i) => { fadeRef.current?.triggerAnimation( opacityFactor(activeSlotIndex as number, i) ); }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [activeSlotIndex]); // useLayoutEffect(() => { // }, []); const defineSlotListItemColor = (slot: Slot) => { if (slot.isUploaded) return "rgba(38, 49, 147, 0.6)"; else if (slot.isComplete) return "rgba(38, 147, 49, 0.6)"; else return "white"; }; return ( setContainerHeight(e.nativeEvent.layout.height)} style={{ ...styles.root, height: slotsContainerHeightCalculator(slotItemHeight, slotItemSpacing), }} > {/* {slotList.map((slot, index) => (