// 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(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 ( ); } export function LibraryView({ hidden }: { hidden: boolean }) { const nav = useNav(); const [shape, setShapeState] = useState(loadLibraryShape); const [data, setData] = useState(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: , })), ...moviesShown.map((movie) => ({ root_id: movie.root_id, wants: movieNeedsAttention(movie), key: `m${movie.id}`, node: , })), ], (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 ( ); } function ListSkeleton() { return ( ); }