Skip to content

fix(admin): make content list search, sort, and pagination work across the whole collection - #749

Closed
edrpls wants to merge 3 commits into
emdash-cms:mainfrom
edrpls:fix/content-list-search-sort-pagination
Closed

edrpls wants to merge 3 commits into
emdash-cms:mainfrom
edrpls:fix/content-list-search-sort-pagination

Conversation

@edrpls

@edrpls edrpls commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes three compounding UX failures in the admin collection list view (/_emdash/admin/content/{collection}). Each one looks like a separate paper-cut in isolation, but together they make the admin functionally broken for any collection larger than the first API page.

1. Search didn't find existing content

Typing Bleeding in the search box on a collection with 100+ entries returned zero results even though a published entry titled exactly Bleeding existed. The admin filtered client-side against items already in memory, and the API accepted neither a search nor a q param. For entries past the first fetch, the user saw "No results" and had no way to know the item was there.

Fix: GET /_emdash/api/content/{collection} now accepts q — case-insensitive substring match against whichever of title, name, and slug actually exist on the collection's table (introspected via pragma_table_info / information_schema.columns). LIKE wildcards are escaped (%, _, \), inputs are trimmed + capped at 200 chars, and the MCP content_list tool accepts q too.

2. Column headers weren't sort controls

Title, Status, Locale, Date were plain <th> labels with no click handler, no aria-sort, no indication they could do anything. The repository's FindManyOptions already supported orderBy, but nothing in the admin sent it.

Fix: headers are now real <button>s wrapped in <th aria-sort>. Click cycles direction; screen readers get the sort state via aria-sort on the header (which they announce automatically), so the button's accessible name stays clean. Extended the server's mapOrderField whitelist to include status, locale, and name alongside the existing date fields.

3. Pagination denominator grew as you paged forward

Pages 1–4 showed 1/5, 2/5, …; page 5 showed 5/10; page 10 showed 10/15; and so on. PAGE_SIZE = 20 paginates over what's been fetched; limit = 100 per API call means 5 client pages per fetch. The auto-fetch effect fired when you reached the last client page, another 100 arrived, totalPages jumped, and the denominator lurched forward.

Fix: list response now returns total alongside items + nextCursor. Admin uses it as the stable denominator (a 143-entry collection reads 1/8 on page 1). When a server doesn't return total, the admin falls back to the pre-change behavior, so old servers keep working.

While I was there

  • ContentPickerModal had the same client-side search bug — it now passes q to the API too. Added placeholderData: keepPreviousData so the picker list doesn't flash to empty between keystrokes while the debounced query refetches.
  • clampedPage logic in ContentList handles the edge case where filters collapse the result count below the current page (previously rendered an empty table until the next refetch).

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

The symptoms are user-facing (search doesn't find existing content; pagination denominator is non-monotonic) and the fixes are scoped to the list endpoint + admin list component + picker. Happy to split into three separate PRs if preferred, but I think the change is easier to review together since the three symptoms share the same root cause (admin treating the list as a thin client over a cursor-only API with no server-side query support).

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes (admin + core)
  • pnpm lint passes (no new findings; pre-existing workspace-wide warnings unchanged)
  • pnpm test passes for touched areas: packages/core repo + handler suites (92 tests), packages/admin component suite (39 tests including 5 new ones)
  • pnpm format has been run
  • Added regression tests that reproduce each of the three bugs before the fix
  • New admin strings were wrapped for translation and pnpm locale:extract was run; the diff simplifies the a11y strings (net -3 new messages across all locales)
  • Added a changeset (patch for emdash and @emdash-cms/admin)
  • New features link to an approved Discussion (N/A — bug fix)

AI-generated code disclosure

  • This PR includes AI-generated code

Produced in a single Claude Code session. Ran Code Reviewer, Security Engineer, and Accessibility Auditor sub-agents against the committed-but-unpushed branch and applied their findings in commit 2 before pushing — specifically:

  • a11y: dropped a verbose aria-label in favor of aria-sort (which screen readers announce automatically) and added an explicit focus-visible outline since kumo resets browser defaults.
  • security: confirmed pragma_table_info(?) is parameterized by better-sqlite3 and validateIdentifier is belt-and-braces, not load-bearing; LIKE escape order is correct; added ESCAPE '\\' pairing verified.
  • code review: added tests for slug-only collections (collection with no title/name) and for multi-page search preserving total across cursor pages; documented the auto-fetch effect's reliance on the caller deduping onLoadMore.

Testing

From a fresh pnpm install + pnpm build:

packages/core $ pnpm test tests/database/repositories/content.test.ts tests/unit/api/content-handlers.test.ts
  ✓ tests/unit/api/content-handlers.test.ts (24 tests)
  ✓ tests/database/repositories/content.test.ts (68 tests)

packages/admin $ pnpm test tests/components/ContentList.test.tsx
  ✓ tests/components/ContentList.test.tsx (39 tests)

packages/admin $ pnpm test   # full suite
  Test Files  54 passed (54)
  Tests       791 passed (791)

New regression tests (all fail on main, pass on this branch):

  • ContentRepository > findMany > search > finds items by a title substring match
  • ContentRepository > findMany > search > is case-insensitive
  • ContentRepository > findMany > search > searches slug-only collections that have no title/name columns
  • ContentRepository > findMany > search > keeps list items and total consistent across pagination
  • ContentRepository > findMany > search > escapes LIKE wildcards so they don't act as patterns
  • ContentRepository > findMany > total > reports total rows regardless of limit
  • ContentRepository > findMany > orderBy > accepts status as an order field
  • Content Handlers — list search and total > filters items by q (case-insensitive substring)
  • Content Handlers — list search and total > returns total independent of limit
  • ContentList > pagination > uses \total` as a stable denominator instead of items.length`
  • ContentList > sortable headers > calls onSortChange when a header is clicked
  • ContentList > sortable headers > exposes sort state via aria-sort on the active header
  • ContentList > controlled search > forwards the typed query to onSearchChange without local filtering

edrpls added 3 commits April 23, 2026 19:15
…s the whole collection

The admin collection list (`/_emdash/admin/content/{collection}`) had three
compounding UX failures that made it functionally broken past the first
API page.

1. Search was client-side, title-only, and scoped to already-fetched
   pages. Typing "Bleeding" on a collection with 100+ entries showed
   zero results because the matching item was on API page 2. The
   handler now accepts `q`; `ContentRepository.findMany` introspects
   the table's columns and builds a case-insensitive `LIKE` against
   whichever of `title`, `name`, `slug` actually exist. LIKE wildcards
   are escaped; inputs are trimmed and capped at 200 chars.

2. Column headers were static labels. They're now buttons that call
   `onSortChange` and surface direction via `aria-sort`. The repo's
   order whitelist picks up `status`, `locale`, and `name` in addition
   to the existing date fields.

3. The pagination denominator grew in increments of 5 as the user
   paged forward (API limit 100, client page size 20). The list
   response now includes `total`; the admin uses it as the stable
   denominator and clamps the current page if filters collapse the
   count.

The MCP `content_list` tool also accepts `q`, so agents don't have to
post-filter either.

Tests: adds regression coverage for all three bugs in both the repo
and the handler, plus controlled-search and sortable-header cases in
the `ContentList` component test. The existing pagination tests still
pass with the legacy unconditional behavior, proving backward compat.
- a11y: drop the redundant aria-label on sort buttons; screen readers
  announce sort state via aria-sort on the <th> already. The visible
  "Title" span is now the button's accessible name, matching the
  WAI-ARIA APG sortable-table pattern and removing verbose re-reads
  on every focus.
- a11y: add an explicit focus-visible outline to the sort button so
  the focus ring doesn't rely on browser defaults (kumo resets them).
- docs: comment the auto-fetch effect in ContentList to document its
  reliance on the caller's onLoadMore being deduped (the router wires
  it to react-query's fetchNextPage, which is).
- ContentPickerModal: pass `placeholderData: keepPreviousData` to
  react-query so the list doesn't flash to empty between keystrokes
  while the debounced search refetches.
- tests: add aria-sort assertions on both the active and inactive
  headers, a slug-only-collection search case (collections with no
  title/name column), and a multi-page search that proves `total`
  stays consistent across cursor pages.
@changeset-bot

changeset-bot Bot commented Apr 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f5bbbf8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
emdash Patch
@emdash-cms/admin Patch
@emdash-cms/cloudflare Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Scope check

This PR changes 2,716 lines across 31 files. Large PRs are harder to review and more likely to be closed without review.

If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs.

See CONTRIBUTING.md for contribution guidelines.

@github-actions

Copy link
Copy Markdown
Contributor

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

File Note
packages/admin/src/locales/ar/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/de/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/en/messages.po Source changed, localizations will be marked as outdated.
packages/admin/src/locales/es-419/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/eu/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/fa/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/fr/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/ja/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/ko/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/pseudo/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/pt-BR/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/zh-CN/messages.po Localization changed, will be marked as complete.
packages/admin/src/locales/zh-TW/messages.po Localization changed, will be marked as complete.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

@pkg-pr-new

pkg-pr-new Bot commented Apr 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@749

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@749

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@749

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@749

emdash

npm i https://pkg.pr.new/emdash@749

create-emdash

npm i https://pkg.pr.new/create-emdash@749

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@749

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@749

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@749

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@749

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@749

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@749

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@749

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@749

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@749

commit: f5bbbf8

@edrpls

edrpls commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

Closing at my own request — this PR bundles three distinct fixes and is too large to review as one unit. Splitting into three independent PRs, each with its own changeset, tests, and scope so they can be reviewed/merged on their own cadence:

  1. Sortable column headers — SortableTh, aria-sort, server orderBy whitelist extension. Smallest, fully independent.
  2. Stable pagination denominator — total field on the list response; admin uses it. Independent.
  3. Server-side search — q query param with column introspection + LIKE escaping, picker migration, MCP tool. Largest surface.

Each will link to the others for context. All three share the same branch root and tests; no one depends on another being merged first, though the UX benefits compound. Thanks — and sorry for the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant