import { useEffect, useRef, useState } from 'react'; import type { Item } from '../plex/types'; import { useSettings } from '../app/settings'; import { itemMenu, openItem, playWithVersion } from '../app/actions'; import { Backdrop } from './Backdrop'; import { Facts, TitleArt } from './media'; import { Button, IconButton } from './primitives'; import { openMenu } from './overlays'; import { art, episodeLabel } from '../app/format'; import { useServer } from '../app/session'; /** How long each feature shows; the dot progress animation matches it. */ const ROTATE_MS = 8000; export interface Feature { item: Item; } /** * The cinematic feature at the top of Home and the Discover pages. Features * rotate every 8 seconds while the pointer is away, crossfading artwork * while the copy lifts out and rises into place. */ export function Hero({ features, variant = 'home' }: { features: Feature[]; variant?: 'home' | 'section' }) { const rotate = useSettings((s) => s.heroRotation); const reduced = useSettings((s) => s.reducedMotion); const [index, setIndex] = useState(0); const [hover, setHover] = useState(false); const [copyKey, setCopyKey] = useState(0); const root = useRef(null); const count = features.length; const feature = features[Math.min(index, count - 1)]; const server = useServer(); // Decode the next feature's artwork ahead of time so the swap is immediate. const upcoming = count > 1 ? features[(index + 1) % count]?.item : undefined; useEffect(() => { const path = upcoming ? art.backdrop(upcoming) : ''; if (!path) return; const img = new Image(); img.decoding = 'async'; img.src = server.image(path, 1600, 900); img.decode?.().catch(() => {}); }, [upcoming?.id]); // Pause rotation when the hero is scrolled away; it picks up again on its return. const [visible, setVisible] = useState(true); useEffect(() => { if (!rotate || reduced || count < 2 || hover || !visible) return; const t = setTimeout(() => go(index + 1), ROTATE_MS); return () => clearTimeout(t); }, [index, rotate, reduced, count, hover, visible]); useEffect(() => { const el = root.current; if (!el) return; const io = new IntersectionObserver(([e]) => setVisible(e.intersectionRatio > 0.4), { threshold: [0, 0.4, 1] }); io.observe(el); return () => io.disconnect(); }, []); const go = (i: number) => { setIndex(((i % count) + count) % count); setCopyKey((k) => k + 1); }; if (!feature) return
; const item = feature.item; const resume = item.viewOffset > 0; const summary = item.summary || item.tagline; return (
setHover(true)} onMouseLeave={() => setHover(false)}>
{item.type === 'episode' && (
{episodeLabel(item)} ยท {item.title}
)} {summary &&

{summary}

}
openMenu(itemMenu(item), e.currentTarget)} />
{count > 1 && (
{features.map((f, i) => ( ))}
)}
); }