import { useEffect, useRef, useState } from 'react'; import type { Item } from '../plex/types'; import { useServer } from '../app/session'; import { useSettings } from '../app/settings'; import { art, washGradient } from '../app/format'; interface Layer { key: number; src: string; wash: string; ready: boolean; } /** * Artwork-led page background. The image sits top-right and fades into the * page toward its left and bottom edges; the UltraBlur colour wash tints the * page beneath it and carries on down behind the rows. Changes crossfade; the * outgoing layer stays until the new image has decoded. */ export function Backdrop({ item, className = '', width = 1600, height = 900, wash: forceWash }: { item: Item | null | undefined; className?: string; width?: number; height?: number; wash?: Item['ultraBlur'] }) { const server = useServer(); const colorWash = useSettings((s) => s.colorWash); const [layers, setLayers] = useState([]); const counter = useRef(0); const path = item ? art.backdrop(item) : ''; const src = path ? server.image(path, width, height) : ''; const blur = forceWash ?? item?.ultraBlur ?? null; const wash = colorWash && blur ? washGradient(blur) : ''; useEffect(() => { if (!src && !wash) return; setLayers((prev) => { if (prev.length && prev[prev.length - 1].src === src) { // Same art, maybe new wash. return prev.map((l, i) => (i === prev.length - 1 ? { ...l, wash } : l)); } return [...prev.slice(-1), { key: ++counter.current, src, wash, ready: !src }]; }); }, [src, wash]); const onReady = (key: number) => setLayers((prev) => { const idx = prev.findIndex((l) => l.key === key); if (idx < 0) return prev; const next = prev.map((l) => (l.key === key ? { ...l, ready: true } : l)); // Drop older layers once the newest is visible (after its fade). setTimeout(() => setLayers((p) => (p.length > 1 && p[p.length - 1].key === key ? p.slice(-1) : p)), 500); return next; }); return ( <> {colorWash && (