/* eslint-disable react-hooks/exhaustive-deps */ /* eslint-disable react-native/no-inline-styles */ import { Dimensions, FlatList, GestureResponderEvent, KeyboardAvoidingView, PanResponder, Platform, StyleSheet, Text, TextInput, View, } from "react-native"; import React, { createRef, RefObject, useEffect, useRef, useState, } from "react"; import Button from "../atoms/Button"; import Fade, { FadeAnimationHandle } from "../transitions/Fade"; import { useAOPSlot, useAOPStep } from "../../utils/stateUtilHooks"; import Icon from "react-native-vector-icons/MaterialCommunityIcons"; import { useSelector } from "react-redux"; import { RootState, useAppDispatch } from "../../redux/store"; import { captureAPI } from "../../apis/backendAPIConfig"; import { io } from "socket.io-client"; import { GPTResponse } from "../../types/AOP"; import { setGPTChatData } from "../../redux/AOPSlice"; type Props = { stepListExpanded: boolean; onExpansionPress?: (event: GestureResponderEvent) => void; forceClose: () => void; }; /** * @param {number} buttonWidth * @param {number} containerWidth * @returns {number} offsetValue * @description * used to calculate the offset in positions of the initial and final list items */ const offsetToPositionCenter = ( buttonWidth: number, containerWidth: number ): number => { return containerWidth / 2 - buttonWidth / 2; }; /** * * @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) < 4) { return 1 - 0.3 * Math.abs(activeIndex - currentIndex); } else { return 0.2; } }; /** * @component * Represents a stepList component instance * displays the list of available steps * and lets the user change the step * using a "slide" gesture or a "press" * of the step button * @description * works on redux based state value to * display the "list of steps", "active step" and "completed steps" * and provides touch based UI feedback * while handling change to the active step index * @returns {JSX.Element} */ const StepList = (props: Props): JSX.Element => { // used to pull the step state from redux store const { steps, activeStepIndex, incrementStep, decrementStep, activeStep } = useAOPStep(); const dispatch = useAppDispatch(); const { activeSlotIndex, activeSlot } = useAOPSlot(); // used to store container width // used for calculating initial and final position offsets in step items const [containerWidth, setContainerWidth] = useState(0); // contains a ref of the actual stepList ListView to trigger ref functions on it const ref = useRef(null); // contains animation handle refs for each animated Component wrapper on list item // to allow for transitions to be triggered declaratively const [fadeRefs, setFadeRefs] = 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([]); // 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(""); const productId = useSelector((state: RootState) => state.App.productID); const gptData = useSelector((state: RootState) => state.AOP.gpt); const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); // swipe sensitivity used, this is used to trigger swipe // only after the finger has moved across a certain threshold of distance const sensitivity = 15; const panResponder = useRef( PanResponder.create({ onMoveShouldSetPanResponder: () => true, onMoveShouldSetPanResponderCapture: () => true, onPanResponderRelease: (_, gestureState) => { // used to calculate if the user has swiped left/right if (gestureState.dx > sensitivity) { setGesture(`right%SEPARATE%${Date.now()}`); } else if (gestureState.dx < -sensitivity) { setGesture(`left%SEPARATE%${Date.now()}`); } }, onPanResponderTerminationRequest: () => true, }) ).current; const widgetChatHandler = async (content: string) => { try { const res = await captureAPI.post(`/gpt/${productId}`, { stepId: activeStep.stepId, slotId: activeSlot.slotId, stepIndex: activeStepIndex, slotIndex: activeSlotIndex, content, }); console.log(res.status); } catch (error) { console.log(error); } }; useEffect(() => { setMessages(gptData[activeStepIndex][activeSlotIndex]); }, [gptData, activeStepIndex, activeSlotIndex]); // used to run a effect, based on the gesture user has selected useEffect(() => { const g = gesture.split("%SEPARATE%")[0]; // edge case to allow left swipe to move to next step only if its not the last step in state list // edge case to allow right swipe to move to first step only if its not the first step in state list if (g === "left") { incrementStep(); } else if (g === "right") { decrementStep(); } }, [gesture]); // used to set the ref handle placeholders and buttonWidth placeholders // for animation on each item to be triggered on component mount useEffect(() => { setFadeRefs([ ...Array(steps.length) .fill(null) .map((_, __) => createRef()), ]); setButtonWidths([ ...Array(steps.length) .fill(null) .map((_, __) => 0), ]); }, [steps.length]); // effect to display on the UI which step is active and in center useEffect(() => { if ( containerWidth > 0 && steps.length > 0 && buttonWidths.length === steps.length ) { // makes the listView declaratively scroll onto the given index // with a calculated offset setTimeout(() => { ref.current?.scrollToIndex({ index: activeStepIndex, viewOffset: offsetToPositionCenter( buttonWidths[activeStepIndex], containerWidth ), }); }); // triggers animation on each step declaratively based on its index fadeRefs.forEach((fadeRef, i) => { fadeRef.current?.triggerAnimation(opacityFactor(activeStepIndex, i)); }); } }, [activeStepIndex, buttonWidths, containerWidth]); useEffect(() => { const client = io("https://console.s20.ai", { path: "/capture-sdk-live", }); client.on("connect", () => console.log("connected to chat server")); client.on("gpt", (data) => { dispatch(setGPTChatData(data.gpt)); setMessages(data.gpt[activeStepIndex][activeSlotIndex]); }); }, []); return ( setContainerWidth(e.nativeEvent.layout.width)} > {