From 452158549b1d8d18941ce993530954475bdb402c Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Wed, 11 Nov 2026 08:18:06 +0000 Subject: [PATCH 1/2] feat(site): add a history entry when a filter value is chosen, and restore the view on Back Choosing or clearing a value in a dropdown pushes an entry; typing in the search box or the entity bounds still rewrites the current one so a query is not one entry per keystroke. A popstate rebuilds the filters from the URL. --- site/client/app.tsx | 46 ++++++++++++++++++++++++++++++++-------- site/e2e/site.browser.ts | 31 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/site/client/app.tsx b/site/client/app.tsx index abc8eff8..a1609639 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 } from "../listing-query"; +import { EMPTY_LISTING_QUERY, FACETS, initialListingQuery, listingLocation, mapFacets, matchesFacets, parseListingQuery, type Facet, type FacetValues, type ListingQuery } from "../listing-query"; import { siteRoot } from "./site-root"; import "../styles.css"; @@ -123,19 +123,47 @@ interface ListingPlace { } /** - * Writes a location into the page's URL in place, without a reload or a new history entry, so the address bar always holds a link to the view on screen. + * Writes a location into the page's URL without a reload, so the address bar always holds a link to the view on screen. * @param location - The path and query string. + * @param entry - "push" adds a history entry, so Back returns to the view before the change; "replace" rewrites the current one, so a typed input does not leave an entry for every keystroke. */ -function writeLocation(location: string): void { - window.history.replaceState(window.history.state, "", `${location}${window.location.hash}`); +function writeLocation(location: string, entry: "push" | "replace"): void { + const target = `${location}${window.location.hash}`; + if (entry === "push") { + window.history.pushState(window.history.state, "", target); + } else { + window.history.replaceState(window.history.state, "", target); + } } -/** The interactive listing: filter inputs over the search index and a sortable table of what matches. The filters start from the page's own filter on a listing page and from the URL's query string on the home listing, and are written back to the URL on every change (see listingLocation). */ +/** The interactive listing: filter inputs over the search index and a sortable table of what matches. The filters start from the page's own filter on a listing page and from the URL's query string on the home listing, and are written back to the URL on every change (see listingLocation): choosing values adds a history entry and Back or Forward restores the view, while typing rewrites the current entry. */ function Listing({ records, place }: { readonly records: readonly SearchRecord[]; readonly place: ListingPlace }): ReactNode { const [query, setQuery] = useState(() => initialListingQuery(place.own, window.location.search)); + // How the next change reaches the history: choosing values is a step the user may want to go back over, typing is refinement of one step. + const entry = useRef<"push" | "replace">("replace"); + const change = (how: "push" | "replace", next: (current: ListingQuery) => ListingQuery): void => { + entry.current = how; + setQuery(next); + }; useEffect(() => { - writeLocation(listingLocation(query, place.own, place.here, place.home)); + const location = listingLocation(query, place.own, place.here, place.home); + // The address already shows this view after Back or Forward, and on mount when the link was already in canonical form: nothing to write. + if (location !== `${window.location.pathname}${window.location.search}`) { + writeLocation(location, entry.current); + } + entry.current = "replace"; }, [query, place]); + useEffect(() => { + const restore = (): void => { + entry.current = "replace"; + setQuery(initialListingQuery(place.own, window.location.search)); + }; + window.addEventListener("popstate", restore); + + return () => { + window.removeEventListener("popstate", restore); + }; + }, [place]); const options = useMemo(() => mapFacets((facet) => facetOptions(records, facet, query.facets[facet])), [records, query.facets]); @@ -191,15 +219,15 @@ function Listing({ records, place }: { readonly records: readonly SearchRecord[]
{ const text = event.currentTarget.value; - setQuery((current) => ({ ...current, text })); + change("replace", (current) => ({ ...current, text })); }} /> {FACETS.map((facet) => ( - { setQuery((current) => ({ ...current, facets: { ...current.facets, [facet]: values } })); }} clearable searchable renderPill={(pill) => facetPill(query.facets[facet], pill)} /> + { change("push", (current) => ({ ...current, facets: { ...current.facets, [facet]: values } })); }} clearable searchable renderPill={(pill) => facetPill(query.facets[facet], pill)} /> ))}
{([["min", "minEntities"], ["max", "maxEntities"]] as const).map(([key, field]) => ( - { setQuery((current) => ({ ...current, [field]: typeof value === "number" ? value : undefined })); }} /> + { change("replace", (current) => ({ ...current, [field]: typeof value === "number" ? value : undefined })); }} /> ))}
diff --git a/site/e2e/site.browser.ts b/site/e2e/site.browser.ts index 4188814a..47f2d49e 100644 --- a/site/e2e/site.browser.ts +++ b/site/e2e/site.browser.ts @@ -132,6 +132,37 @@ for (const url of ["/", "/?kind=graph", "/?kind=graph,resources&domain=Social%20 }); } +test("choosing a filter value adds a history entry, typing does not, and Back and Forward restore the view", async ({ page }) => { + await page.goto("/"); + await expect(page.getByLabel("Search the catalogue")).toBeVisible(); + const all = await shownOf(page); + const entries = async (): Promise => page.evaluate(() => window.history.length); + const start = await entries(); + await page.getByRole("combobox", { name: "Kind", exact: true }).click(); + await page.getByRole("option", { name: /^graph \(/ }).click(); + await expect(page).toHaveURL(/\/\?kind=graph$/); + expect(await entries()).toBe(start + 1); + const graphs = await shownOf(page); + expect(graphs.shown).toBeLessThan(all.shown); + + await page.getByLabel("Search the catalogue").pressSequentially("karate"); + await expect(page).toHaveURL(/\/\?kind=graph&q=karate$/); + expect(await entries()).toBe(start + 1); + + await page.goBack(); + await expect(page).not.toHaveURL(/kind=/); + await expect(page.locator("#island-root .island-filter .mantine-Pill-root")).toHaveCount(0); + await expect(page.getByLabel("Search the catalogue")).toHaveValue(""); + expect(await shownOf(page)).toEqual(all); + expect(await entries()).toBe(start + 1); + + await page.goForward(); + await expect(page).toHaveURL(/\/\?kind=graph&q=karate$/); + await expect(page.locator("#island-root .island-filter .mantine-Pill-root", { hasText: "graph" })).toBeVisible(); + await expect(page.getByLabel("Search the catalogue")).toHaveValue("karate"); + expect((await shownOf(page)).shown).toBeLessThan(graphs.shown); +}); + 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 }); From b3899d3b5e2d3bc6fe088cd82026153fefac58b7 Mon Sep 17 00:00:00 2001 From: Joseph Mearman Date: Wed, 11 Nov 2026 08:15:39 +0000 Subject: [PATCH 2/2] fix(site): the index text filter rewrites its history entry as it is typed --- site/client/app.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/client/app.tsx b/site/client/app.tsx index a1609639..c108d7f6 100644 --- a/site/client/app.tsx +++ b/site/client/app.tsx @@ -294,7 +294,7 @@ function GroupIndex({ groups, noun, initialOrder }: { readonly groups: readonly const [text, setText] = useState(() => parseListingQuery(window.location.search).text); const [order, setOrder] = useState(initialOrder); useEffect(() => { - writeLocation(listingLocation({ ...EMPTY_LISTING_QUERY, text }, undefined, window.location.pathname, window.location.pathname)); + writeLocation(listingLocation({ ...EMPTY_LISTING_QUERY, text }, undefined, window.location.pathname, window.location.pathname), "replace"); }, [text]); const shown = useMemo(() => orderGroups(matchingGroups(groups, text), order), [groups, text, order]);