Files
Blink/src/components/carousel/index.tsx
T
1xDed ae5a2133fa
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
chore: window resize handles, UI cleanup and type fixes
Adds edge resize handles for frameless Windows/Linux windows, reserves space for the custom titlebar, and fixes assorted unused imports, error-boundary typing and image decoding hints.
2026-11-28 01:37:33 +03:00

175 lines
4.6 KiB
TypeScript

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]);
// 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 (
<section
className={`cinema-hero ${isPaused ? "paused" : ""}`}
aria-roledescription="carousel"
aria-label="Featured"
onMouseEnter={() => setIsPaused(true)}
onMouseLeave={() => setIsPaused(false)}
>
<MemoizedCarouselSlide key={currentItem.Id} item={currentItem} />
{total > 1 && (
<button
type="button"
className="cinema-arrow right"
onClick={next}
aria-label="Next"
>
<span className="material-symbols-rounded">chevron_right</span>
</button>
)}
<div className="cinema-trending">
<div className="cinema-trending-head">
<h3>Trending now</h3>
{total > 1 && (
<div className="cinema-dots">
{content.map((item, index) => (
<button
key={item.Id}
type="button"
className={index === currentSlide ? "active" : ""}
onClick={() => goTo(index)}
aria-label={item.Name ?? `Slide ${index + 1}`}
aria-current={index === currentSlide}
/>
))}
</div>
)}
</div>
<div className="cinema-trending-rail">
{content.map((item, index) => {
const imageUrl = trendingImageUrl(api, item);
return (
<button
key={item.Id}
type="button"
className={`cinema-trending-card ${
index === currentSlide ? "active" : ""
}`}
onClick={() => goTo(index)}
aria-label={item.Name ?? "Featured item"}
>
{imageUrl ? (
<img
src={imageUrl}
alt=""
loading="lazy"
decoding="async"
/>
) : (
<div className="cinema-trending-placeholder">
<span className="material-symbols-rounded">
{ item.Type === BaseItemKind.Series ? "tv" : "movie" }
</span>
</div>
)}
<span className="cinema-trending-chip">
{trendingLabel(item)}
</span>
</button>
);
})}
</div>
</div>
</section>
);
};
export default React.memo(Carousel);