Kaleido-Desktop/src/ui/HoverPreview.tsx
Elijah 43c168da95 Address the first beta's bugs and requested changes
Startup now uses the Android iris animation, and playback can continue
in a corner miniplayer while browsing (the video is drawn into a corner
rectangle beneath a matching hole in the page). The player keeps the
window controls, hides its chrome when the pointer leaves, and gains
audio/subtitle tracks, audio sync, source bitrate and playback details
in its options. Subtitles can be styled in Settings with a live preview.
Categories replace Collections, the A–Z rail magnifies, descriptions
expand in place, downloads can be removed in bulk, and search keeps its
results while typing.

Fixes scroll jank from per-card and scrolling backdrop blurs, the hidden
colour wash, the cubic volume curve, loud theme music, the search focus
ring, a stray nav indicator, Continue Watching previews and the TV Shows
header overlap. Removes the star rating, the duplicate Settings menu
entry and the server settings' Show advanced switch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 18:06:54 -07:00

115 lines
4.6 KiB
TypeScript

import { useEffect, useLayoutEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { create } from 'zustand';
import { useRouter } from '../app/router';
import type { Item } from '../plex/types';
import { useSession } from '../app/session';
import { art, duration, episodeLabel } from '../app/format';
import { Ratings } from './media';
/**
* A quiet preview shown after the pointer rests on a poster: artwork, facts
* and synopsis, without opening the title. It never captures the pointer.
*/
const usePreview = create<{ item: Item | null; rect: DOMRect | null; anchor: HTMLElement | null; show(item: Item, anchor: HTMLElement): void; hide(): void }>((set, get) => ({
item: null,
rect: null,
anchor: null,
show: (item, anchor) => set({ item, anchor, rect: anchor.getBoundingClientRect() }),
// Called on every wheel event; only notify when something was showing.
hide: () => get().item && set({ item: null, rect: null, anchor: null }),
}));
let timer: ReturnType<typeof setTimeout> | null = null;
const DELAY = 650;
export function previewHandlers(item: Item) {
return {
onMouseEnter: (e: React.MouseEvent) => {
const el = e.currentTarget as HTMLElement;
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
if (el.matches(':hover') && !document.querySelector('.menu-layer')) usePreview.getState().show(item, el);
}, DELAY);
},
onMouseLeave: () => {
if (timer) clearTimeout(timer);
timer = null;
usePreview.getState().hide();
},
onMouseDown: () => {
if (timer) clearTimeout(timer);
usePreview.getState().hide();
},
};
}
export function HoverPreviewHost() {
const { item, rect } = usePreview();
const server = useSession((s) => s.server);
const ref = useRef<HTMLDivElement>(null);
const [pos, setPos] = useState<{ left: number; top: number; side: 'right' | 'left' } | null>(null);
useEffect(() => {
const close = () => usePreview.getState().hide();
// The anchor card can disappear (navigation, re-render) without a mouseleave.
const check = () => {
const a = usePreview.getState().anchor;
if (a && (!a.isConnected || !a.matches(':hover'))) close();
};
const unsub = useRouter.subscribe(close);
window.addEventListener('wheel', close, { passive: true });
window.addEventListener('keydown', close);
window.addEventListener('input', close, true);
window.addEventListener('mousemove', check, { passive: true });
return () => {
unsub();
window.removeEventListener('wheel', close);
window.removeEventListener('keydown', close);
window.removeEventListener('input', close, true);
window.removeEventListener('mousemove', check);
};
}, []);
useLayoutEffect(() => {
if (!rect || !ref.current) return setPos(null);
const w = ref.current.offsetWidth;
const h = ref.current.offsetHeight;
const side = rect.right + 14 + w < window.innerWidth - 12 ? 'right' : 'left';
const left = side === 'right' ? rect.right + 14 : rect.left - 14 - w;
const top = Math.min(Math.max(76, rect.top + rect.height / 2 - h / 2), window.innerHeight - h - 12);
setPos({ left, top, side });
}, [rect, item]);
if (!item || !rect || !server) return null;
const backdrop = art.backdrop(item);
const summary = item.summary || item.tagline;
const facts = [
item.type === 'episode' ? episodeLabel(item) : item.year || '',
item.type === 'show' ? (item.childCount ? `${item.childCount} season${item.childCount === 1 ? '' : 's'}` : '') : duration(item.duration),
item.genres.slice(0, 2).map((g) => g.tag).join(', '),
].filter(Boolean);
return createPortal(
<div ref={ref} className={`hover-preview ${pos?.side ?? 'right'}`} style={{ left: pos?.left ?? -9999, top: pos?.top ?? -9999 }} aria-hidden="true">
{backdrop && (
<div className="hover-preview-art">
<img src={server.image(backdrop, 360, 202)} alt="" />
{item.logo && <img className="hover-preview-logo" src={server.image(item.logo, 200, 64, { format: 'png' })} alt="" />}
</div>
)}
<div className="hover-preview-body">
{!item.logo && <b className="hover-preview-title">{item.type === 'episode' ? item.grandparentTitle : item.title}</b>}
<div className="hover-preview-facts">
{item.contentRating && <span className="badge">{item.contentRating}</span>}
{facts.map((f) => (
<span key={f}>{f}</span>
))}
</div>
<Ratings item={item} compact />
{summary && <p>{summary}</p>}
</div>
</div>,
document.body,
);
}