import React, { useCallback, useEffect, useState } from "react"; import type { Api } from "@jellyfin/sdk"; import { type BaseItemDto, BaseItemKind, } from "@jellyfin/sdk/lib/generated-client"; import { useCarouselStore } from "../../utils/store/carousel"; import "./carousel.scss"; import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; import { resolveItemImage } from "@/utils/methods/itemImages"; import { useApiInContext } from "@/utils/store/api"; import CarouselSlide from "../carouselSlide"; const AUTOPLAY_MS = 9000; // Memoize CarouselSlide to prevent unnecessary re-renders const MemoizedCarouselSlide = React.memo(CarouselSlide); /** Wide artwork for the trending rail, falling back through the item's images. */ const trendingImageUrl = ( api: Api, item: BaseItemDto, ): string | undefined => { const image = resolveItemImage(item, "Thumb"); if (!image) return undefined; return getImageUrlsApi(api).getItemImageUrlById( image.itemId, image.imageType, { quality: 85, fillWidth: 480, tag: image.tag, }, ); }; const trendingLabel = (item: BaseItemDto): string => { const genre = item.Genres?.[0]; if (genre) return genre.charAt(0).toUpperCase() + genre.slice(1); return item.Type === BaseItemKind.Series ? "Series" : "Movie"; }; const Carousel = ({ content, onChange, }: { content: BaseItemDto[]; onChange: (currentSlide: number) => void; }) => { const [currentSlide, setCurrentSlide] = useState(0); const [isPaused, setIsPaused] = useState(false); const [setDirection] = useCarouselStore((state) => [state.setDirection]); const api = useApiInContext((s) => s.api); const total = content.length; useEffect(() => { if (currentSlide >= total && total > 0) { setCurrentSlide(total - 1); } }, [currentSlide, total]); useEffect(() => { onChange(currentSlide); }, [currentSlide, onChange]); const goTo = useCallback( (index: number) => { if (index === currentSlide) return; setDirection(index > currentSlide ? "right" : "left"); setCurrentSlide(index); }, [currentSlide, setDirection], ); const next = useCallback(() => { if (total < 2) return; setDirection("right"); setCurrentSlide((prev) => (prev + 1) % total); }, [total, setDirection]); const previous = useCallback(() => { if (total < 2) return; setDirection("left"); setCurrentSlide((prev) => (prev - 1 + total) % total); }, [total, setDirection]); // Autoplay; pauses while the pointer is over the hero. useEffect(() => { if (isPaused || total < 2) return; const timer = window.setInterval(() => { setDirection("right"); setCurrentSlide((prev) => (prev + 1) % total); }, AUTOPLAY_MS); return () => window.clearInterval(timer); }, [isPaused, total, setDirection]); if (!api || total === 0) return null; const currentItem = content[currentSlide]; return (
setIsPaused(true)} onMouseLeave={() => setIsPaused(false)} > {total > 1 && ( <> )}

Trending now

{total > 1 && (
{content.map((item, index) => (
)}
{content.map((item, index) => { const imageUrl = trendingImageUrl(api, item); return ( ); })}
); }; export default React.memo(Carousel);