Kaleido-Desktop/src/ui/Hero.tsx
Elijah 8755d16735
All checks were successful
Validate Kaleido Desktop / validate (push) Successful in 1m22s
Fix playback, account, download and library bugs found in review
- Clear cached library data when the account or profile behind the
  server connection changes, not only on Home user switches.
- TitleArt falls back to the title when there is no server, so the
  player no longer crashes offline.
- Playback keeps the server it started on: progress, watched state and
  transcodes never go to a server selected later, a download only
  reports to its own server, and switching account or server ends a
  stream from the old one.
- Closing or replacing playback during setup can no longer start the
  file afterwards.
- Transcodes save the chosen audio/subtitle tracks on the server before
  starting (and name them in the request), apply the language and
  subtitle preferences, and keep the current tracks across quality
  changes.
- Offline playback saves its resume point and watched count in the
  download's metadata.
- Download tasks take over from a previous task for the same title only
  after it exits, and pause/remove stay wired to the current task.
- Downloads prefer a single-file version and refuse split ones instead
  of saving only part one.
- Download artwork is named per file, so episodes in one season folder
  no longer overwrite or delete each other's images.
- Library grids refetch loaded pages when the cache is invalidated.
- Request timeouts and cancellation also cover the response body.
- Hero rotation resumes after being scrolled back into view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 22:41:12 -07:00

105 lines
4.3 KiB
TypeScript

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<HTMLElement>(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 <section className={`hero ${variant} empty`} />;
const item = feature.item;
const resume = item.viewOffset > 0;
const summary = item.summary || item.tagline;
return (
<section ref={root} className={`hero ${variant}`} onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}>
<Backdrop item={item} className="hero-backdrop" />
<div className="hero-content" key={copyKey}>
<TitleArt item={item} className="hero-title" maxWidth={variant === 'home' ? 420 : 380} maxHeight={variant === 'home' ? 128 : 108} />
{item.type === 'episode' && (
<div className="hero-episode">
{episodeLabel(item)} · {item.title}
</div>
)}
<Facts item={item} />
{summary && <p className="hero-summary">{summary}</p>}
<div className="actions hero-actions">
<Button variant="primary" icon="play" onClick={() => playWithVersion(item)}>
{resume ? 'Resume' : 'Play'}
</Button>
<Button variant="muted" icon="info" onClick={() => openItem(item.type === 'episode' ? { ...item, id: item.grandparentId || item.id } : item)}>
More info
</Button>
<IconButton round icon="dots" label="More actions" onClick={(e) => openMenu(itemMenu(item), e.currentTarget)} />
</div>
</div>
{count > 1 && (
<div className="hero-dots" role="tablist" aria-label="Featured titles">
{features.map((f, i) => (
<button key={f.item.id} role="tab" aria-selected={i === index} aria-label={f.item.title} className={i === index ? 'active' : ''} onClick={() => go(i)}>
{i === index && rotate && !reduced && !hover && visible && <i key={copyKey} className="hero-dot-progress" />}
</button>
))}
</div>
)}
</section>
);
}