From 4c9dd869627ce594e2e811c3d2d98f24c84dfe94 Mon Sep 17 00:00:00 2001 From: 1xDed Date: Fri, 27 Nov 2026 09:20:26 +0300 Subject: [PATCH] feat(hero): pick the best Jellyfin artwork for the home hero Jellyfin stores several images per item. Ask /Items/{id}/Images for their real sizes and use the biggest wide image (Backdrop, then Thumb) for the 16:9 hero. Poster-only items are no longer stretched: the poster is blurred into an ambient background with the sharp poster displayed beside the text. The slide element is kept mounted across changes so the previous artwork stays until the next loads. --- src/components/carousel/index.tsx | 11 +- src/components/carouselSlide/index.tsx | 64 ++++++---- src/routes/_api/home/home.scss | 35 +++++- src/utils/methods/heroArtwork.ts | 158 +++++++++++++++++++++++++ 4 files changed, 235 insertions(+), 33 deletions(-) create mode 100644 src/utils/methods/heroArtwork.ts diff --git a/src/components/carousel/index.tsx b/src/components/carousel/index.tsx index a6c31c5..b3e9841 100644 --- a/src/components/carousel/index.tsx +++ b/src/components/carousel/index.tsx @@ -1,4 +1,3 @@ -import { motion } from "motion/react"; import React, { useCallback, useEffect, useState } from "react"; import type { Api } from "@jellyfin/sdk"; @@ -116,15 +115,9 @@ const Carousel = ({ onMouseEnter={() => setIsPaused(true)} onMouseLeave={() => setIsPaused(false)} > - +
- +
{content.map((item, index) => (
({ - x: direction === "right" ? 40 : -40, - opacity: 0, - }), + // No hidden start state: hero text must not blink on slide changes. + initial: { + x: 0, + opacity: 1, + }, animate: { x: 0, opacity: 1, @@ -61,36 +69,38 @@ const CarouselSlide = ({ item }: { item: BaseItemDto }) => { 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; + // Jellyfin stores several images per item (poster, thumb, backdrops). + // Ask the server which ones exist and their real size, then pick the wide one. + const imageInfos = useQuery(getItemImagesQueryOptions(api, item.Id)); + const heroArtwork = resolveHeroArtwork(api, item, imageInfos.data); + const heroPoster = heroArtwork?.isPortrait + ? getHeroPosterUrl(api, item) + : undefined; return (
- {heroBackgroundSrc ? ( + {heroArtwork?.blurhash && ( + + )} + {heroArtwork ? ( {item.Name { - e.currentTarget.style.opacity = "1"; - }} + className={`hero-carousel-background-image${heroArtwork.isPortrait ? " is-poster" : ""}`} + src={heroArtwork.url} loading="eager" /> ) : ( @@ -98,6 +108,14 @@ const CarouselSlide = ({ item }: { item: BaseItemDto }) => { {getTypeIcon(item.Type ?? "Movie")}
)} + {heroPoster && ( + {item.Name + )} + queryOptions({ + queryKey: ["item", "images", itemId], + queryFn: async () => { + if (!api || !itemId) return []; + const result = await getImageApi(api).getItemImageInfos({ itemId }); + return result.data; + }, + enabled: Boolean(api && itemId), + staleTime: 30 * 60_000, + }); + +/** Prefer the biggest wide image Jellyfin has; Backdrop wins ties over Thumb. */ +const pickWideImage = (images: ImageInfo[]): ImageInfo | undefined => { + const wide = images.filter((image) => { + if (image.ImageType !== "Backdrop" && image.ImageType !== "Thumb") { + return false; + } + const width = image.Width ?? 0; + const height = image.Height ?? 0; + return height > 0 && width / height >= 1.3; + }); + wide.sort((a, b) => { + const area = (i: ImageInfo) => (i.Width ?? 0) * (i.Height ?? 0); + const diff = area(b) - area(a); + if (diff !== 0) return diff; + return a.ImageType === "Backdrop" ? -1 : 1; + }); + return wide[0]; +}; + +const imageUrl = ( + api: Api, + itemId: string, + type: ImageType, + tag: string | null | undefined, + params: { maxWidth?: number; maxHeight?: number }, +) => { + const query = new URLSearchParams(); + if (tag) query.set("tag", tag); + if (params.maxWidth) query.set("maxWidth", String(params.maxWidth)); + if (params.maxHeight) query.set("maxHeight", String(params.maxHeight)); + query.set("quality", "90"); + return `${api.basePath}/Items/${itemId}/Images/${type}?${query.toString()}`; +}; + +const blurhashFor = ( + item: BaseItemDto, + type: ImageType, + tag: string | null | undefined, +) => (tag ? item.ImageBlurHashes?.[type]?.[tag] ?? undefined : undefined); + +/** + * Best artwork for the home hero (a wide 16:9 area). + * + * Jellyfin usually stores several images per item; the player/hero should use + * a wide one: Backdrop, then Thumb (often a 4K still). A Primary poster is 2:3, + * so it is only used when nothing wide exists, and the caller renders it as a + * poster on a blurred background instead of stretching it. + */ +export const resolveHeroArtwork = ( + api: Api | undefined, + item: BaseItemDto | undefined | null, + images: ImageInfo[] | undefined, +): HeroArtwork | undefined => { + if (!api || !item?.Id) return undefined; + + const wide = images?.length ? pickWideImage(images) : undefined; + if (wide) { + return { + url: imageUrl(api, item.Id, wide.ImageType as ImageType, wide.ImageTag, { + maxWidth: 1920, + }), + isPortrait: false, + blurhash: blurhashFor(item, wide.ImageType as ImageType, wide.ImageTag), + }; + } + + // Fall back to tags while /Images is still loading (or for servers that + // don't return image infos). + if (item.BackdropImageTags?.length) { + return { + url: imageUrl(api, item.Id, "Backdrop", item.BackdropImageTags[0], { + maxWidth: 1920, + }), + isPortrait: false, + blurhash: blurhashFor(item, "Backdrop", item.BackdropImageTags[0]), + }; + } + if (item.ImageTags?.Thumb) { + return { + url: imageUrl(api, item.Id, "Thumb", item.ImageTags.Thumb, { + maxWidth: 1920, + }), + isPortrait: false, + blurhash: blurhashFor(item, "Thumb", item.ImageTags.Thumb), + }; + } + if (item.ImageTags?.Primary) { + return { + url: imageUrl(api, item.Id, "Primary", item.ImageTags.Primary, { + maxHeight: 1400, + }), + isPortrait: true, + blurhash: blurhashFor(item, "Primary", item.ImageTags.Primary), + }; + } + if (item.ParentBackdropItemId && item.ParentBackdropImageTags?.length) { + return { + url: imageUrl( + api, + item.ParentBackdropItemId, + "Backdrop", + item.ParentBackdropImageTags[0], + { maxWidth: 1920 }, + ), + isPortrait: false, + blurhash: blurhashFor( + item, + "Backdrop", + item.ParentBackdropImageTags[0], + ), + }; + } + return undefined; +}; + +/** Sharp poster URL for the poster-on-blurred-background hero treatment. */ +export const getHeroPosterUrl = ( + api: Api | undefined, + item: BaseItemDto | undefined | null, +) => { + if (!api || !item?.Id || !item.ImageTags?.Primary) return undefined; + return imageUrl(api, item.Id, "Primary", item.ImageTags.Primary, { + maxHeight: 900, + }); +};