Files
ersatztv/web/src/media/MediaPosterCard.tsx
T
timothyandClaude Fable 5 5b945c308d
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 7m30s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 11s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 9m53s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
fix(spa): episode anchor nav must handle in-app popstate + not re-scroll on refetch (#220 review)
Adversarial review of #220 found in-grid episode card clicks never
scrolled/highlighted: navigateToPath() (routing.ts) does pushState +
a synthetic popstate, not a real hash change, so the anchor effect's
hashchange-only listener never fired for same-pathname navigation.
Now listens to both hashchange and popstate.

Also: track the last anchor value actually scrolled to so a
refetch/pagination that recreates the items array (anchor unchanged)
doesn't hijack scroll position; document the known CHILD_PAGE_SIZE
deep-link limitation (parity with the Blazor fragment link); and fix
the MediaPosterCard/shell.css comments that described the highlight
ring as "temporary" when only its glow pulse fades, not the ring
itself.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 23:36:15 +02:00

111 lines
3.9 KiB
TypeScript

import type { CSSProperties, ReactNode } from 'react';
import { Film } from 'lucide-react';
import type { LibraryBrowseItem } from '../api';
import { TYPE_ICON, TYPE_LABEL, hueOf, itemSubtitle } from './mediaKinds';
// A poster/thumbnail grid card for a browse/search item. Selectable + clickable for the media
// browse/search/trash screens. Falls back to a deterministic gradient + type icon when no artwork.
// `actions` renders an overlay in the poster's top-right corner (e.g. an Add-to menu button);
// interactions inside it are isolated from the card's open/select gesture via stopPropagation.
export function MediaPosterCard({
item,
selected,
onToggleSelect,
onOpen,
height = 150,
actions,
id,
highlighted
}: {
item: LibraryBrowseItem;
selected?: boolean;
onToggleSelect?: (item: LibraryBrowseItem) => void;
onOpen?: (item: LibraryBrowseItem) => void;
height?: number;
actions?: ReactNode;
// Stable DOM id (e.g. `episode-{id}`) so callers can deep-link/scroll to this card.
id?: string;
// Applies a ring highlight while this card is the deep-link target (e.g. the current
// `#episode-{id}` hash) — persists for as long as the anchor matches this card, with only the
// ring's outer glow pulse fading shortly after mount (see .ctv-media-card-highlighted in
// shell.css).
highlighted?: boolean;
}) {
const hue = hueOf(item.title);
const Icon = TYPE_ICON[item.mediaType] ?? Film;
const subtitle = itemSubtitle(item);
const posterStyle: CSSProperties = {
width: '100%',
height,
background: `linear-gradient(160deg, hsl(${hue} 24% 17%), hsl(${hue} 26% 12%))`
};
// Selection (channel builder) takes precedence over open (browse drill-in) when both are supplied.
const selectable = typeof onToggleSelect === 'function';
const openable = !selectable && typeof onOpen === 'function';
const interactive = selectable || openable;
const activate = () => {
if (selectable) {
onToggleSelect?.(item);
} else if (openable) {
onOpen?.(item);
}
};
return (
<div
className={`ctv-media-card${selected ? ' ctv-media-card-selected' : ''}${interactive ? ' ctv-press' : ''}${highlighted ? ' ctv-media-card-highlighted' : ''}`}
id={id}
onClick={interactive ? activate : undefined}
role={interactive ? 'button' : undefined}
tabIndex={interactive ? 0 : undefined}
onKeyDown={
interactive
? (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
activate();
}
}
: undefined
}
aria-pressed={selectable ? selected ?? false : undefined}
>
<div className="ctv-media-card-poster" style={posterStyle}>
{item.artwork ? (
<img src={item.artwork} alt="" loading="lazy" />
) : (
<div
style={{
position: 'absolute',
inset: 0,
backgroundImage: `repeating-linear-gradient(135deg, hsl(${hue} 22% 10% / .55) 0 1px, transparent 1px 9px)`
}}
/>
)}
<span className="ctv-media-card-icon" style={{ color: `hsl(${hue} 30% 68%)` }}>
<Icon size={15} aria-hidden="true" />
</span>
{actions && (
<div
className="ctv-media-card-actions"
// Keep the actions slot's own clicks/keys from bubbling up to the card's
// open/select gesture.
onClick={(event) => event.stopPropagation()}
onKeyDown={(event) => event.stopPropagation()}
>
{actions}
</div>
)}
{selected && <div className="ctv-media-card-check">✓</div>}
</div>
<div className="ctv-media-card-body">
<div className="ctv-media-card-title" title={item.title}>
{item.title}
</div>
<div className="ctv-media-card-meta">{subtitle || TYPE_LABEL[item.mediaType]}</div>
</div>
</div>
);
}