// The §9.3 release deck: three buckets, eligible always open, below-policy // and rejected collapsed to counts. Shared by the movie page and the tv deck. import { Fragment, type ReactNode, useEffect, useRef, useState } from "react"; import { blacklistAdvice, blacklistClass, blacklistReasonLabel, bucketOf, formatAudio, formatHdr, formatResolution, formatScore, formatSeeders, formatSize, formatSource, type MovieRelease, overridable, ruleLabel, type WaiveOutcome, } from "./releases"; import { Chip, Control, DeckHead, Disclosure, EmptyRow, Row, Rows, Section, SkeletonRows, useNote, } from "./ui"; export type Bucket = "eligible" | "waived" | "rejected"; export interface ReleaseActions { reload: () => Promise; notify: (text: string, tone?: "fault") => void; /** One click on grab — a plain grab, or waive-then-grab where the rule * that failed has an override (§9.3). */ grab: (release: MovieRelease, bucket: Bucket) => Promise; } /** * What the deck shows: placeholders while the first read is out, nothing, * or a set of verdicts. `paint` changes on every repaint, so a repaint * collapses the buckets and resets the rows the way a fresh list does. */ export type DeckState = | { kind: "skeleton" } | { kind: "none" } | { kind: "releases"; releases: MovieRelease[]; paint: number }; /** Stops in the score heat scale — keep in step with the --score-* tokens. */ const SCORE_STOPS = 6; type ScoreStopper = (score: number | null) => number | null; /** * Score colour is relative to one list (issue 111): highest green, lowest * red, seven interpolated stops between. */ function scoreStopper(releases: MovieRelease[]): ScoreStopper { let min = Number.POSITIVE_INFINITY; let max = Number.NEGATIVE_INFINITY; for (const release of releases) { if (release.score !== null) { if (release.score < min) { min = release.score; } if (release.score > max) { max = release.score; } } } return (score: number | null): number | null => { if (score === null || min > max) { return null; } // all-equal scores are jointly the best the list offers return max === min ? SCORE_STOPS : Math.round(((score - min) / (max - min)) * SCORE_STOPS); }; } /** Long lists are paged: the first rows render, the rest wait behind one control. */ const PAGE_ROWS = 25; function Paged({ items, keyOf, render, }: { items: T[]; keyOf: (item: T) => string | number; render: (item: T) => ReactNode; }) { const [all, setAll] = useState(false); const shown = all ? items : items.slice(0, PAGE_ROWS); const rest = items.length - shown.length; return ( <> {shown.map((item) => ( {render(item)} ))} {rest > 0 && (
  • setAll(true)} />
  • )} ); } /** The column header the release cells line up under, from 46rem. */ function Colhead() { return ( ); } function CollapsedBucket({ label, releases, bucket, scoreStop, actions, }: { label: string; releases: MovieRelease[]; bucket: Bucket; scoreStop: ScoreStopper; actions: ReleaseActions; }) { const [open, setOpen] = useState(false); return ( ); } /** * Splits classified releases into the three buckets and paints them. The * API pre-sorts by bucket then score; splitting preserves order. */ export function Buckets({ state, actions }: { state: DeckState; actions: ReleaseActions }) { if (state.kind !== "releases") { return (
    ); } return ; } function PaintedBuckets({ releases, actions, }: { releases: MovieRelease[]; actions: ReleaseActions; }) { const scoreStop = scoreStopper(releases); const eligible = releases.filter((release) => bucketOf(release) === "eligible"); const waived = releases.filter((release) => bucketOf(release) === "waived"); const rejected = releases.filter((release) => bucketOf(release) === "rejected"); // §9.3: over-strict filters must be visible, not silently absent — and // where a rule can be waived, the count says so rather than leaving the // way out folded inside a collapsed bucket. Only a rejected row needs // forcing; a row below policy is offered already. const forceable = rejected.filter(overridable).length; return (
    {eligible.length === 0 ? ( 0 ? `none — every candidate is below policy or rejected; ${forceable} can be forced` : "none — every candidate is below policy or rejected" } /> ) : ( release.id} render={(release) => ( )} /> )}
    ); } type GrabState = "idle" | "busy" | "done" | "failed"; /** How long "sent" holds before the control is ready again. */ const SENT_MS = 4000; /** * One classified release: the score leads, the deciding attributes follow, * the name is secondary and opens to its full string on a tap (§9.3). Grab is * the one word control a row may carry, at a fixed width, and its outcome * changes its label rather than adding a line. */ function ReleaseRow({ release, bucket, scoreStop, actions, }: { release: MovieRelease; bucket: Bucket; scoreStop: ScoreStopper; actions: ReleaseActions; }) { const [note, say] = useNote(); const [grab, setGrab] = useState("idle"); const timer = useRef(undefined); useEffect(() => () => window.clearTimeout(timer.current), []); const stop = scoreStop(release.score); const values = { res: formatResolution(release.parsed), src: formatSource(release.parsed), size: formatSize(release.size), hdr: formatHdr(release.parsed), aud: formatAudio(release.parsed), seed: formatSeeders(release.seeders), }; const attribute = (label: string, value: string, secondary: boolean) => ( {value} ); // #227: a blacklisted release was grabbed, downloaded and condemned at // import. Which of the two things happened decides what the operator does // next, and `blacklisted` alone reads the same for both. const blacklisted = bucket === "rejected" && release.rejected_rule === "blacklisted" ? blacklistClass(release.blacklist_reason) : null; // §9.3: every row that is not eligible names the rule behind it const verdictText = bucket === "eligible" ? null : bucket === "waived" ? release.rejected_rule ? `below policy · ${ruleLabel(release.rejected_rule)}` : "below policy" : blacklisted ? `blacklisted · ${blacklistReasonLabel(release.blacklist_reason)}` : release.rejected_rule ? `rejected · ${ruleLabel(release.rejected_rule)}` : "rejected"; // A rejected row is normally inert, but §9.3's one click is exactly for // the rule the operator disagrees with: where an override exists, the row // offers it and the grab that follows stays a waiver. const writesOverride = bucket !== "eligible" && overridable(release); const offersGrab = bucket !== "rejected" || writesOverride; const label = writesOverride ? "force" : "grab"; const grabText = grab === "busy" ? "sending" : grab === "failed" ? "retry" : grab === "done" ? "sent" : label; const run = () => { setGrab("busy"); say(null); void actions.grab(release, bucket).then((outcome) => { const written = outcome.overrideWritten ? "override written · " : ""; if (outcome.kind === "error") { setGrab("failed"); say(`${written}grab failed — ${outcome.detail}`, "fault"); } else { setGrab("done"); timer.current = window.setTimeout(() => setGrab("idle"), SENT_MS); } if (outcome.overrideWritten) { // the override changed classification; reread the verdicts and // keep the outcome visible past the re-render const text = outcome.kind === "error" ? `${written}grab failed — ${outcome.detail}` : `${written}grab sent`; void actions.reload().then(() => { actions.notify(text, outcome.kind === "error" ? "fault" : undefined); }); } }); }; return ( {formatScore(release.score)} } meta={ <> {verdictText !== null && ( {verdictText} )} {attribute("resolution", values.res, false)} {attribute("size", values.size, false)} {attribute("source", values.src, true)} {attribute("seeders", values.seed, true)} {attribute("hdr", values.hdr, true)} {attribute("audio", values.aud, true)} } beforeSub={ } sub={{release.name}} subExpands afterSub={ blacklisted && ( {blacklistAdvice(release.blacklist_reason)} ) } actions={[ { node: offersGrab ? ( ) : null, width: "word", }, ]} note={note} /> ); }