/* eslint-disable react-hooks/exhaustive-deps */ import gsap from "gsap"; import { useState, useEffect } from "react"; import { isMobile } from "react-device-detect"; import { useSwipeable } from "react-swipeable"; import { Transition } from "react-transition-group"; import { useNavigate } from "react-router-dom"; import Button from "../Button"; import LeftArrowSliderIcon from "../icons/LeftArrowSliderIcon"; import RightArrowSliderIcon from "../icons/RightArrowSliderIcon"; import useCompass from "../../store/useCompass"; import SequenceHighlighting from "./SequenceHighlighting"; // import { laptopWidth, mobileWidht } from "../../consts/masterplan"; const arrayLength = 360; const keyframes: number[] = [51, 178, 249, 339]; interface SequenceSliderProps { path: string; } function SequenceSlider({ path }: SequenceSliderProps) { const [selectedImageRightIndex, setSelectedRightImageIndex] = useState(keyframes[3]); const [selectedImageLeftKeyframe, setSelectedImageLeftKeyframe] = useState(arrayLength - keyframes[3]); const [isAnimate, setIsAnimate] = useState(false); const [loadedImages, setLoadedImages] = useState(0); const [keyframeIndex, setKeyframeIndex] = useState(3); const [left, setLeft] = useState(0); const { setCurrentCompassRotate, currentCompassRotate } = useCompass(); const [width, setWidth] = useState(); const [top, setTop] = useState(); const navigate = useNavigate(); useEffect(() => { setCurrentCompassRotate(keyframes[3]); }, []); const handlers = useSwipeable({ trackMouse: true, onSwipedLeft: () => !isAnimate && next(), onSwipedRight: () => !isAnimate && prev(), }); function handleLoad() { setLoadedImages((prev) => prev + 1); } function animateToRight(currentOffset: number) { const obj = { selectedImageIndex: selectedImageRightIndex }; setCurrentCompassRotate(currentCompassRotate + currentOffset); gsap.to(obj, { selectedImageIndex: selectedImageRightIndex + currentOffset, duration: 0.75, ease: "power1.inOut", onUpdate: () => { const roundedIndex = Math.floor(obj.selectedImageIndex); if (roundedIndex >= arrayLength) { setSelectedRightImageIndex(roundedIndex - arrayLength); setSelectedImageLeftKeyframe( arrayLength - (roundedIndex - arrayLength) ); } else { setSelectedRightImageIndex(roundedIndex); setSelectedImageLeftKeyframe(arrayLength - roundedIndex); } }, onComplete: () => { setIsAnimate(false); }, }); } function animateToLeft(currentOffset: number) { const obj = { selectedImageIndex: selectedImageLeftKeyframe }; setCurrentCompassRotate(currentCompassRotate - currentOffset); gsap.to(obj, { selectedImageIndex: selectedImageLeftKeyframe + currentOffset, duration: 0.75, ease: "power1.inOut", onUpdate: () => { const roundedIndex = Math.floor(obj.selectedImageIndex); if (roundedIndex > arrayLength) { setSelectedRightImageIndex( arrayLength - (roundedIndex - arrayLength) ); setSelectedImageLeftKeyframe(roundedIndex - arrayLength); } else { setSelectedImageLeftKeyframe(roundedIndex); setSelectedRightImageIndex(arrayLength - roundedIndex); } }, onComplete: () => { setIsAnimate(false); }, }); } function next() { const updatedKeyframeIndex = keyframeIndex !== keyframes.length - 1 ? keyframeIndex + 1 : 0; const currentOffset = keyframeIndex !== keyframes.length - 1 ? keyframes[updatedKeyframeIndex] - keyframes[keyframeIndex] : 360 - keyframes[keyframeIndex] + keyframes[0]; setKeyframeIndex(updatedKeyframeIndex); setIsAnimate(true); animateToRight(currentOffset); } function prev() { const updatedKeyframeIndex = keyframeIndex !== 0 ? keyframeIndex - 1 : keyframes.length - 1; const currentOffset = keyframeIndex !== 0 ? keyframes[keyframeIndex] - keyframes[updatedKeyframeIndex] : 360 - keyframes[updatedKeyframeIndex] + keyframes[keyframeIndex]; setKeyframeIndex(updatedKeyframeIndex); setIsAnimate(true); animateToLeft(currentOffset); } // function handleResize() { // const screenWidth = window.innerWidth; // const screenHeight = window.innerHeight; // // if (screenWidth > laptopWidth) { // const _top = screenHeight / 7; // setTop(_top); // const _left = 2500 - screenWidth; // // const _left = laptopWidth - screenWidth; // const _width = screenWidth + _left * 2; // setWidth(_width); // setLeft(_left); // // setWidth(screenWidth); // // setTop(screenWidth / 2 - screenHeight / 2); // // setLeft(0); // // } else { // // if (screenWidth > mobileWidht) { // // const _top = screenHeight / 4; // // setTop(_top); // // } else { // // const _top = screenHeight / 2.5; // // setTop(_top); // // } // // const _left = laptopWidth - screenWidth; // // const _width = screenWidth + _left * 2; // // setWidth(_width); // // setLeft(_left); // // } // } function handleResize() { const screenWidth = window.innerWidth; const screenHeight = window.innerHeight; const _top = screenHeight / 4.5; setTop(_top); const _left = screenHeight - screenWidth; const _width = screenHeight + _left; setWidth(_width); setLeft(_left); } function handleOnSequenceClick(): void { navigate("./wing"); } useEffect(() => { handleResize(); window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, []); return (
{Array.from({ length: arrayLength }).map((_, index) => { // const imgSrc = `${path}/${ // !isAnimate && keyframes.includes(index + 1) // ? `${index + 1}-4k.jpg` // : `${index + 1}.jpg` // }`; return ( <> ); })} {keyframes.map((keyframe, index) => { const imgSrc = `${path}/${ keyframe === selectedImageRightIndex ? `${keyframe + 1}-4k.jpg` : `` }`; return ( <> ); })}
); } export default SequenceSlider;