Skip to content

Commit 823a6b6

Browse files
TMHSDigitalclaude
andcommitted
search: filter defaults in one place; stateQuery writes every choice
stateQuery listed five of the six choice keys by hand, so a new choice would be read from a shared link and then dropped from the URL. It now iterates CHOICES (category first, then q, then the rest). The filter defaults move from app.js and a test copy into search.mjs FILTER_DEFAULTS. A round-trip test sets every choice and switch to a non-default value and checks each survives, and that every state field is a choice, a switch or q. Closes #223 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpkeXQPaUhxjgqdqnk6BrJ
1 parent 7f9dc0b commit 823a6b6

3 files changed

Lines changed: 45 additions & 22 deletions

File tree

‎site/lib/lib.test.mjs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ eq("every word must match", kScore("ui zzz"), 0);
923923
eq("name hit outranks summary hit", kScore("kit") > kScore("panels"), true);
924924
eq("regex characters are literal", kScore("c++"), 0);
925925

926-
const URL_DEF = { category: "all", q: "", active: true, review: true, deprecated: false, commercialOnly: false, noAttr: false, perspective: "any", license: "any", format: "any", engine: "any", sort: "name" };
926+
const { FILTER_DEFAULTS: URL_DEF, CHOICES: URL_CHOICES, SWITCHES: URL_SWITCHES } = await import("./search.mjs");
927927
const URL_ALLOWED = { category: ["all", "2d", "3d"], sort: ["name", "license", "verified"], perspective: ["any", "top_down"], license: ["any", "cc0"], format: ["any", "image"] };
928928
const urlSt = readState("?cat=2d&q=pixel&sort=bogus&view=top_down&deprecated=1&review=maybe&licence=cc0", URL_DEF, URL_ALLOWED);
929929
eq("url: known category kept", urlSt.category, "2d");
@@ -937,6 +937,21 @@ eq("url: round trip", readState(`?${stateQuery(urlSt, URL_DEF)}`, URL_DEF, URL_A
937937
eq("url: defaults write nothing", stateQuery(URL_DEF, URL_DEF), "");
938938
eq("url: switch off written as 0", stateQuery({ ...URL_DEF, active: false }, URL_DEF), "active=0");
939939
eq("url: blank q not written", stateQuery({ ...URL_DEF, q: " " }, URL_DEF), "");
940+
{
941+
// #223: every choice and switch survives a round trip through the URL.
942+
const other = { category: "3d", sort: "verified", perspective: "top_down", license: "cc0", format: "image", engine: "godot" };
943+
const allowed = Object.fromEntries(URL_CHOICES.map(([, f]) => [f, [URL_DEF[f], other[f]]]));
944+
const changed = { ...URL_DEF, q: "pixel art" };
945+
for (const [, f] of URL_CHOICES) {
946+
eq(`url: ${f} has a non-default test value`, typeof other[f] === "string" && other[f] !== URL_DEF[f], true);
947+
changed[f] = other[f];
948+
}
949+
for (const [, f] of URL_SWITCHES) changed[f] = !URL_DEF[f];
950+
const back = readState(`?${stateQuery(changed, URL_DEF)}`, URL_DEF, allowed);
951+
for (const f of Object.keys(URL_DEF)) eq(`url: round trip keeps ${f}`, back[f], changed[f]);
952+
eq("url: every state field is a choice, a switch or q", Object.keys(URL_DEF).sort().join(), ["q", ...URL_CHOICES.map(([, f]) => f), ...URL_SWITCHES.map(([, f]) => f)].sort().join());
953+
eq("url: category first, then q", stateQuery(changed, URL_DEF).split("&").slice(0, 2).map((kv) => kv.split("=")[0]).join(), "cat,q");
954+
}
940955

941956
const fmtGroups = { image: { formats: ["PNG", "SVG"] } };
942957
eq("filter: needs-review hidden when review off", passesFilters({ status: "needs-review" }, { ...URL_DEF, review: false }), false);

‎site/lib/search.mjs‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,27 @@ export function gapIssueUrl(repo, query, categoryLabel = "") {
248248
return `${repo}/issues/new?template=request-asset.yml&title=${encodeURIComponent(title)}`;
249249
}
250250

251-
const CHOICES = [
251+
/**
252+
* The filter state of the catalog page and its defaults: the one list. The
253+
* page starts from it, and the URL holds only what differs from it.
254+
*/
255+
export const FILTER_DEFAULTS = Object.freeze({
256+
category: "all",
257+
q: "",
258+
active: true,
259+
review: true,
260+
deprecated: false,
261+
commercialOnly: false,
262+
noAttr: false,
263+
perspective: "any",
264+
license: "any",
265+
format: "any",
266+
engine: "any",
267+
sort: "name",
268+
});
269+
270+
/** Choice params: URL key -> state field, in the order the URL writes them (after q). */
271+
export const CHOICES = [
252272
["cat", "category"],
253273
["sort", "sort"],
254274
["view", "perspective"],
@@ -257,7 +277,7 @@ const CHOICES = [
257277
["engine", "engine"],
258278
];
259279
/** On/off params: URL key -> state field, written as 1 or 0. */
260-
const SWITCHES = [
280+
export const SWITCHES = [
261281
["active", "active"],
262282
["review", "review"],
263283
["deprecated", "deprecated"],
@@ -292,11 +312,12 @@ export function readState(search, defaults, allowed) {
292312
/** The query string for a state: only what differs from the defaults. */
293313
export function stateQuery(state, defaults) {
294314
const p = new URLSearchParams();
295-
const field = Object.fromEntries(CHOICES);
296-
if (state.category !== defaults.category) p.set("cat", state.category);
315+
// The category reads first in a shared link, then the search, then the rest.
316+
const [[catKey, catField], ...rest] = CHOICES;
317+
if (state[catField] !== defaults[catField]) p.set(catKey, state[catField]);
297318
if (state.q.trim()) p.set("q", state.q.trim());
298-
for (const key of ["sort", "view", "licence", "format", "engine"]) {
299-
if (state[field[key]] !== defaults[field[key]]) p.set(key, state[field[key]]);
319+
for (const [key, f] of rest) {
320+
if (state[f] !== defaults[f]) p.set(key, state[f]);
300321
}
301322
for (const [key, f] of SWITCHES) {
302323
if (state[f] !== defaults[f]) p.set(key, state[f] ? "1" : "0");

‎site/public/app.js‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { gapIssueUrl, closestEntries, computeResults as computeResultsFor, passesFilters as passes, placementKey, queryTests as compileQuery, readState, scoreFields, searchFields, shouldGroup as shouldGroupFor, slugOf, sorterFor, SORTS, stateQuery, ENGINES } from "./search.js";
1+
import { FILTER_DEFAULTS, gapIssueUrl, closestEntries, computeResults as computeResultsFor, passesFilters as passes, placementKey, queryTests as compileQuery, readState, scoreFields, searchFields, shouldGroup as shouldGroupFor, slugOf, sorterFor, SORTS, stateQuery, ENGINES } from "./search.js";
22
import { CARD_LIMIT, LIST_LIMIT, commercialLabel, entryCardHtml, escapeHtml, groupHeadingHtml, groupMoreHtml, PERSPECTIVE_LABELS } from "./cards.mjs";
33
import { installViewToggles } from "./views.js";
44
import { PERSPECTIVES } from "./shared.mjs";
@@ -19,20 +19,7 @@ import { PERSPECTIVES } from "./shared.mjs";
1919
const $ = (sel, root = document) => root.querySelector(sel);
2020
const $$ = (sel, root = document) => [...root.querySelectorAll(sel)];
2121

22-
const DEFAULTS = {
23-
category: "all",
24-
q: "",
25-
active: true,
26-
review: true,
27-
deprecated: false,
28-
commercialOnly: false,
29-
noAttr: false,
30-
perspective: "any",
31-
license: "any",
32-
format: "any",
33-
engine: "any",
34-
sort: "name",
35-
};
22+
const DEFAULTS = FILTER_DEFAULTS;
3623
const state = { ...DEFAULTS };
3724
// Licence families and format groups come from the build (license-vocabulary.json, format-vocabulary.json).
3825
const LICENSE_FAMILIES = data.filters?.licenseFamilies || {};

0 commit comments

Comments
 (0)