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.
This commit is contained in:
1 parent
3131c1f905
commit
4c9dd86962
4 files changed
+235
-33
No files matched your search
@@ -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)}
|
||||
>
|
||||
<motion.div
|
||||
key={content[currentSlide].Id}
|
||||
className="hero-carousel-slide-wrapper"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
transition={{ duration: 0.35, ease: "easeInOut" }}
|
||||
>
|
||||
<div className="hero-carousel-slide-wrapper">
|
||||
<MemoizedCarouselSlide item={content[currentSlide]} />
|
||||
</motion.div>
|
||||
</div>
|
||||
<div className="carousel-sidebar" ref={sidebarRef}>
|
||||
{content.map((item, index) => (
|
||||
<div
|
||||
|
||||
@@ -8,9 +8,16 @@ import { green, red, yellow } from "@mui/material/colors";
|
||||
import Stack from "@mui/material/Stack";
|
||||
import Typography from "@mui/material/Typography";
|
||||
import { useNavigate } from "@tanstack/react-router";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { motion, type Variants } from "motion/react";
|
||||
import React from "react";
|
||||
import { endsAt, getRuntime } from "@/utils/date/time";
|
||||
import {
|
||||
getHeroPosterUrl,
|
||||
getItemImagesQueryOptions,
|
||||
resolveHeroArtwork,
|
||||
} from "@/utils/methods/heroArtwork";
|
||||
import BlurhashCanvas from "../blurhash-canvas";
|
||||
|
||||
import { useApiInContext } from "@/utils/store/api";
|
||||
import { useCarouselStore } from "@/utils/store/carousel";
|
||||
@@ -25,10 +32,11 @@ const MotionTypography = motion.create(Typography);
|
||||
const MotionStack = motion.create(Stack);
|
||||
|
||||
const textVariants: Variants = {
|
||||
initial: (direction: string) => ({
|
||||
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 (
|
||||
<div className="hero-carousel-slide">
|
||||
<motion.div
|
||||
className="hero-carousel-background-container"
|
||||
initial={{ opacity: 0 }}
|
||||
initial={{ opacity: 1 }}
|
||||
animate={{ opacity: 1 }}
|
||||
exit={{ opacity: 0 }}
|
||||
transition={{ duration: 0.4, ease: "easeInOut" }}
|
||||
>
|
||||
{heroBackgroundSrc ? (
|
||||
{heroArtwork?.blurhash && (
|
||||
<BlurhashCanvas
|
||||
blurhashString={heroArtwork.blurhash}
|
||||
width={48}
|
||||
height={27}
|
||||
canvasProps={{
|
||||
className: "hero-carousel-background-blurhash",
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{heroArtwork ? (
|
||||
<img
|
||||
alt={item.Name ?? "item"}
|
||||
className="hero-carousel-background-image"
|
||||
src={heroBackgroundSrc}
|
||||
style={{
|
||||
opacity: 0,
|
||||
}}
|
||||
onLoad={(e) => {
|
||||
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")}
|
||||
</div>
|
||||
)}
|
||||
{heroPoster && (
|
||||
<img
|
||||
alt={item.Name ?? "poster"}
|
||||
className="hero-carousel-poster"
|
||||
src={heroPoster}
|
||||
loading="eager"
|
||||
/>
|
||||
)}
|
||||
</motion.div>
|
||||
<motion.div
|
||||
className="hero-carousel-detail"
|
||||
|
||||
@@ -57,7 +57,22 @@
|
||||
object-fit: cover;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
transition: opacity 0.5s;
|
||||
|
||||
// Poster-only item: never stretch/crop a 2:3 poster across 16:9.
|
||||
// Blur it into an ambient background and show the sharp poster on top.
|
||||
&.is-poster {
|
||||
filter: blur(36px) saturate(1.6) brightness(0.65);
|
||||
transform: scale(1.25);
|
||||
}
|
||||
}
|
||||
|
||||
&-blurhash {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
width: 100% !important;
|
||||
height: 100% !important;
|
||||
z-index: 0;
|
||||
filter: blur(30px) saturate(1.4);
|
||||
}
|
||||
|
||||
&-icon-container {
|
||||
@@ -101,6 +116,20 @@
|
||||
}
|
||||
}
|
||||
|
||||
&-poster {
|
||||
position: absolute;
|
||||
z-index: 3;
|
||||
right: 7%;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
height: 66%;
|
||||
max-width: 26%;
|
||||
object-fit: contain;
|
||||
border-radius: 14px;
|
||||
box-shadow: 0 24px 60px rgb(0 0 0 / 0.65), 0 0 0 1px rgb(255 255 255 / 0.08);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
&-overview {
|
||||
overflow: hidden;
|
||||
margin-bottom: 0.5em;
|
||||
@@ -152,6 +181,10 @@
|
||||
min-height: 430px;
|
||||
}
|
||||
.hero-carousel {
|
||||
&-poster {
|
||||
display: none;
|
||||
}
|
||||
|
||||
&-detail {
|
||||
width: 100%;
|
||||
padding: 3rem;
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
import type { Api } from "@jellyfin/sdk";
|
||||
import type {
|
||||
BaseItemDto,
|
||||
ImageInfo,
|
||||
ImageType,
|
||||
} from "@jellyfin/sdk/lib/generated-client";
|
||||
import { getImageApi } from "@jellyfin/sdk/lib/utils/api/image-api";
|
||||
import { queryOptions } from "@tanstack/react-query";
|
||||
|
||||
export type HeroArtwork = {
|
||||
url: string;
|
||||
/** Primary (poster) is portrait; it must not be cropped into a 16:9 hero. */
|
||||
isPortrait: boolean;
|
||||
/** BlurHash for the chosen image, used as an instant placeholder. */
|
||||
blurhash?: string;
|
||||
};
|
||||
|
||||
/** All images Jellyfin actually stores for an item (with real resolutions). */
|
||||
export const getItemImagesQueryOptions = (
|
||||
api: Api | null | undefined,
|
||||
itemId: string | undefined | null,
|
||||
) =>
|
||||
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,
|
||||
});
|
||||
};
|
||||
Reference in new issue
Block a user