feat(facets): all-facets panel with under-filter counts (faceted plan Phase 7)

The 9 Batch A facets could not be SET from the web UI, the facets
endpoint had no in-app consumer, and the registry's libraryTypes hints
rendered nowhere. This phase closes all three.

- ONE UNION-ALL sqlc query (FacetCountsUnderFilters) computes all 14
  listable facets in a single round trip: each arm omits its OWN
  facet's filter block and carries every other registered facet's
  block (copied mechanically from SearchMediaItemsUnified) — classic
  faceted semantics: with filters active each facet counts under the
  others; with none, blocks are neutral and the counts are the v1
  global-per-library ones. One query serves both cases. The 14
  per-column Batch C queries are deleted.
- Endpoint: no facet param → the new all-facets shape
  ({facets: {param: [{value, display?, count}]}}), filters bound via
  the shared ParseMediaSearchParams, library_id optional (the
  All-Libraries view, family lens still enforced); facet=X keeps the
  pinned single-facet shape (now routed through the same query with
  neutral filters).
- SSR partial GET /media-items/facet-panel: templ renders the drawer's
  value lists — registry order, Batch D library-type scoping
  (FacetsForLibraryType: manga_type on manga, reading_direction on
  manga+comics, narrator on audiobooks; All view = union), prettified
  genre labels + language display names, active values marked. Family
  lens: out-of-set explicit library_id is 403.
- Drawer wiring: the panel fetches on drawer open and after every
  apply with the CURRENT filter query string; a value click toggles
  that facet's comma-joined filter (hidden input where the drawer has
  no field — same as deep links) and re-applies the form; chips and
  URL stay in sync. Values are server-rendered (rendering doctrine);
  bookshelf.ts owns only the click/fetch orchestration.
- prettifyGenre moves to internal/utils as the canonical
  implementation (services' display enrichment shares it; templ wraps
  it; the TS port in bookshelf.ts is unchanged).
- Drift guard extended: FacetCountsUnderFiltersParams must carry every
  registry facet's param, the bools as pgtype.Bool.

Tests: under-filter exactness (own-block omission, OR-within,
AND-across), family lens (hidden values absent; 403 on both shapes),
all-shape + pinned-shape coexistence, partial rendering with Batch D
scoping + active marking + 403. Full container suite green except the
documented pre-existing TestAdminCreateUser red.
This commit is contained in:
John O'Keefe committed 2026-10-20 08:24:36 -04:00
1 parent 6152137156
commit f52bb6f073
18 files changed
+3001 -911

No files matched your search

+95 -3
View File
@@ -94,6 +94,7 @@ Alpine.data("bookshelf", () => ({
// Standalone function references (don't access component state)
clearFilters,
initBookshelf,
loadFacetPanel,
// Inline method - opens the modal
showSaveFilterModal() {
@@ -658,6 +659,74 @@ function refreshFilterChips(): void {
);
}
// splitFacetValues is the client twin of services.SplitFacetValues: a
// comma-joined filter value is a multi-select (OR within the facet,
// AND across facets).
function splitFacetValues(wireValue: string): string[] {
return wireValue
.split(",")
.map((v) => v.trim())
.filter((v) => v !== "");
}
// loadFacetPanel fetches the drawer's facet-value partial with the
// CURRENT filter query string (faceted plan Phase 7): the values are
// server-rendered by templ (templates/facet_panel.templ), and their
// counts are computed under the active filters by the all-facets query
// — one round trip for every facet at once.
async function loadFacetPanel(): Promise<void> {
const target = document.getElementById("facet-panel");
const filterForm = document.getElementById(
"filter-form",
) as HTMLFormElement | null;
if (!target || !filterForm) return;
// Serialize the form's named fields (all inputs/selects — no
// multi-selects or file inputs here) — typed directly, since the
// project's tsconfig lib lacks DOM.Iterable (FormData.entries()).
const pairs: string[][] = [];
filterForm
.querySelectorAll<HTMLInputElement | HTMLSelectElement>("input, select")
.forEach((el) => {
if (el.name) pairs.push([el.name, el.value]);
});
const qs = new URLSearchParams(pairs).toString();
await window.htmx.ajax("GET", `/media-items/facet-panel?${qs}`, {
target: "#facet-panel",
swap: "innerHTML",
});
}
// toggleFacetValue applies one panel click: the value joins/leaves its
// facet's comma-joined filter. The facet's input may be a typed drawer
// field (set its value) or absent entirely — deep-link-only facets get
// a hidden input, exactly like applyURLFilters does, so chips and
// clearing keep working.
function toggleFacetValue(param: string, value: string): void {
const filterForm = document.getElementById(
"filter-form",
) as HTMLFormElement | null;
if (!filterForm) return;
let input = filterForm.querySelector(
`[name="${param}"]`,
) as HTMLInputElement | null;
if (!input) {
const hidden = document.createElement("input");
hidden.type = "hidden";
hidden.name = param;
filterForm.appendChild(hidden);
input = hidden;
}
const values = splitFacetValues(input.value);
const idx = values.indexOf(value);
if (idx >= 0) {
values.splice(idx, 1);
} else {
values.push(value);
}
input.value = values.join(",");
window.htmx.trigger(filterForm, "submit");
}
function initBookshelf(): void {
const filterForm = document.getElementById(
"filter-form",
@@ -666,9 +735,32 @@ function initBookshelf(): void {
// Active-filter chips refresh after every grid refetch — drawer
// applies, sort changes, and pagination all include the form's
// filters, so one bubbled htmx:afterRequest hook is the sync point.
filterForm.addEventListener("htmx:afterRequest", () =>
refreshFilterChips(),
);
filterForm.addEventListener("htmx:afterRequest", () => {
refreshFilterChips();
// Facet counts must track the applied filters: refetch the panel
// while the drawer is open (the panel's own htmx.ajax requests
// dispatch on body, so this form-scoped hook never loops).
const bookshelfElem = document.querySelector(
'[x-data*="bookshelf"]',
) as any;
const data = bookshelfElem?._x_dataStack?.[0];
if (data?.filtersOpen) {
void loadFacetPanel();
}
});
// Panel value clicks: one delegated listener for every section's
// buttons (the partial re-renders on each refetch).
const facetPanel = document.getElementById("facet-panel");
if (facetPanel) {
facetPanel.addEventListener("click", (ev) => {
const btn = (ev.target as HTMLElement).closest<HTMLElement>(
".facet-value-btn",
);
if (!btn || !btn.dataset.param || !btn.dataset.value) return;
toggleFacetValue(btn.dataset.param, btn.dataset.value);
});
}
// A2c §5.3: deep-linkable filters — /bookshelf?author_filter=Sapkowski
// pre-fills the form and applies it to the first grid load. Author