diff --git a/src/components/card/card.tsx b/src/components/card/card.tsx index c5c62fd..294f556 100644 --- a/src/components/card/card.tsx +++ b/src/components/card/card.tsx @@ -10,6 +10,7 @@ import React, { memo, type Ref, useCallback, useState } from "react"; import { ErrorBoundary } from "react-error-boundary"; import { useInView } from "react-intersection-observer"; import getImageUrlsApi from "@/utils/methods/getImageUrlsApi"; +import { resolveItemImage } from "@/utils/methods/itemImages"; import { useApiInContext } from "@/utils/store/api"; import LikeButton from "../buttons/likeButton"; import MarkPlayedButton from "../buttons/markPlayedButton"; @@ -56,6 +57,10 @@ const CardContent = ({ const api = useApiInContext((s) => s.api); const navigate = useNavigate(); + const image = resolveItemImage(item, imageType, { + seriesId: seriesId ? (item.SeriesId ?? undefined) : undefined, + }); + const defaultOnClick = useCallback(() => { if (item?.Id) { navigate(getItemLinkOptions(item.Id, item?.Type)); @@ -102,20 +107,20 @@ const CardContent = ({ ? getTypeIcon(overrideIcon) : getTypeIcon(item.Type ?? "universal")} - {inView && ( + {inView && image && ( {item.Name { + const image = resolveItemImage(item, "Thumb"); + if (!image) return undefined; + return getImageUrlsApi(api).getItemImageUrlById( + image.itemId, + image.imageType, + { + quality: 90, + fillHeight: 360, + tag: image.tag, + }, + ); +}; + const Carousel = ({ content, onChange, @@ -96,12 +116,15 @@ const Carousel = ({ onMouseEnter={() => setIsPaused(true)} onMouseLeave={() => setIsPaused(false)} > - - - + + +
{content.map((item, index) => (
{ const [animationDirection] = useCarouselStore((state) => [state.direction]); + // Hero background: a real backdrop when the item has one, otherwise fall back + // to the poster instead of a big type icon. + const heroBackgroundSrc = item.BackdropImageTags?.length + ? item.ParentBackdropItemId + ? `${api?.basePath}/Items/${item.ParentBackdropItemId}/Images/Backdrop?quality=80` + : `${api?.basePath}/Items/${item.Id}/Images/Backdrop?quality=80&fillHeight=1400` + : item.ImageTags?.Primary + ? `${api?.basePath}/Items/${item.Id}/Images/Primary?quality=80&fillHeight=1400` + : undefined; + return (
{ exit={{ opacity: 0 }} transition={{ duration: 0.4, ease: "easeInOut" }} > - {item.BackdropImageTags?.length ? ( + {heroBackgroundSrc ? ( {item.Name inside a line-clamped heading; as an inline image + // its box was clipped by the heading's line box (cut-off titles). + display: block; } } @@ -144,10 +147,15 @@ @media (max-width: 1140px) { .carousel { height: 60vh !important; + // Never squeeze the hero below the height its own content needs, + // otherwise the title/logo is clipped by the slide's overflow. + min-height: 430px; } .hero-carousel { &-detail { width: 100%; + padding: 3rem; + padding-bottom: 4rem; background: linear-gradient(to top, rgba(0,0,0,0.9), transparent); } @@ -156,6 +164,11 @@ } &-text { + // Narrow layouts: the hero column is ~400px, a 3.75rem title wraps + // to two lines and overflows the slide, clipping the top line. + font-size: 2.5rem !important; + line-height: 1.15 !important; + &-logo { max-width: 60vw; max-height: 10vh; diff --git a/src/utils/methods/itemImages.ts b/src/utils/methods/itemImages.ts new file mode 100644 index 0000000..c04cff4 --- /dev/null +++ b/src/utils/methods/itemImages.ts @@ -0,0 +1,118 @@ +import type { BaseItemDto, ImageType } from "@jellyfin/sdk/lib/generated-client"; + +export type ResolvedItemImage = { + /** Item that actually owns the image (episodes borrow their series' artwork). */ + itemId: string; + imageType: ImageType; + tag?: string; +}; + +const ownFallbackOrder: Partial> = { + Primary: ["Primary", "Thumb", "Backdrop"], + Thumb: ["Thumb", "Backdrop", "Primary"], + Backdrop: ["Backdrop", "Thumb", "Primary"], + Logo: ["Logo", "Primary", "Thumb"], + Banner: ["Banner", "Backdrop", "Primary"], + Art: ["Art", "Backdrop", "Primary"], + Disc: ["Disc", "Backdrop", "Primary"], +}; + +const hasOwnImage = (item: BaseItemDto, type: ImageType): boolean => { + if (type === "Backdrop") { + return Boolean(item.BackdropImageTags?.length || item.ImageTags?.Backdrop); + } + return Boolean(item.ImageTags?.[type]); +}; + +const ownTag = (item: BaseItemDto, type: ImageType): string | undefined => { + if (type === "Backdrop") { + return item.BackdropImageTags?.[0] ?? item.ImageTags?.Backdrop ?? undefined; + } + return item.ImageTags?.[type] ?? undefined; +}; + +/** + * Pick the best real image for an item. Many items do not have the requested + * image type (movies rarely have `Thumb`, episodes borrow artwork from their + * series), so try the item's own images first, then the series/parent ones. + * Returns `undefined` when there is genuinely nothing to show. + */ +export const resolveItemImage = ( + item: BaseItemDto | undefined | null, + preferred: ImageType = "Primary", + options: { seriesId?: string | null } = {}, +): ResolvedItemImage | undefined => { + if (!item) return undefined; + + // Library grids show one poster per episode, owned by the series. + const seriesId = options.seriesId ?? item.SeriesId; + if (options.seriesId && seriesId) { + return { + itemId: seriesId, + imageType: "Primary", + tag: item.SeriesPrimaryImageTag ?? undefined, + }; + } + + const order = ownFallbackOrder[preferred] ?? [preferred]; + for (const type of order) { + if (hasOwnImage(item, type) && item.Id) { + return { itemId: item.Id, imageType: type, tag: ownTag(item, type) }; + } + } + + // Episodes (and some other child items) inherit artwork from their parent. + if (seriesId && item.SeriesPrimaryImageTag) { + return { + itemId: seriesId, + imageType: "Primary", + tag: item.SeriesPrimaryImageTag, + }; + } + if (seriesId && item.Type === "Episode") { + return { itemId: seriesId, imageType: "Primary" }; + } + // Audio inherits its cover from the album. + if (item.AlbumId) { + return { itemId: item.AlbumId, imageType: "Primary" }; + } + if (item.ParentBackdropItemId && item.ParentBackdropImageTags?.length) { + return { + itemId: item.ParentBackdropItemId, + imageType: "Backdrop", + tag: item.ParentBackdropImageTags[0], + }; + } + + // Nothing known: keep the requested type so the server gets a chance, but + // only when the item exists. Callers render their icon placeholder on 404. + if (item.Id) { + return { itemId: item.Id, imageType: preferred }; + } + return undefined; +}; + +/** Image tag for the resolved type, so caches reuse the exact displayed image. */ +export const getImageTag = ( + item: BaseItemDto | undefined | null, + type: ImageType, + series = false, +): string | undefined => { + if (!item) return undefined; + if (series && type === "Primary" && item.SeriesPrimaryImageTag) { + return item.SeriesPrimaryImageTag; + } + return ownTag(item, type); +}; + +export const getAvailableImageType = ( + item: BaseItemDto | undefined | null, + preferred: ImageType = "Primary", +): ImageType => { + if (!item) return preferred; + const order = ownFallbackOrder[preferred] ?? [preferred]; + for (const type of order) { + if (hasOwnImage(item, type)) return type; + } + return preferred; +};