Skip to content

Sort the admin account list on the server - #297

Merged
dillonstreator merged 4 commits into
mainfrom
claude/server-driven-table-sorting-9881e5
Nov 18, 2026
Merged

dillonstreator merged 4 commits into
mainfrom
claude/server-driven-table-sorting-9881e5

Conversation

@dillonstreator

Copy link
Copy Markdown
Member

Problem

The sortable column headers on the admin Users page reordered only the rows already loaded (50 per page). Sorting by "Stored, biggest first" showed the biggest of the first page as if it were the biggest overall, and "Load more" appended rows out of that order.

Repro: have more accounts than one page, sort by Stored or Active, and compare the top rows with the same order done in SQL.

Fix

The sort is now part of the list query, applied by the database to every match:

  • admin.users.list / groups take sort (name, status, projects, sourceMinutes, clipsRendered, storedBytes, lastActive, created) and dir. Each column is one SQL value per account (USER_SORT_SQL), paged by a keyset on (value, id). The cursor names its sort, so a cursor from another order is refused. Search with a sort pages over every match; without a sort, search still returns the best matches.
  • Web: the headers change the query instead of sorting rows in the browser. The sort is in the hash (#sort=storedBytes&dir=desc), and grouped sections page in the same order.
  • MCP / CLI: list_users takes sort and dir; reelsaw users takes --sort and --dir. The generated docs tables are refreshed.

Closing the class

  • A new "Sorting happens in the database" rule in docs/PRINCIPLES.md (Scale), with a pointer in AGENTS.md and on ListColumn.sort.
  • apps/server/test/list-sorting.test.ts (stack-free) fails when a web component that pages with useInfiniteQuery also calls .sort(/.toSorted(. The old Users page fails it.
  • The Users table was the only paged table that sorted in the browser. The other browser sorts are on bounded lists that are loaded whole (a project's clips, timelines).

Notes

  • The all-time usage values are written twice: in USER_SORT_SQL for sorting and in usageByUser for the numbers on each row. Comments in both places say to change them together.
  • Sorting by a usage column works out that total for every matching account. That is fine for an admin-only list; revisit if it gets slow with many accounts.

Testing

  • Server and web typecheck, web build, pnpm lint, docs:check, and the new guard test pass.
  • Against a local Postgres: every key in both directions, paged 3 at a time, came back in order with no duplicates. Search with a sort pages, a cursor from another sort is refused, and grouped sections use the sort.
  • Deployed locally and checked in the browser: a link with a sort sends it to the server; Account cycles A→Z, Z→A, then the default order, and the hash follows; Active sorts newest first; grouped sections come back in the sorted order.

Column headers on the Users page reordered only the rows already loaded, so a
sort showed the top of one page as if it were the whole list. The sort is now a
list input (sort, dir) applied in SQL to every match and paged by a keyset on
(sort value, id); the web page, list_users and reelsaw users share it, and the
sort lives in the page hash. PRINCIPLES gains "Sorting happens in the
database", guarded by a test that fails when a paged component sorts rows in
the browser.

Signed-off-by: dillonstreator <dillonstreator@gmail.com>
@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
reelsaw-website Ready Ready Preview Nov 18, 2026 7:12pm UTC

Request Review

Signed-off-by: dillonstreator <dillonstreator@gmail.com>
- The usage sorts reuse allTimeUsageSql in quota.ts; storedBytes() and the sort
  share storedBytesSql, so the quota and the order cannot count differently.
- projects_upload_id_idx serves the "upload copied yet" lookup the Stored sort,
  the quota and upload cleanup run.
- ORDER BY names the selected sort_value, so it is computed once per row.
- lastActive is one SQL value (UTC) for both the shown time and the sort.
- dir without sort is refused on every surface; the sort input says the usage
  keys order by usage.*.
- reelsaw users --sort prints the column it sorted by.
- users-sort-stack.test.ts pages every key both ways against the shown values;
  the guard test also covers nextCursor lists and sortable ListColumn headers.

Signed-off-by: dillonstreator <dillonstreator@gmail.com>
…table-sorting-9881e5

Signed-off-by: dillonstreator <dillonstreator@gmail.com>

# Conflicts:
#	apps/server/drizzle/meta/0059_snapshot.json
#	apps/server/drizzle/meta/_journal.json
@dillonstreator
dillonstreator merged commit f0e2af2 into main Nov 18, 2026
15 checks passed
@dillonstreator dillonstreator mentioned this pull request Nov 18, 2026
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.

1 participant