Admin tables and CSV
For Admin panel only projects, the login is /, the home page is /dashboard, and other panel pages omit the /admin prefix. Components use @/components and @/components/ui. API endpoints such as /api/admin/users/export keep their paths. Examples below use the combined public website + admin panel layout; generated standalone guides use the app’s actual paths.
Reusable admin tables
The Users page is a working reference for a searchable, sortable list with URL state.
Reuse the existing pieces
| Source | Purpose |
|---|---|
src/lib/table-query.ts |
Base Zod schema, allowed sizes, and URL helpers |
src/lib/users-query.ts |
User-specific role and sort validation |
DataTable |
Columns, rows, and initial row skeletons |
DataTableSearch |
Debounced search |
DataTablePagination |
Page and size controls |
useDataTableState |
URL synchronization |
Extend the schema for another entity and keep its columns/query separate. Validate authorization and select only display fields in each list endpoint.
URL behavior
- Search waits 450 ms and replaces the current history entry.
- Filters, sort order, and page size reset pagination together and push history.
- Back/Forward restores state without a server navigation.
- Unrelated URL parameters are preserved; default values are omitted.
- Out-of-range pages are clamped by the server.
Private query behavior
Users queries wait for the server-verified administrator identity and browser URL. Query keys include that identity and the filters.
The list keeps data fresh for one minute, removes unused queries after five minutes, supports cancellation, and disables automatic retries. Stale results refresh on mount, focus, or reconnect. Extra page prefetching is unnecessary.
Initial skeletons cover cells and unknown totals at the requested page size. Static table controls remain visible; background updates preserve existing rows and counts.
Export filtered CSV
On /admin/users, click the export control to download all matching users. It respects the current search, role, and sorting, without limiting the result to the visible page.
The export route is /api/admin/users/export. It authorizes the administrator independently and reads batches of 500 rows.
To add export to another list:
- Share its selection, filters, order, and cursor predicates between the list and export. Users does this in
src/trpc/routers/users-data.ts. - Define an export input schema without pagination. Users uses
usersExportQuerySchema. - Build the response with
createCsvResponsefromsrc/lib/csv.ts. - Reuse
DataTableExportfor the download control.
Export does not change records or populate the TanStack query cache. Preserve existing content and sanitized errors if a download fails.