Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions site/client/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -191,15 +219,15 @@ function Listing({ records, place }: { readonly records: readonly SearchRecord[]
<div className="island-controls">
<TextInput className="island-search" aria-label="Search the catalogue" placeholder="search the catalogue" value={query.text} onChange={(event) => {
const text = event.currentTarget.value;
setQuery((current) => ({ ...current, text }));
change("replace", (current) => ({ ...current, text }));
}} />
{FACETS.map((facet) => (
<MultiSelect key={facet} className="island-filter" aria-label={FACET_CONTROLS[facet].label} placeholder={FACET_CONTROLS[facet].placeholder} data={options[facet]} value={[...query.facets[facet]]} onChange={(values) => { setQuery((current) => ({ ...current, facets: { ...current.facets, [facet]: values } })); }} clearable searchable renderPill={(pill) => facetPill(query.facets[facet], pill)} />
<MultiSelect key={facet} className="island-filter" aria-label={FACET_CONTROLS[facet].label} placeholder={FACET_CONTROLS[facet].placeholder} data={options[facet]} value={[...query.facets[facet]]} onChange={(values) => { change("push", (current) => ({ ...current, facets: { ...current.facets, [facet]: values } })); }} clearable searchable renderPill={(pill) => facetPill(query.facets[facet], pill)} />
))}
<div className="island-range" role="group" aria-label="Entities">
<span className="island-range-label" aria-hidden="true">Entities</span>
{([["min", "minEntities"], ["max", "maxEntities"]] as const).map(([key, field]) => (
<NumberInput key={key} className="island-bound" aria-label={`${key === "min" ? "Minimum" : "Maximum"} entities`} placeholder={key} min={0} allowNegative={false} allowDecimal={false} value={query[field] ?? ""} onChange={(value) => { setQuery((current) => ({ ...current, [field]: typeof value === "number" ? value : undefined })); }} />
<NumberInput key={key} className="island-bound" aria-label={`${key === "min" ? "Minimum" : "Maximum"} entities`} placeholder={key} min={0} allowNegative={false} allowDecimal={false} value={query[field] ?? ""} onChange={(value) => { change("replace", (current) => ({ ...current, [field]: typeof value === "number" ? value : undefined })); }} />
))}
</div>
<Badge variant="light" size="lg">
Expand Down Expand Up @@ -266,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]);

Expand Down
31 changes: 31 additions & 0 deletions site/e2e/site.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> => 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 });
Expand Down
Loading