238 lines
7.3 KiB
TypeScript
238 lines
7.3 KiB
TypeScript
// The chips and row parts several views share: media state, waivers, live
|
|
// downloads, counts, ratings and the row poster.
|
|
|
|
import { useState } from "react";
|
|
import { useLive } from "./context";
|
|
import { downloadChipDetail, downloadChipLabel, findDownload } from "./downloads";
|
|
import { Icon, Star } from "./icons";
|
|
import { waiverLabel } from "./library";
|
|
import { CHIP_ICON } from "./look";
|
|
|
|
/** A row's lead thumbnail at 2:3; a missing one keeps the same box. */
|
|
const POSTER =
|
|
"block w-poster aspect-2/3 flex-none object-cover bg-panel-raised border border-line rounded-chip";
|
|
|
|
import { formatRating, formatVoteCount, tmdbImage } from "./movie";
|
|
import { Chip, type Say } from "./ui";
|
|
|
|
/**
|
|
* What the operator reads for the stored `blocked` flag. §6.3 gives it one
|
|
* effect — it stops the targeted-search lane and leaves RSS matching on —
|
|
* and "blocked" reads as "block this title outright", which is the wrong
|
|
* promise. The flag, the column and the API keep their name; only the words
|
|
* on screen say what it does. [`BLOCKED_EFFECT`] carries the RSS half.
|
|
*/
|
|
export const NO_TARGETED_SEARCH = "no targeted search";
|
|
|
|
/** The half a three-word label cannot hold, for titles and accessible names. */
|
|
export const BLOCKED_EFFECT = "arr stops searching for this title; RSS still matches it";
|
|
|
|
export function two(value: number): string {
|
|
return value < 10 ? `0${value}` : String(value);
|
|
}
|
|
|
|
export function seasonTag(seasonNumber: number): string {
|
|
return `S${two(seasonNumber)}`;
|
|
}
|
|
|
|
export function episodeTag(seasonNumber: number, episodeNumber: number): string {
|
|
return `${seasonTag(seasonNumber)}E${two(episodeNumber)}`;
|
|
}
|
|
|
|
/** A row's title: the name, then the year in the readout face. */
|
|
export function TitleText({ title, year }: { title: string; year: number | null }) {
|
|
return (
|
|
<span>
|
|
{title}
|
|
{year !== null && (
|
|
<span className="ml-2 font-readout text-xs font-normal text-ink-faint">{year}</span>
|
|
)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* §4.2 state ramp on a media-state chip. `available` is a drawn check glyph —
|
|
* a shape, not only a hue, so it still reads greyscale — with the word kept
|
|
* as the chip's aria-label; every other state keeps the word on the chip.
|
|
*/
|
|
export function MediaStateChip({ state, wanted }: { state: string; wanted: boolean }) {
|
|
if (state === "available") {
|
|
// a bare span's aria-label is not reliably exposed; role="img" is what
|
|
// makes the word reachable once the glyph replaced it
|
|
return (
|
|
<Chip role="img" aria-label={state} data-movie-state={state} data-wanted={String(wanted)}>
|
|
<Icon name="check" className={CHIP_ICON} />
|
|
</Chip>
|
|
);
|
|
}
|
|
return (
|
|
<Chip data-movie-state={state} data-wanted={String(wanted)}>
|
|
{state}
|
|
</Chip>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* §5.7 honesty: a waived import is never presented as a clean match. The
|
|
* rule that was relaxed is a sentence, not a word, so the chip draws the
|
|
* warning glyph (§4.2's drawn-glyph exemption) and the sentence is a tap
|
|
* away in the row's status slot — never only in a tooltip.
|
|
*/
|
|
export function WaiverChip({ waiver, say }: { waiver: unknown; say?: Say }) {
|
|
const label = waiverLabel(waiver);
|
|
if (label === null) {
|
|
return null;
|
|
}
|
|
return (
|
|
<Chip
|
|
glyph
|
|
data-verdict="waived"
|
|
aria-label={label}
|
|
role={say ? undefined : "img"}
|
|
explain={say ? { text: `imported below policy — ${label}`, say, tone: "warn" } : undefined}
|
|
>
|
|
<Icon name="warning" className={CHIP_ICON} />
|
|
</Chip>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* §9.8: live download state on the row that owns the item. Absent unless
|
|
* arr itself grabbed this exact target — a season pack shows on the season
|
|
* row only, because the API only ever returns a row for the
|
|
* `target_kind`/`target_id` actually grabbed. Fixed width, and it sits last
|
|
* on its line, so the poll adding or dropping it never moves another chip.
|
|
* Its detail (rate, ETA) is a tap away rather than a hover away.
|
|
*/
|
|
export function DownloadChip({
|
|
targetKind,
|
|
targetId,
|
|
say,
|
|
}: {
|
|
targetKind: string;
|
|
targetId: number;
|
|
say?: Say;
|
|
}) {
|
|
const { downloads } = useLive();
|
|
const download = findDownload(downloads, targetKind, targetId);
|
|
return (
|
|
<span className="contents" data-dl-kind={targetKind} data-dl-id={targetId}>
|
|
{download !== null && (
|
|
<Chip
|
|
download
|
|
data-dl-phase={download.phase}
|
|
aria-label={`download ${downloadChipDetail(download)}`}
|
|
explain={say ? { text: `download ${downloadChipDetail(download)}`, say } : undefined}
|
|
>
|
|
{downloadChipLabel(download)}
|
|
</Chip>
|
|
)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
/** The ramp read off N/M itself: green when full, amber while a gap remains. */
|
|
function countsTone(available: number, total: number): "complete" | "partial" | undefined {
|
|
if (total === 0) {
|
|
return undefined;
|
|
}
|
|
return available >= total ? "complete" : "partial";
|
|
}
|
|
|
|
export function CountsChip({
|
|
label,
|
|
available,
|
|
total,
|
|
ariaLabel,
|
|
glyph = false,
|
|
}: {
|
|
label: string;
|
|
available: number;
|
|
total: number;
|
|
ariaLabel: string;
|
|
/** Season rows lead the count with the disk glyph. */
|
|
glyph?: boolean;
|
|
}) {
|
|
return (
|
|
<Chip data-counts={countsTone(available, total)} aria-label={ariaLabel}>
|
|
{glyph && <Icon name="disk" className={CHIP_ICON} />}
|
|
{label}
|
|
</Chip>
|
|
);
|
|
}
|
|
|
|
/** Episode counts for a title, hidden rather than shown as `0/0`. */
|
|
export function EpisodeCountsChip({ available, total }: { available: number; total: number }) {
|
|
if (total === 0) {
|
|
return null;
|
|
}
|
|
return (
|
|
<CountsChip
|
|
label={`${available}/${total} eps`}
|
|
available={available}
|
|
total={total}
|
|
ariaLabel={`${available} of ${total} episodes on disk`}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* TMDB's rating as a chip (#148). No votes means no chip — never a 0.0.
|
|
*/
|
|
export function RatingChip({
|
|
voteAverage,
|
|
voteCount,
|
|
secondary = false,
|
|
}: {
|
|
voteAverage: number | null;
|
|
voteCount?: number;
|
|
secondary?: boolean;
|
|
}) {
|
|
if (voteAverage === null) {
|
|
return null;
|
|
}
|
|
const votes = voteCount === undefined ? "" : ` from ${formatVoteCount(voteCount)} votes`;
|
|
return (
|
|
<Chip
|
|
aria-label={`rated ${formatRating(voteAverage)}${votes}`}
|
|
data-pri={secondary ? "2" : undefined}
|
|
>
|
|
<Star />
|
|
{formatRating(voteAverage)}
|
|
</Chip>
|
|
);
|
|
}
|
|
|
|
/** A search row's poster at w92, or a same-size blank so the edge never jitters. */
|
|
export function RowPoster({ posterPath, title }: { posterPath: string | null; title: string }) {
|
|
const [broken, setBroken] = useState(false);
|
|
if (posterPath === null || broken) {
|
|
return <span className={POSTER} aria-hidden="true" />;
|
|
}
|
|
return (
|
|
<img
|
|
className={POSTER}
|
|
loading="lazy"
|
|
src={tmdbImage(posterPath, "w92") ?? ""}
|
|
alt={`${title} poster`}
|
|
// hotlinked art can 404; fall back to the blank rather than alt-text spill
|
|
onError={() => setBroken(true)}
|
|
/>
|
|
);
|
|
}
|
|
|
|
/** The amber lamp that leads every attention-queue row. */
|
|
export function AttentionLamp() {
|
|
return (
|
|
<span className="flex w-4 justify-center">
|
|
<span className="lamp" data-state="warn" aria-hidden="true" />
|
|
</span>
|
|
);
|
|
}
|
|
|
|
/** The audience of a root, or its id when the roots list does not carry it. */
|
|
export function audienceOf(roots: { id: number; audience: string }[], rootId: number): string {
|
|
return roots.find((root) => root.id === rootId)?.audience ?? `root ${rootId}`;
|
|
}
|