import Link from "next/link"; import React, { useState, useRef, useEffect, Fragment } from "react"; import { useDraggable } from "react-use-draggable-scroll"; import Image from "next/image"; import { MdChevronRight } from "react-icons/md"; import { ChevronRightIcon, ArrowRightCircleIcon, } from "@heroicons/react/24/outline"; import { ChevronLeftIcon } from "@heroicons/react/20/solid"; import { ExclamationCircleIcon, PlayIcon } from "@heroicons/react/24/solid"; import { useRouter } from "next/router"; import HistoryOptions from "./content/historyOptions"; import { toast } from "sonner"; import { truncateImgUrl } from "@/utils/imageUtils"; export default function Content({ ids, section, data, userData, og, userName, setRemoved, type = "anime", }) { const router = useRouter(); const ref = useRef(); const { events } = useDraggable(ref); const [clicked, setClicked] = useState(false); useEffect(() => { const click = localStorage.getItem("clicked"); if (click) { setClicked(JSON.parse(click)); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const [scrollLeft, setScrollLeft] = useState(false); const [scrollRight, setScrollRight] = useState(true); const slideLeft = () => { ref.current.classList.add("scroll-smooth"); var slider = document.getElementById(ids); slider.scrollLeft = slider.scrollLeft - 500; ref.current.classList.remove("scroll-smooth"); }; const slideRight = () => { ref.current.classList.add("scroll-smooth"); var slider = document.getElementById(ids); slider.scrollLeft = slider.scrollLeft + 500; ref.current.classList.remove("scroll-smooth"); }; const handleScroll = (e) => { const scrollLeft = e.target.scrollLeft > 31; const scrollRight = e.target.scrollLeft < e.target.scrollWidth - e.target.clientWidth; setScrollLeft(scrollLeft); setScrollRight(scrollRight); }; function handleAlert(e) { if (localStorage.getItem("clicked")) { const existingDataString = localStorage.getItem("clicked"); const existingData = JSON.parse(existingDataString); existingData[e] = true; const updatedDataString = JSON.stringify(existingData); localStorage.setItem("clicked", updatedDataString); } else { const newData = { [e]: true, }; const newDataString = JSON.stringify(newData); localStorage.setItem("clicked", newDataString); } } const array = data; let filteredData = array?.filter((item) => item !== null); const slicedData = filteredData?.length > 15 ? filteredData?.slice(0, 15) : filteredData; const goToPage = () => { if (section === "Recently Watched") { router.push(`/en/anime/recently-watched`); } if (section === "New Episodes") { router.push(`/en/anime/recent`); } if (section === "Trending Now") { router.push(`/en/anime/trending`); } if (section === "Popular Anime") { router.push(`/en/anime/popular`); } if (section === "Your Plan") { router.push(`/en/profile/${userName}/#planning`); } if (section === "On-Going Anime" || section === "Your Watch List") { router.push(`/en/profile/${userName}/#current`); } }; const removeItem = async (id, aniId) => { if (userName) { // remove from database const res = await fetch(`/api/user/update/episode`, { method: "DELETE", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name: userName, id, aniId, }), }); const data = await res.json(); if (id) { // remove from local storage const artplayerSettings = JSON.parse(localStorage.getItem("artplayer_settings")) || {}; if (artplayerSettings[id]) { delete artplayerSettings[id]; localStorage.setItem( "artplayer_settings", JSON.stringify(artplayerSettings) ); } } if (aniId) { const currentData = JSON.parse(localStorage.getItem("artplayer_settings")) || {}; const updatedData = {}; for (const key in currentData) { const item = currentData[key]; if (item.aniId !== aniId) { updatedData[key] = item; } } localStorage.setItem("artplayer_settings", JSON.stringify(updatedData)); } // update client setRemoved(id || aniId); if (data?.message === "Episode deleted") { toast.success("Episode removed from history"); } } else { if (id) { // remove from local storage const artplayerSettings = JSON.parse(localStorage.getItem("artplayer_settings")) || {}; if (artplayerSettings[id]) { delete artplayerSettings[id]; localStorage.setItem( "artplayer_settings", JSON.stringify(artplayerSettings) ); } setRemoved(id); } if (aniId) { const currentData = JSON.parse(localStorage.getItem("artplayer_settings")) || {}; // Create a new object to store the updated data const updatedData = {}; // Iterate through the current data and copy items with different aniId to the updated object for (const key in currentData) { const item = currentData[key]; if (item.aniId !== aniId) { updatedData[key] = item; } } // Update localStorage with the filtered data localStorage.setItem("artplayer_settings", JSON.stringify(updatedData)); setRemoved(aniId); } } }; return (

{section}

{ids !== "recentlyWatched" ? slicedData?.map((anime) => { const progress = og?.find((i) => i.mediaId === anime.id); return (
{ids === "onGoing" && (

{anime.title.romaji || anime.title.english}

{checkProgress(progress) && !clicked?.hasOwnProperty(anime.id) && ( )} {checkProgress(progress) && (
handleAlert(anime.id)} className="group-hover:visible invisible absolute top-0 bg-black bg-opacity-20 w-full h-full z-20 text-center" >

{checkProgress(progress)}

)} {anime.nextAiringEpisode && (

Episode {anime.nextAiringEpisode.episode} in

{convertSecondsToTime( anime?.nextAiringEpisode?.timeUntilAiring )}

)}
)}
{ids === "recentAdded" && (
)} {
{ids === "recentAdded" && ( episode-badge

Episode{" "} {anime?.currentEpisode || anime?.episodeNumber}

)} {ids !== "onGoing" && (

{anime.status === "RELEASING" || ids === "recentAdded" ? ( ) : anime.status === "NOT_YET_RELEASED" ? ( ) : null} {anime.title.romaji}

)}
); }) : userData ?.filter((i) => i.title && i.title !== null) ?.slice(0, 10) .map((i) => { const time = i.timeWatched; const duration = i.duration; let prog = (time / duration) * 100; if (prog > 90) prog = 100; return (
{i?.nextId && ( )}

{i?.title === i.aniTitle ? `Episode ${i.episode}` : i?.title || i.anititle}

{i?.image && ( Episode Thumbnail )} {/*

{i.title}

*/}

{i.aniTitle} {" "} | Episode {i.episode}

); })} {userData?.filter((i) => i.aniId !== null)?.length >= 10 && section !== "Recommendations" && (

More on {section}

)} {filteredData?.length >= 10 && section !== "Recommendations" && (

More on {section}

)}
); } function convertSecondsToTime(sec) { let days = Math.floor(sec / (3600 * 24)); let hours = Math.floor((sec % (3600 * 24)) / 3600); let minutes = Math.floor((sec % 3600) / 60); let time = ""; if (days > 0) { time += `${days}d `; time += `${hours}h`; } else { time += `${hours}h `; time += `${minutes}m`; } return time.trim(); } function checkProgress(entry) { const { progress, media } = entry; const { episodes, nextAiringEpisode } = media; if (nextAiringEpisode !== null) { const { episode } = nextAiringEpisode; if (episode - progress > 1) { const missedEpisodes = episode - progress - 1; return `${missedEpisodes} episode${missedEpisodes > 1 ? "s" : ""} behind`; } } return; }