295 lines
10 KiB
TypeScript
295 lines
10 KiB
TypeScript
// What both §9.6 detail pages share: the hero, the action bar's links, and
|
||
// the removal confirmation.
|
||
|
||
import { type CSSProperties, type ReactNode, useEffect, useRef, useState } from "react";
|
||
import { Icon, ProviderMark, Star } from "./icons";
|
||
import { control, NOTE, detail as page, type } from "./look";
|
||
|
||
/** each line keeps its height while empty, so the hero never grows in steps */
|
||
const HERO_LINE = "readout flex items-center gap-1 min-h-line m-0 text-xs";
|
||
|
||
import { formatMetaLine, formatRating, formatVoteCount, tmdbImage } from "./movie";
|
||
import { type ActionOutcome, formatSize, totalSize } from "./releases";
|
||
import { cx, flag, IconControl, IconLink, type Note, NoteLine } from "./ui";
|
||
|
||
/** What both detail heroes paint from TMDB (§9.6). */
|
||
export interface HeroDetail {
|
||
poster_path: string | null;
|
||
backdrop_path: string | null;
|
||
vote_average: number | null;
|
||
vote_count: number;
|
||
runtime: number | null;
|
||
genres: string[];
|
||
tagline: string | null;
|
||
overview: string | null;
|
||
}
|
||
|
||
/**
|
||
* The detail hero is a fixed grid: the poster's box, the chip line, one line
|
||
* each for rating and meta, and three reserved lines of overview. Data fills
|
||
* the slots; it never adds them, so the page does not grow in steps.
|
||
*/
|
||
export function Hero({
|
||
title,
|
||
year,
|
||
detail,
|
||
chips,
|
||
note,
|
||
onBack,
|
||
}: {
|
||
title: string;
|
||
year: number | null;
|
||
detail: HeroDetail | null;
|
||
chips: ReactNode;
|
||
note: Note | null;
|
||
onBack: () => void;
|
||
}) {
|
||
const heading = useRef<HTMLHeadingElement>(null);
|
||
const [posterLoaded, setPosterLoaded] = useState(false);
|
||
const [posterBroken, setPosterBroken] = useState(false);
|
||
const [overviewFull, setOverviewFull] = useState(false);
|
||
useEffect(() => {
|
||
heading.current?.focus({ preventScroll: true });
|
||
window.scrollTo({ top: 0 });
|
||
}, []);
|
||
|
||
const posterUrl = detail === null ? null : tmdbImage(detail.poster_path, "w342");
|
||
const backdropUrl = detail === null ? null : tmdbImage(detail.backdrop_path, "w1280");
|
||
const tagline = detail?.tagline;
|
||
|
||
return (
|
||
<section
|
||
className={cx("hero", backdropUrl !== null && "has-backdrop")}
|
||
style={
|
||
backdropUrl === null
|
||
? undefined
|
||
: ({ "--backdrop": `url("${backdropUrl}")` } as CSSProperties)
|
||
}
|
||
aria-label="title metadata"
|
||
data-bleed=""
|
||
>
|
||
<header className={page.pageHead}>
|
||
<IconControl glyph="back" label="back" look="bare" onClick={onBack} />
|
||
<h2 className={cx(type.pageTitle, "flex-initial")} tabIndex={-1} ref={heading}>
|
||
{title === "" ? " " : title}
|
||
</h2>
|
||
<span className={page.pageMeta}>{year === null ? "" : year}</span>
|
||
</header>
|
||
<div className="grid [grid-template-columns:6rem_minmax(0,1fr)] gap-4 [align-items:start] @row:[grid-template-columns:9rem_minmax(0,1fr)] @row:gap-6 @wide:[grid-template-columns:10.5rem_minmax(0,1fr)]">
|
||
<div className="aspect-2/3 bg-panel-raised border border-line-strong rounded-panel overflow-hidden">
|
||
<img
|
||
className="block w-full h-full object-cover opacity-0 [transition:opacity_300ms_var(--ease-out)] data-loaded:opacity-100"
|
||
alt={posterUrl === null || posterBroken ? "" : `${title} poster`}
|
||
src={posterUrl === null || posterBroken ? undefined : posterUrl}
|
||
data-loaded={flag(posterLoaded && !posterBroken)}
|
||
onLoad={() => setPosterLoaded(true)}
|
||
onError={() => setPosterBroken(true)}
|
||
/>
|
||
</div>
|
||
<div className="grid gap-2 content-start min-w-0">
|
||
<span className="flex flex-wrap gap-1 min-h-chip-h">{chips}</span>
|
||
<p className={cx(HERO_LINE, "text-ink")}>
|
||
{detail !== null && detail.vote_average !== null && (
|
||
<>
|
||
<Star />
|
||
{`${formatRating(detail.vote_average)} `}
|
||
<span className="dim">{`(${formatVoteCount(detail.vote_count)})`}</span>
|
||
</>
|
||
)}
|
||
</p>
|
||
<p className={cx(HERO_LINE, "text-ink-muted")}>
|
||
{detail === null ? "" : formatMetaLine(detail.runtime, detail.genres)}
|
||
</p>
|
||
<p className="hidden @row:block m-0 font-display text-md font-semibold text-ink-muted">
|
||
{tagline !== null && tagline !== undefined && tagline !== "" ? `“${tagline}”` : ""}
|
||
</p>
|
||
{/* biome-ignore lint/a11y/useKeyWithClickEvents: a reading aid; the full text is in the DOM either way */}
|
||
<p
|
||
className="line-clamp-3 min-h-overview max-w-measure m-0 text-sm text-ink cursor-pointer data-full:[-webkit-line-clamp:unset]"
|
||
data-full={flag(overviewFull)}
|
||
onClick={() => setOverviewFull(!overviewFull)}
|
||
>
|
||
{detail?.overview ?? ""}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
<NoteLine note={note} className={cx(NOTE, "mt-2")} />
|
||
</section>
|
||
);
|
||
}
|
||
|
||
/** The trailer: the play glyph, and the word wherever there is room for it. */
|
||
export function TrailerLink({ href, title }: { href: string; title: string }) {
|
||
return (
|
||
<IconLink shape="trailer" label={`watch the ${title} trailer`} href={href}>
|
||
<Icon name="play" />
|
||
<span className="hidden @row:inline">trailer</span>
|
||
</IconLink>
|
||
);
|
||
}
|
||
|
||
export function ProviderLink({
|
||
mark,
|
||
label,
|
||
href,
|
||
}: {
|
||
mark: "tmdb" | "tvdb" | "imdb" | "rt";
|
||
label: string;
|
||
href: string;
|
||
}) {
|
||
return (
|
||
<IconLink label={label} href={href}>
|
||
<ProviderMark name={mark} />
|
||
</IconLink>
|
||
);
|
||
}
|
||
|
||
/** The slice of a file the removal panel reads: evidence, nothing else. */
|
||
export interface RemoveFile {
|
||
path: string;
|
||
size: number;
|
||
}
|
||
|
||
/**
|
||
* What the panel needs to know about a title (issue 175): a movie and a
|
||
* series differ only in which endpoints they call, how their files roll up
|
||
* to one folder, and how many torrents the §7.3 warning speaks of.
|
||
*/
|
||
export interface RemoveSubject {
|
||
title: string;
|
||
files: () => Promise<{ kind: "files"; files: RemoveFile[] } | { kind: "error"; detail: string }>;
|
||
/** The one folder the delete takes, when the files agree on it. */
|
||
folder: (files: RemoveFile[]) => string | null;
|
||
remove: () => Promise<ActionOutcome>;
|
||
/** What does not happen: seeding continues under its own rule (§7.3). */
|
||
seedLine: string;
|
||
}
|
||
|
||
/**
|
||
* The removal confirmation (issue 104, simplified by 110): removing a title
|
||
* always unlinks its §7.4 folder, so there is one decision, not two.
|
||
*
|
||
* It names the §7.4 folder it would unlink rather than promising in the
|
||
* abstract — the service knows only what it wrote (§2), so the file list is
|
||
* the whole truth about what disappears. And it says what does not happen:
|
||
* seeding continues under its own rule (§7.3).
|
||
*/
|
||
export function RemovePanel({
|
||
subject,
|
||
onCancel,
|
||
onRemoved,
|
||
}: {
|
||
subject: RemoveSubject;
|
||
onCancel: () => void;
|
||
onRemoved: () => void;
|
||
}) {
|
||
const [evidence, setEvidence] = useState("reading files…");
|
||
const [path, setPath] = useState<string | null>(null);
|
||
const [files, setFiles] = useState<RemoveFile[] | null>(null);
|
||
const [folderNamed, setFolderNamed] = useState(false);
|
||
const [working, setWorking] = useState<{ text: string; tone?: "fault" } | null>(null);
|
||
const [busy, setBusy] = useState(false);
|
||
const keep = useRef<HTMLButtonElement>(null);
|
||
|
||
// cancel takes focus, not the destructive action: no toggle is left to arm first
|
||
useEffect(() => {
|
||
keep.current?.focus();
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
let live = true;
|
||
void subject.files().then((outcome) => {
|
||
if (!live) {
|
||
return;
|
||
}
|
||
if (outcome.kind === "error") {
|
||
setEvidence(`files unreadable — ${outcome.detail}`);
|
||
return;
|
||
}
|
||
setFiles(outcome.files);
|
||
if (outcome.files.length === 0) {
|
||
setEvidence("nothing on disk");
|
||
return;
|
||
}
|
||
const count = `${outcome.files.length} ${outcome.files.length === 1 ? "file" : "files"}`;
|
||
setEvidence(`${count} · ${formatSize(totalSize(outcome.files))}`);
|
||
const folder = subject.folder(outcome.files);
|
||
setFolderNamed(folder !== null);
|
||
setPath(folder ?? outcome.files.map((file) => file.path).join("\n"));
|
||
});
|
||
return () => {
|
||
live = false;
|
||
};
|
||
}, [subject]);
|
||
|
||
const hasFiles = files !== null && files.length > 0;
|
||
const note =
|
||
working ??
|
||
(hasFiles
|
||
? {
|
||
text: `${folderNamed ? "deletes the folder above." : "deletes the files above."} ${subject.seedLine}`,
|
||
tone: "warn" as const,
|
||
}
|
||
: { text: "the library entry goes. nothing was imported for this title." });
|
||
|
||
const confirm = () => {
|
||
setBusy(true);
|
||
setWorking({ text: "removing title and files…" });
|
||
void subject.remove().then((outcome) => {
|
||
if (outcome.kind === "done") {
|
||
onRemoved();
|
||
return;
|
||
}
|
||
setBusy(false);
|
||
// the row is still there on a failed unlink, so retrying is the fix
|
||
setWorking({ text: `remove failed — ${outcome.detail}`, tone: "fault" });
|
||
});
|
||
};
|
||
|
||
// evidence, then what removing it costs
|
||
return (
|
||
// biome-ignore lint/a11y/useSemanticElements: a fieldset brings its own border, padding and min-width, which this group does not want
|
||
<div
|
||
className={cx(
|
||
"grid gap-2 p-3 bg-panel-raised border rounded-panel",
|
||
hasFiles ? "[border-color:oklch(from_var(--signal-warn)_l_c_h/55%)]" : "border-line",
|
||
)}
|
||
role="group"
|
||
aria-label={`remove ${subject.title}`}
|
||
data-armed={String(hasFiles)}
|
||
>
|
||
<p className="readout dim m-0 text-xs">{evidence}</p>
|
||
<p
|
||
className="readout m-0 text-xs text-ink-muted [overflow-wrap:anywhere] whitespace-pre-line"
|
||
hidden={path === null}
|
||
>
|
||
{path ?? ""}
|
||
</p>
|
||
<p
|
||
className="readout m-0 text-xs text-ink-muted data-[tone=warn]:text-signal-warn data-[tone=fault]:text-signal-fault"
|
||
id="remove-note"
|
||
role="status"
|
||
data-tone={note.tone}
|
||
>
|
||
{note.text}
|
||
</p>
|
||
<div className="flex flex-wrap gap-2">
|
||
{/* armed: the confirm turns caution amber with the rest of the panel */}
|
||
<button
|
||
type="button"
|
||
className={control({ state: hasFiles ? "warn" : null })}
|
||
data-tone={hasFiles ? "warn" : undefined}
|
||
aria-describedby="remove-note"
|
||
disabled={busy}
|
||
onClick={confirm}
|
||
>
|
||
remove and delete files
|
||
</button>
|
||
<button type="button" className={control()} ref={keep} disabled={busy} onClick={onCancel}>
|
||
keep
|
||
</button>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|