Files
arr/web/src/views/Library.tsx
T
Miguel PalhasandClaude Opus 5.5 db22e4a1d2
ci / web (pull_request) Successful in 30s
ci / rust (pull_request) Successful in 1m28s
ci / image (pull_request) Skipped
refactor(web): page skeleton as Tailwind utilities
Decks, page bars, sections, heads, status slots and group notes move
out of components.css into Deck, PageBar, Section, Rows and friends.
The theme gains the geometry tokens and the container widths.

Refs #280

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-11 17:16:47 +00:00

244 lines
7.9 KiB
TypeScript

// The library (§4.2, issue #32; §9.7 makes it the homepage): series and
// movies as a poster grid by root (§9.6, #151), or as two lists.
import { Fragment, type ReactNode, useEffect, useRef, useState } from "react";
import { useNav } from "../context";
import { Icon } from "../icons";
import {
fetchLibrary,
type LibrarySeries,
movieNeedsAttention,
seriesNeedsAttention,
} from "../library";
import { MovieCard, MovieRow, SeriesCard, SeriesRow } from "../rows";
import { allRoots, type LibraryMovie, type Root } from "../search";
import { Deck, DeckHead, PageBar, Rows, Section, SkeletonRows, useStatus } from "../ui";
type LibraryShape = "grid" | "list";
const LIBRARY_SHAPE_KEY = "arr.libraryView";
/**
* The grid is default and the choice is per-person — no server state. A
* corrupt or foreign value falls back to the grid rather than breaking open.
*/
function loadLibraryShape(): LibraryShape {
try {
return window.localStorage.getItem(LIBRARY_SHAPE_KEY) === "list" ? "list" : "grid";
} catch {
return "grid";
}
}
/**
* Stable partition: the items that still want something keep their order and
* come first, the satisfied ones keep theirs and follow.
*/
function attentionFirst<T>(items: T[], wants: (item: T) => boolean): T[] {
return [...items.filter(wants), ...items.filter((item) => !wants(item))];
}
interface LibraryData {
series: LibrarySeries[];
movies: LibraryMovie[];
roots: Root[];
}
/** The last answer, so a return to the library paints at once and then refreshes. */
let cached: LibraryData | null = null;
function GridSkeleton() {
return (
<Rows grid hidden>
{Array.from({ length: 6 }, (_, index) => (
// biome-ignore lint/suspicious/noArrayIndexKey: placeholders have no identity
<li key={index}>
<span className="card card-skel">
<span className="card-art" />
<span className="card-body">
<span className="skel skel-line" />
<span className="skel skel-line skel-short" />
</span>
</span>
</li>
))}
</Rows>
);
}
export function LibraryView({ hidden }: { hidden: boolean }) {
const nav = useNav();
const [shape, setShapeState] = useState(loadLibraryShape);
const [data, setData] = useState<LibraryData | null>(cached);
const [failed, setFailed] = useState(false);
const [status, setStatus] = useStatus();
// guards a stale fetch from painting over a newer view
const sequence = useRef(0);
useEffect(() => {
sequence.current += 1;
const ticket = sequence.current;
setStatus("reading library…");
void Promise.all([fetchLibrary(), allRoots().catch(() => cached?.roots ?? [])]).then(
([outcome, roots]) => {
if (ticket !== sequence.current) {
return;
}
if (outcome.kind === "error") {
setFailed(true);
setStatus(`library unavailable — ${outcome.detail}`, "fault");
return;
}
cached = { series: outcome.series, movies: outcome.movies, roots };
setFailed(false);
setData(cached);
const total = outcome.series.length + outcome.movies.length;
setStatus(total === 0 ? "library is empty — the search box above adds titles" : null);
},
);
return () => {
sequence.current += 1;
};
}, [setStatus]);
const setShape = (next: LibraryShape) => {
setShapeState(next);
try {
window.localStorage.setItem(LIBRARY_SHAPE_KEY, next);
} catch {
// private mode or full storage: the choice just does not persist
}
};
const shown = failed ? null : data;
const total = shown === null ? 0 : shown.series.length + shown.movies.length;
const roots = shown?.roots ?? [];
// §4.2: nothing is hidden. Satisfied titles sort last so what still
// wants something is what the eye lands on first.
const seriesShown = shown === null ? [] : attentionFirst(shown.series, seriesNeedsAttention);
const moviesShown = shown === null ? [] : attentionFirst(shown.movies, movieNeedsAttention);
let grid: { id: number; label: string; items: { key: string; node: ReactNode }[] }[] = [];
if (shown !== null && shape === "grid") {
// series and movies share a root section, so the two lists interleave
// before the cards are built — otherwise a satisfied series would sit
// above a movie that still wants a file.
const entries = attentionFirst(
[
...seriesShown.map((series) => ({
root_id: series.root_id,
wants: seriesNeedsAttention(series),
key: `s${series.id}`,
node: <SeriesCard series={series} open={nav.openSeries} />,
})),
...moviesShown.map((movie) => ({
root_id: movie.root_id,
wants: movieNeedsAttention(movie),
key: `m${movie.id}`,
node: <MovieCard movie={movie} open={nav.openMovie} />,
})),
],
(entry) => entry.wants,
);
const sections = [
...roots.map((root) => ({
id: root.id,
label: `${root.kind === "tv" ? "series" : "movies"} · ${root.audience}`,
})),
...[
...new Set(
entries
.filter((entry) => !roots.some((root) => root.id === entry.root_id))
.map((entry) => entry.root_id),
),
].map((id) => ({ id, label: `root ${id}` })),
];
grid = sections
.map((section) => ({
...section,
items: entries.filter((entry) => entry.root_id === section.id),
}))
.filter((section) => section.items.length > 0);
}
return (
<Deck label="library" hidden={hidden}>
<PageBar
title="library"
count={total === 0 ? "" : `${total} ${total === 1 ? "title" : "titles"}`}
status={status}
>
<fieldset className="segmented" aria-label="layout">
<button
type="button"
className="seg seg-icon"
aria-pressed={shape === "grid"}
aria-label="poster grid"
onClick={() => setShape("grid")}
>
<Icon name="grid" />
</button>
<button
type="button"
className="seg seg-icon"
aria-pressed={shape === "list"}
aria-label="list"
onClick={() => setShape("list")}
>
<Icon name="list" />
</button>
</fieldset>
</PageBar>
<div hidden={shown !== null && (shape === "list" || grid.length === 0)}>
{data === null && !failed && (shape === "grid" ? <GridSkeleton /> : <ListSkeleton />)}
{shown !== null &&
grid.map((section) => (
<Section key={section.id}>
<DeckHead label={section.label} count={section.items.length} />
<Rows grid>
{section.items.map((item) => (
<Fragment key={item.key}>{item.node}</Fragment>
))}
</Rows>
</Section>
))}
</div>
<Section
hidden={shown === null || shape === "grid" || seriesShown.length === 0}
labelledBy="label-lib-series"
>
<DeckHead label="series" labelId="label-lib-series" count={seriesShown.length} />
<Rows>
{shape === "list" &&
seriesShown.map((series) => (
<SeriesRow key={series.id} series={series} roots={roots} open={nav.openSeries} />
))}
</Rows>
</Section>
<Section
hidden={shown === null || shape === "grid" || moviesShown.length === 0}
labelledBy="label-lib-movies"
>
<DeckHead label="movies" labelId="label-lib-movies" count={moviesShown.length} />
<Rows>
{shape === "list" &&
moviesShown.map((movie) => (
<MovieRow key={movie.id} movie={movie} roots={roots} open={nav.openMovie} />
))}
</Rows>
</Section>
</Deck>
);
}
function ListSkeleton() {
return (
<Rows>
<SkeletonRows count={6} />
</Rows>
);
}