Skip to content

Add sort and filter controls to the data-bound screen (#330) - #385

Merged
jimhoyd merged 1 commit into
mainfrom
feat/crud-sort-filter-screen
Sep 21, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
feat/crud-sort-filter-screen

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

Finishes #330. The store half shipped in #357 (collections declare sortable/filterable fields; list requests take sort=<field>/sort=-<field> and up to three equality filters; undeclared names return 400 invalid_query). This adds the screen half, which had previously been reverted for pushing @jimhoyd/urlcode-ui about 2.2 KB over its 350 KiB unpacked-size budget (scripts/package-audit.ts), per the recorded decision in docs/OPEN-DECISIONS.md.

Per instructions, the budget was not raised. The trim option was tried first, honestly, and it worked with room to spare.

Byte accounting

Change dist file Before After Delta
Widen kitCss/kitCssLimit type (packages/ui/src/kit-styles.ts) dist/kit-styles.d.ts 34,423 562 −33,861
Widen stylesheet type (packages/ui/scripts/build-styles.mjs → generated styles.generated.ts) dist/styles.generated.d.ts 34,583 41 −34,542
Screen slice (sort select, filter controls, data-query attribute, one copy key, script changes) dist/crud.js, dist/crud.d.ts, dist/crud-script.js — — +3,483

Package total (unpacked, npm pack --dry-run): 356,904 → 288,561 (trim) → 292,613 bytes, against the unchanged 358,400-byte (350 KiB) budget — about 66 KB of headroom remaining.

The trim: kitCss, kitCssLimit and stylesheet were exported with tsc-inferred string/number-literal types, so their .d.ts declarations duplicated the entire compiled value (65 KB+ of generated CSS) a second time as a type. Adding an explicit : string / : number annotation widens the type to the primitive, which drops that duplication. No change to the emitted JavaScript, the runtime value, or any consumer — nothing in the tree matches on the literal type (verified by search).

What shipped

  • CrudCollection gains optional sortable/filterable (same shape and validation as the store: at most 8 names each, must be declared fields, filterable cannot reuse limit/cursor/sort; a bad declaration throws naming the offending key, same as columns).
  • crudMarkup adds a data-query attribute (only when either list is non-empty) carrying the sortable/filterable field names and labels; a collection with neither list renders byte-identical output to before (data-query omitted entirely).
  • The crud kit script adds a sort <select> and one control per filterable field (reusing the same control() builder; a boolean filter becomes a two-option select for true/false) when data-query is present, and nothing otherwise.
  • Requests are built with URLSearchParams — never string concatenation — from the controls' current values. "Load more" reuses the sort/filter query string that was applied when the list was last (re)loaded, not whatever the controls hold at click time, so a control edited but not yet applied can't leak into a cursor follow-up.
  • One new copy key: ui.crud.sort ("Sort by").
  • No inline script; the crud script is still served nonce'd and content-hashed (its hash changes because its content changed — expected).
  • The script still never assigns markup (verified by the existing "never assigns markup" test) and avoids backticks/backslashes/${} so it survives its template-literal wrapper unchanged (verified programmatically).

Tests added (packages/ui/test/crud.test.ts)

  • A collection with no sortable/filterable (or empty arrays) renders exactly the same shell as before.
  • Hostile/invalid declarations are refused with the offending key named (__proto__, constructor, duplicate names, non-string entries, over 8 entries, reusing sort as a filter name).
  • Sort/filter values reach markup only as escaped JSON attribute data (an enum value containing <b>/<img> never reaches raw HTML).
  • Client test: the sort <select> options and filter controls match the declared fields only, in order; changing a control fires a URLSearchParams-built GET with hostile values (<b>x</b>&a=1) correctly encoded; "load more" repeats the previously applied query plus cursor.
  • Client test: a collection without declarations shows no sort/filter controls and issues a plain request.

Verification

  • npm run typecheck, npm run build, npm test (root) — pass.
  • npm run verify --workspace @jimhoyd/urlcode-ui — pass (85 unit tests including the 4 new ones, plus the pre-existing edit-in-progress, checkbox-rollback, and strict-CSP tests, unaffected).
  • npm run test:browser --workspace @jimhoyd/urlcode-ui — Chrome was available in this environment; all 3 browser tests (hostile markup, edit-in-progress survives re-render, checkbox rollback) passed.
  • npm run audit:packages — all five packages pass; @jimhoyd/urlcode-ui at 292,613 / 358,400 bytes.
  • npm run verify (full root chain: lint, typecheck, check, build, test, verify:workspaces including audit:packages) — pass.
  • npm run check:docs — pass (trust-model prose, version statements, local links, guidance claims, YAML reference, llms reference all current).
  • npm run docs:llms — regenerated (the file is gitignored, so no diff to commit).

Docs updated

Open decisions

None remaining for #330 — it fit the existing budget, so no docs-only fallback PR was needed. The only unrelated pending #330-adjacent item already tracked separately: per-record ownership (#331) still applies to sort/filter as it does to the rest of the collection (sort/filter act on the whole collection, as documented).

Siblings avoided: #383 (redirect guidance/llms.txt/recipes work) and #309 (Cloudflare 404 research) — untouched.

This PR does not close #330 (per instructions) and is not set to auto-merge.

🤖 Generated with Claude Code

The store half shipped in #357. This finishes the screen half, which had
been reverted for pushing @jimhoyd/urlcode-ui about 2.2 KB over its
350 KiB unpacked-size budget.

Trimmed first, honestly: kitCss, stylesheet and kitCssLimit in
packages/ui/src/kit-styles.ts and the generated styles.generated.ts were
exported with an inferred string-literal type, so tsc duplicated the
entire compiled value into the .d.ts declaration a second time. Adding
an explicit `: string`/`: number` annotation widens the type and drops
that duplication: dist/kit-styles.d.ts 34,423 -> 562 bytes,
dist/styles.generated.d.ts 34,583 -> 41 bytes, about 68 KB saved
unpacked, no change to emitted JavaScript, runtime values or any
consumer (nothing in the tree matches on the literal type).

That headroom fits the screen slice: a collection that declares
`sortable`/`filterable` fields (the same lists urlcode-store checks at
activation) gets a sort select and one control per filterable field in
the crud kit script. Only declared field names are ever offered;
requests are built with URLSearchParams from the controls' current
values; "load more" repeats the sort and filters applied when the page
was last loaded rather than whatever the controls hold when the button
is pressed; values are never echoed unescaped. One new copy key,
ui.crud.sort. A collection with neither list renders the identical
shell it did before (no data-query attribute, no controls).

@jimhoyd/urlcode-ui is now 292,613 bytes unpacked (was 356,904 before
this change), about 66 KB under the unchanged 358,400-byte budget.

Updated packages/ui README/CHANGELOG, docs/STORE.md and the
docs/OPEN-DECISIONS.md row for #330 (marked done).

Refs #330.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jimhoyd
jimhoyd merged commit d5eade5 into main Sep 21, 2026
20 checks passed
@jimhoyd
jimhoyd deleted the feat/crud-sort-filter-screen branch September 21, 2026 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Request]: Richer data-bound CRUD screens: per-field labels, columns, filtering and sorting

1 participant