From c74f749b76cb7750c7a5dada26fd18caad215914 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Wed, 11 Nov 2026 08:27:08 +0000 Subject: [PATCH 1/2] feat(site): count each filter option against the other filters An option's count was its total over every record, so with several filters set it promised rows the listing did not hold. It now shows how many records the option adds given the other filters, and how many hold it in all when that differs. --- site/client/app.tsx | 50 ++++++++++++++++++++------------- site/e2e/site.browser.ts | 21 ++++++++++++++ site/listing-query.ts | 22 +++++++++++++++ site/listing-query.unit.test.ts | 24 +++++++++++++++- 4 files changed, 96 insertions(+), 21 deletions(-) diff --git a/site/client/app.tsx b/site/client/app.tsx index c108d7f6..de514d1a 100644 --- a/site/client/app.tsx +++ b/site/client/app.tsx @@ -11,7 +11,7 @@ import { type ColumnDef, createSortedRowModel, rowSortingFeature, tableFeatures, import { entitiesText, entityTotal, sizeText, type Dimensions } from "../dimensions"; import { listingColumnWidths } from "../listing-columns"; import { matchingGroups, orderGroups, type GroupLink, type GroupOrder } from "../group-index"; -import { EMPTY_LISTING_QUERY, FACETS, initialListingQuery, listingLocation, mapFacets, matchesFacets, parseListingQuery, type Facet, type FacetValues, type ListingQuery } from "../listing-query"; +import { EMPTY_LISTING_QUERY, FACETS, facetValueCounts, initialListingQuery, listingLocation, mapFacets, matchesFacets, parseListingQuery, type Facet, type FacetValues, type ListingQuery } from "../listing-query"; import { siteRoot } from "./site-root"; import "../styles.css"; @@ -92,26 +92,26 @@ const FACET_CONTROLS: Readonly(chosen.map((value) => [value, 0])); - for (const record of records) { - for (const value of new Set(heldBy(record)[facet])) { - counts.set(value, (counts.get(value) ?? 0) + 1); - } - } - const byLabel = [...counts].sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)); - +function facetOptions(totals: ReadonlyMap, relative: ReadonlyMap, facet: Facet, chosen: readonly string[]): { value: string; label: string }[] { + const values = new Map([...chosen.map((value): [string, number] => [value, 0]), ...totals]); + const byLabel = [...values].sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)); const ordered = FACET_CONTROLS[facet].byCount ? byLabel.sort(([, left], [, right]) => right - left) : byLabel; const picked = new Set(chosen); + const label = (value: string, total: number): string => { + const shown = relative.get(value) ?? 0; + + return shown === total ? `${value} (${String(total)})` : `${value} (${String(shown)} of ${String(total)})`; + }; // The chosen values lead, in the attribute's own order, so they sit where a finger or the keyboard lands first and a click on one clears it. - return [...ordered.filter(([value]) => picked.has(value)), ...ordered.filter(([value]) => !picked.has(value))].map(([value, count]) => ({ value, label: `${value} (${String(count)})` })); + return [...ordered.filter(([value]) => picked.has(value)), ...ordered.filter(([value]) => !picked.has(value))].map(([value, total]) => ({ value, label: label(value, total) })); } /** Where a listing sits: the path back to the site root, the page's own filter as a query string (absent on the home listing), and the page's and the home listing's paths. */ @@ -165,15 +165,12 @@ function Listing({ records, place }: { readonly records: readonly SearchRecord[] }; }, [place]); - const options = useMemo(() => mapFacets((facet) => facetOptions(records, facet, query.facets[facet])), [records, query.facets]); - const filtered = useMemo(() => { + // What the free text and the entity bounds admit; the attributes narrow this further, and each attribute's options are counted against the other attributes' choices over this set. + const scoped = useMemo(() => { const needle = query.text.trim().toLowerCase(); return records.filter((record) => { - if (!matchesFacets(heldBy(record), query.facets)) { - return false; - } if (!within(entityTotal(record), query.minEntities, query.maxEntities)) { return false; } @@ -184,7 +181,20 @@ function Listing({ records, place }: { readonly records: readonly SearchRecord[] return haystack.some((field) => field.toLowerCase().includes(needle)); }); - }, [records, query]); + }, [records, query.text, query.minEntities, query.maxEntities]); + const filtered = useMemo(() => scoped.filter((record) => matchesFacets(heldBy(record), query.facets)), [scoped, query.facets]); + + const totals = useMemo(() => { + const held = records.map(heldBy); + + return mapFacets((facet) => facetValueCounts(held, facet, EMPTY_LISTING_QUERY.facets)); + }, [records]); + const options = useMemo(() => { + const held = scoped.map(heldBy); + + return mapFacets((facet) => facetOptions(totals[facet], facetValueCounts(held, facet, query.facets), facet, query.facets[facet])); + }, [scoped, totals, query.facets]); + // Column definitions are written as plain literals against the hook's own column type, not built through createColumnHelper: the helper infers each accessor over its own value type, which does not inter-type with the widened union under exactOptionalPropertyTypes. The order matches the static table's Name, Id, Kind, Domain so the two views of the listing read the same way. const columns: readonly ColumnDef[] = [ diff --git a/site/e2e/site.browser.ts b/site/e2e/site.browser.ts index 47f2d49e..5abd7f45 100644 --- a/site/e2e/site.browser.ts +++ b/site/e2e/site.browser.ts @@ -163,6 +163,27 @@ test("choosing a filter value adds a history entry, typing does not, and Back an expect((await shownOf(page)).shown).toBeLessThan(graphs.shown); }); +test("a dropdown counts each option against the other filters, and says how many there are in all when that differs", async ({ page }) => { + await page.goto("/"); + const subject = page.getByRole("combobox", { name: "Subject", exact: true }); + await subject.click(); + const unfiltered = await page.getByRole("option").allInnerTexts(); + expect(unfiltered.every((text) => !text.includes(" of "))).toBe(true); + await page.keyboard.press("Escape"); + + await page.goto("/?kind=graph"); + await subject.click(); + const narrowed = await page.getByRole("option").allInnerTexts(); + const counted = narrowed.map((text) => /^(.*) \((?:(\d+) of )?(\d+)\)$/.exec(text.replaceAll("\n", " "))); + expect(counted.every((match) => match !== null)).toBe(true); + const partial = counted.filter((match) => match?.[2] !== undefined); + expect(partial.length).toBeGreaterThan(0); + expect(partial.every((match) => Number(match?.[2]) < Number(match?.[3]))).toBe(true); + // The listing's own total is the sum of the filtered rows, so no option can claim more than the page holds. + const { shown } = await shownOf(page); + expect(counted.every((match) => Number(match?.[2] ?? match?.[3]) <= shown)).toBe(true); +}); + test("a chosen value leads its dropdown, ticked, and a click on it clears it", async ({ page }) => { await page.goto("/?kind=resources"); const kind = page.getByRole("combobox", { name: "Kind", exact: true }); diff --git a/site/listing-query.ts b/site/listing-query.ts index 8ee6fcee..d90c41ce 100644 --- a/site/listing-query.ts +++ b/site/listing-query.ts @@ -179,3 +179,25 @@ export function listingLocation(query: ListingQuery, own: string | undefined, he export function matchesFacets(held: FacetValues, facets: FacetValues): boolean { return FACETS.every((facet) => facets[facet].length === 0 || facets[facet].some((value) => held[facet].includes(value))); } + +/** + * How many of some records hold each value of one attribute, given the choices made in the other attributes: the number an option would add to the listing if it were chosen alongside what is already chosen. The attribute's own choices are not applied, since several values of one attribute are alternatives, so each of its options is counted against the same other filters. + * @param held - What each record holds, already narrowed by every filter that is not an attribute (free text, entity bounds). + * @param facet - The attribute whose options are counted. + * @param facets - The values chosen for every attribute. + * @returns For each value some record holds, how many of the records the other attributes admit hold it. + */ +export function facetValueCounts(held: readonly FacetValues[], facet: Facet, facets: FacetValues): ReadonlyMap { + const others = { ...facets, [facet]: [] }; + const counts = new Map(); + for (const record of held) { + if (!matchesFacets(record, others)) { + continue; + } + for (const value of new Set(record[facet])) { + counts.set(value, (counts.get(value) ?? 0) + 1); + } + } + + return counts; +} diff --git a/site/listing-query.unit.test.ts b/site/listing-query.unit.test.ts index 89c847e7..ec00ad23 100644 --- a/site/listing-query.unit.test.ts +++ b/site/listing-query.unit.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { EMPTY_LISTING_QUERY, facetQuery, initialListingQuery, listingLocation, matchesFacets, parseListingQuery, serialiseListingQuery, type ListingQuery } from "./listing-query"; +import { EMPTY_LISTING_QUERY, facetQuery, facetValueCounts, initialListingQuery, listingLocation, matchesFacets, parseListingQuery, serialiseListingQuery, type FacetValues, type ListingQuery } from "./listing-query"; const full: ListingQuery = { text: "lead time & cost", @@ -84,3 +84,25 @@ describe("where a listing starts and what link it writes", () => { expect(listingLocation(EMPTY_LISTING_QUERY, own, "/keepset/tags/weight/", "/keepset/")).toBe("/keepset/"); }); }); + +describe("facetValueCounts", () => { + const record = (kind: string, domain: string, subjects: string[]): FacetValues => ({ ...EMPTY_LISTING_QUERY.facets, kind: [kind], domain: [domain], subject: subjects }); + const held = [record("graph", "Social", ["a", "b"]), record("graph", "Academic", ["a"]), record("table", "Academic", ["c"])]; + + it("counts every value against all records when nothing is chosen", () => { + expect([...facetValueCounts(held, "subject", EMPTY_LISTING_QUERY.facets)]).toEqual([["a", 2], ["b", 1], ["c", 1]]); + }); + + it("counts a value against the other attributes' choices only", () => { + const counts = facetValueCounts(held, "subject", { ...EMPTY_LISTING_QUERY.facets, domain: ["Social"] }); + expect(counts.get("a")).toBe(1); + expect(counts.get("b")).toBe(1); + expect(counts.get("c")).toBeUndefined(); + }); + + it("does not apply the attribute's own choices, so its alternatives are counted alike", () => { + const counts = facetValueCounts(held, "subject", { ...EMPTY_LISTING_QUERY.facets, subject: ["c"], domain: ["Academic"] }); + expect(counts.get("a")).toBe(1); + expect(counts.get("c")).toBe(1); + }); +}); From 10202e7d181c9a4fe5b3da23ba77cb040662d892 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Wed, 11 Nov 2026 08:28:02 +0000 Subject: [PATCH 2/2] test(site): take the fixture's subjects as a readonly array --- site/listing-query.unit.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/listing-query.unit.test.ts b/site/listing-query.unit.test.ts index ec00ad23..a827b1ed 100644 --- a/site/listing-query.unit.test.ts +++ b/site/listing-query.unit.test.ts @@ -86,7 +86,7 @@ describe("where a listing starts and what link it writes", () => { }); describe("facetValueCounts", () => { - const record = (kind: string, domain: string, subjects: string[]): FacetValues => ({ ...EMPTY_LISTING_QUERY.facets, kind: [kind], domain: [domain], subject: subjects }); + const record = (kind: string, domain: string, subjects: readonly string[]): FacetValues => ({ ...EMPTY_LISTING_QUERY.facets, kind: [kind], domain: [domain], subject: subjects }); const held = [record("graph", "Social", ["a", "b"]), record("graph", "Academic", ["a"]), record("table", "Academic", ["c"])]; it("counts every value against all records when nothing is chosen", () => {