Files
arr/web/src/buckets.tsx
T
naps62-yolo 5a18facc4f
ci / web (push) Successful in 33s
e2e / e2e (push) Successful in 1m36s
ci / rust (push) Successful in 1m49s
ci / image (push) Successful in 2m36s
refactor(web): move the stylesheet into Tailwind utilities (#281)
2026-11-11 21:14:23 +00:00

476 lines
14 KiB
TypeScript

// 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 { CELLS, cx, ROW_RULE, score } from "./look";
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<void>;
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<WaiveOutcome>;
}
/**
* 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<T>({
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) => (
<Fragment key={keyOf(item)}>{render(item)}</Fragment>
))}
{rest > 0 && (
<li className="pt-3">
<Control
text={`show ${rest} more`}
quiet
glyph="chevronDown"
onClick={() => setAll(true)}
/>
</li>
)}
</>
);
}
const COLHEAD_LABEL = "font-readout text-2xs uppercase tracking-title text-ink-faint";
/** The column header the release cells line up under, from 46rem. */
function Colhead() {
return (
<div
className={cx(
"hidden @cells:grid [grid-template-columns:var(--control-h)_minmax(0,1fr)_var(--word-w)] gap-x-3 pt-2 pb-1",
ROW_RULE,
)}
aria-hidden="true"
>
<span className={COLHEAD_LABEL}>score</span>
<span className={cx("col-start-2 grid", CELLS)}>
<span className={COLHEAD_LABEL}>res</span>
<span className={COLHEAD_LABEL}>source</span>
<span className={cx(COLHEAD_LABEL, "text-right")}>size</span>
<span className={COLHEAD_LABEL}>hdr</span>
<span className={COLHEAD_LABEL}>audio</span>
<span className={cx(COLHEAD_LABEL, "text-right")}>seed</span>
</span>
</div>
);
}
function CollapsedBucket({
label,
releases,
bucket,
scoreStop,
actions,
}: {
label: string;
releases: MovieRelease[];
bucket: Bucket;
scoreStop: ScoreStopper;
actions: ReleaseActions;
}) {
const [open, setOpen] = useState(false);
return (
<Section hidden={releases.length === 0}>
<DeckHead as="h4" label={label} count={releases.length === 0 ? "" : releases.length}>
<Disclosure count={releases.length} open={open} onToggle={() => setOpen(!open)} />
</DeckHead>
<div hidden={!open}>
<Colhead />
<Rows>
<Paged
items={releases}
keyOf={(release) => release.id}
render={(release) => (
<ReleaseRow
release={release}
bucket={bucket}
scoreStop={scoreStop}
actions={actions}
/>
)}
/>
</Rows>
</div>
</Section>
);
}
/**
* 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 (
<div className="@container">
<Section hidden={state.kind === "none"}>
<DeckHead as="h4" label="eligible" count="" />
<Colhead />
<Rows>{state.kind === "skeleton" && <SkeletonRows count={4} />}</Rows>
</Section>
<Section hidden>
<DeckHead as="h4" label="below policy" count="" />
</Section>
<Section hidden>
<DeckHead as="h4" label="rejected" count="" />
</Section>
</div>
);
}
return <PaintedBuckets key={state.paint} releases={state.releases} actions={actions} />;
}
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 (
<div className="@container">
<Section>
<DeckHead as="h4" label="eligible" count={eligible.length} />
<Colhead />
<Rows>
{eligible.length === 0 ? (
<EmptyRow
text={
forceable > 0
? `none — every candidate is below policy or rejected; ${forceable} can be forced`
: "none — every candidate is below policy or rejected"
}
/>
) : (
<Paged
items={eligible}
keyOf={(release) => release.id}
render={(release) => (
<ReleaseRow
release={release}
bucket="eligible"
scoreStop={scoreStop}
actions={actions}
/>
)}
/>
)}
</Rows>
</Section>
<CollapsedBucket
label="below policy"
releases={waived}
bucket="waived"
scoreStop={scoreStop}
actions={actions}
/>
<CollapsedBucket
label="rejected"
releases={rejected}
bucket="rejected"
scoreStop={scoreStop}
actions={actions}
/>
</div>
);
}
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<GrabState>("idle");
const timer = useRef<number | undefined>(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) => (
<Chip
className={value === "—" ? "dim" : undefined}
data-cell=""
data-pri={secondary ? "2" : undefined}
aria-label={value === "—" ? `${label} unclaimed` : `${label} ${value}`}
>
{value}
</Chip>
);
// #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 (
<Row
kind="release"
lead={
// biome-ignore lint/a11y/useAriaPropsSupportedByRole: the score's name is read with its row, not as a control
<span
className={score(stop)}
data-score={stop === null ? undefined : String(stop)}
aria-label={`score ${formatScore(release.score)}`}
>
{formatScore(release.score)}
</span>
}
meta={
<>
{verdictText !== null && (
<Chip data-verdict={bucket} data-blacklist={blacklisted ?? undefined}>
{verdictText}
</Chip>
)}
{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={
<div className={cx("hidden @cells:grid", CELLS)} aria-hidden="true">
{(
[
[values.res, false],
[values.src, false],
[values.size, true],
[values.hdr, false],
[values.aud, false],
[values.seed, true],
] as const
).map(([value, numeric], index) => (
<span
// fixed columns: the position is the identity
// biome-ignore lint/suspicious/noArrayIndexKey: see above
key={index}
title={value}
className={cx(
"truncate font-readout text-xs tabular-nums",
numeric && "text-right",
value === "—" ? "text-ink-faint" : "text-ink-muted",
)}
>
{value}
</span>
))}
</div>
}
sub={<span>{release.name}</span>}
subExpands
afterSub={
blacklisted && (
<span
className={cx(
"readout text-xs",
blacklisted === "release"
? "[color:oklch(from_var(--signal-fault)_0.78_0.1_h)]"
: "text-ink-muted",
)}
data-blacklist={blacklisted}
>
{blacklistAdvice(release.blacklist_reason)}
</span>
)
}
actions={[
{
node: offersGrab ? (
<Control
text={grabText}
fixed
busy={grab === "busy"}
done={grab === "done"}
failed={grab === "failed"}
disabled={grab === "busy" || grab === "done"}
aria-label={
writesOverride
? `force grab — relaxes ${ruleLabel(release.rejected_rule)} for this title and grabs anyway`
: undefined
}
onClick={run}
/>
) : null,
width: "word",
},
]}
note={note}
/>
);
}