feat(search): rank titles and filter by kind (#274)
This commit was merged in pull request #274.
This commit is contained in:
+67
-17
@@ -558,6 +558,7 @@ const DEBOUNCE_MS = 250;
|
||||
interface DeckRefs {
|
||||
deck: HTMLElement;
|
||||
status: HTMLElement;
|
||||
filter: HTMLElement;
|
||||
groups: {
|
||||
library: { section: HTMLElement; count: HTMLElement; rows: HTMLUListElement };
|
||||
tmdb: { section: HTMLElement; count: HTMLElement; rows: HTMLUListElement };
|
||||
@@ -566,6 +567,9 @@ interface DeckRefs {
|
||||
manualIntake: HTMLElement;
|
||||
}
|
||||
|
||||
/** Which kind the deck is showing. `all` is both, and the default. */
|
||||
type KindFilter = "all" | "movie" | "series";
|
||||
|
||||
interface SearchView {
|
||||
/** Restores the library with the search box empty — the "/" route. */
|
||||
showIdle: () => void;
|
||||
@@ -584,6 +588,7 @@ function searchMain(
|
||||
const refs: DeckRefs = {
|
||||
deck: must<HTMLElement>("#deck"),
|
||||
status: must<HTMLElement>("#deck-status"),
|
||||
filter: must<HTMLElement>("#deck-filter"),
|
||||
groups: {
|
||||
library: {
|
||||
section: must<HTMLElement>("#group-library"),
|
||||
@@ -663,10 +668,26 @@ function searchMain(
|
||||
group.section.hidden = true;
|
||||
group.rows.replaceChildren();
|
||||
}
|
||||
refs.filter.hidden = true;
|
||||
refs.manualSection.hidden = true;
|
||||
refs.manualIntake.replaceChildren();
|
||||
}
|
||||
|
||||
const filterButtons = [...refs.filter.querySelectorAll<HTMLButtonElement>("[data-kind]")];
|
||||
let kind: KindFilter = "all";
|
||||
// The last answer, kept so flipping the filter re-paints what is already
|
||||
// in hand instead of costing another TMDB round trip.
|
||||
let shown: SearchResponse | null = null;
|
||||
|
||||
for (const button of filterButtons) {
|
||||
button.addEventListener("click", () => {
|
||||
kind = (button.dataset.kind ?? "all") as KindFilter;
|
||||
if (shown) {
|
||||
paint(shown);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function run(query: string) {
|
||||
controller?.abort();
|
||||
controller = new AbortController();
|
||||
@@ -685,6 +706,11 @@ function searchMain(
|
||||
}
|
||||
|
||||
function render(response: SearchResponse) {
|
||||
shown = response;
|
||||
paint(response);
|
||||
}
|
||||
|
||||
function paint(response: SearchResponse) {
|
||||
clearGroups();
|
||||
if (
|
||||
response.manual !== null &&
|
||||
@@ -696,14 +722,9 @@ function searchMain(
|
||||
return;
|
||||
}
|
||||
|
||||
// §9.2: in-library hits read as whatever they are — a movie or a series.
|
||||
const libraryMovies = response.library.filter(
|
||||
(hit): hit is LibraryMovie => hit.kind === "movie",
|
||||
);
|
||||
const librarySeries = response.library.filter(
|
||||
(hit): hit is LibrarySeriesHit => hit.kind === "series",
|
||||
);
|
||||
const inLibrary = new Set([...libraryMovies, ...librarySeries].map((title) => title.tmdb_id));
|
||||
const inLibrary = new Set(response.library.map((title) => title.tmdb_id));
|
||||
// a TMDB hit already in the library group below never repeats there
|
||||
const tmdb = response.tmdb.filter((hit) => !inLibrary.has(hit.tmdb_id));
|
||||
// Issue #168: an unreachable TMDB degrades only its own group — the
|
||||
// operator sees the library half plus why TMDB is missing, and can tell
|
||||
// that apart from a clean "nothing found".
|
||||
@@ -714,24 +735,39 @@ function searchMain(
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const library = response.library.filter((hit) => kind === "all" || hit.kind === kind);
|
||||
const tmdbHits = tmdb.filter((hit) => kind === "all" || hit.kind === kind);
|
||||
paintFilter([...response.library, ...tmdb]);
|
||||
|
||||
if (tmdbDown) {
|
||||
setStatus("tmdb unavailable — showing library matches only", "fault");
|
||||
} else if (library.length === 0 && tmdbHits.length === 0) {
|
||||
// Every match is of the other kind. Name the count it is holding back,
|
||||
// or the filter reads as a search that found nothing.
|
||||
const held = response.library.length + tmdb.length;
|
||||
const other =
|
||||
kind === "movie"
|
||||
? `${held} series ${held === 1 ? "does" : "do"}`
|
||||
: `${held} ${held === 1 ? "movie does" : "movies do"}`;
|
||||
setStatus(`no ${kind === "movie" ? "movies" : "series"} match — ${other}`);
|
||||
} else {
|
||||
setStatus(null);
|
||||
}
|
||||
|
||||
if (response.library.length > 0) {
|
||||
if (library.length > 0) {
|
||||
refs.groups.library.section.hidden = false;
|
||||
refs.groups.library.count.textContent = String(response.library.length);
|
||||
for (const movie of libraryMovies) {
|
||||
refs.groups.library.rows.append(libraryRow(movie, roots, openMovie));
|
||||
}
|
||||
for (const series of librarySeries) {
|
||||
refs.groups.library.rows.append(librarySeriesRow(series, roots, openSeries));
|
||||
refs.groups.library.count.textContent = String(library.length);
|
||||
// §9.2: in-library hits read as whatever they are — a movie or a
|
||||
// series — and hold the order the API ranked them in.
|
||||
for (const hit of library) {
|
||||
refs.groups.library.rows.append(
|
||||
hit.kind === "movie"
|
||||
? libraryRow(hit, roots, openMovie)
|
||||
: librarySeriesRow(hit, roots, openSeries),
|
||||
);
|
||||
}
|
||||
}
|
||||
// a TMDB hit already in the library group above never repeats here
|
||||
const tmdbHits = response.tmdb.filter((hit) => !inLibrary.has(hit.tmdb_id));
|
||||
if (tmdbHits.length > 0) {
|
||||
refs.groups.tmdb.section.hidden = false;
|
||||
refs.groups.tmdb.count.textContent = String(tmdbHits.length);
|
||||
@@ -741,6 +777,20 @@ function searchMain(
|
||||
}
|
||||
}
|
||||
|
||||
/** The filter, with what each chip would show — including the zero. */
|
||||
function paintFilter(hits: { kind: "movie" | "series" }[]) {
|
||||
refs.filter.hidden = false;
|
||||
for (const button of filterButtons) {
|
||||
const buttonKind = button.dataset.kind ?? "all";
|
||||
const count = hits.filter((hit) => buttonKind === "all" || hit.kind === buttonKind).length;
|
||||
button.setAttribute("aria-pressed", String(buttonKind === kind));
|
||||
const slot = button.querySelector<HTMLElement>('[data-role="count"]');
|
||||
if (slot) {
|
||||
slot.textContent = String(count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
input.addEventListener("input", () => {
|
||||
window.clearTimeout(timer);
|
||||
const query = input.value.trim();
|
||||
|
||||
@@ -622,6 +622,29 @@ body {
|
||||
gap: var(--space-3);
|
||||
}
|
||||
|
||||
/* the kind filter narrows both groups at once, so it sits above them rather
|
||||
than inside either head. Its counts are the whole point — they say what
|
||||
the filter is holding back, so an empty group never reads as no match. */
|
||||
.deck-filter {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: var(--space-2);
|
||||
margin: 0 0 var(--space-6);
|
||||
}
|
||||
|
||||
.control-filter {
|
||||
gap: var(--space-2);
|
||||
}
|
||||
|
||||
.filter-count {
|
||||
color: var(--ink-faint);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.control-filter[aria-pressed="true"] .filter-count {
|
||||
color: var(--ink-muted);
|
||||
}
|
||||
|
||||
/* a sweep in flight borrows the rail's probing-lamp idiom, inline */
|
||||
.deck-status .lamp {
|
||||
display: inline-block;
|
||||
|
||||
Reference in New Issue
Block a user