import { useEffect, useRef, useState } from "react"; import { ArrowsPointingOutIcon, ArrowsPointingInIcon, ChevronLeftIcon, ChevronRightIcon, PlusIcon, MinusIcon, } from "@heroicons/react/24/outline"; import Image from "next/image"; import { useRouter } from "next/router"; import { useAniList } from "../../../lib/anilist/useAnilist"; import { getHeaders, getRandomId } from "@/utils/imageUtils"; export default function FirstPanel({ aniId, data, hasRun, currentId, seekPage, setSeekPage, visible, setVisible, chapter, nextChapter, prevChapter, paddingX, session, mobileVisible, setMobileVisible, setCurrentPage, number, mangadexId, }) { const { markProgress } = useAniList(session); const [currentImageIndex, setCurrentImageIndex] = useState(0); const imageRefs = useRef([]); const scrollContainerRef = useRef(); const [imageQuality, setImageQuality] = useState(80); const router = useRouter(); // console.log({ chapter }); useEffect(() => { const handleScroll = () => { const scrollTop = scrollContainerRef.current.scrollTop; let index = 0; for (let i = 0; i < imageRefs.current.length; i++) { const img = imageRefs.current[i]; if ( scrollTop >= img?.offsetTop - scrollContainerRef.current.offsetTop && scrollTop < img.offsetTop - scrollContainerRef.current.offsetTop + img.offsetHeight ) { index = i; break; } } if (index === data?.length - 3 && !hasRun.current) { if (session) { if (aniId?.length > 6) return; const currentChapter = chapter.chapters?.find( (x) => x.id === currentId ); if (currentChapter) { const chapterNumber = currentChapter.number ?? chapter.chapters.indexOf(currentChapter) + 1; markProgress(aniId, chapterNumber); console.log("marking progress"); } } hasRun.current = true; } setCurrentPage(index + 1); setCurrentImageIndex(index); setSeekPage(index); }; scrollContainerRef?.current?.addEventListener("scroll", handleScroll, { passive: true, }); return () => { if (scrollContainerRef.current) { scrollContainerRef.current.removeEventListener("scroll", handleScroll, { passive: true, }); } }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [data, session, chapter]); // console.log({ imageQuality }); useEffect(() => { if (scrollContainerRef.current && seekPage !== currentImageIndex) { const targetImageRef = imageRefs.current[seekPage]; if (targetImageRef) { scrollContainerRef.current.scrollTo({ top: targetImageRef.offsetTop - scrollContainerRef.current.offsetTop, behavior: "smooth", }); } } }, [seekPage, currentImageIndex]); useEffect(() => { if (scrollContainerRef.current) { scrollContainerRef.current.scrollTo(0, 0); } }, [currentId]); useEffect(() => { if (typeof window !== "undefined") { const root = window.document.documentElement; root.style.setProperty("--dynamic-padding", `${paddingX}px`); } }, [paddingX]); return (
{data && Array.isArray(data) && data?.length > 0 ? ( data.map((i, index) => (
(imageRefs.current[index] = el)} > {index} setMobileVisible(!mobileVisible)} className="w-screen lg:w-full h-auto bg-[#bbb]" />
)) ) : (
{/* {data.error || "Not found"} :( */}

)}
{/* */} {visible ? ( ) : ( )}
{`Page ${ currentImageIndex + 1 }/${data?.length}`}
); }