Skip to content

fix: add server-side sorting to user list, prevent client-side sort on paged data#6194

Open
DawnMoon1542 wants to merge 2 commits into
QuantumNous:mainfrom
DawnMoon1542:fix/user-list-server-side-sorting
Open

fix: add server-side sorting to user list, prevent client-side sort on paged data#6194
DawnMoon1542 wants to merge 2 commits into
QuantumNous:mainfrom
DawnMoon1542:fix/user-list-server-side-sorting

Conversation

@DawnMoon1542

@DawnMoon1542 DawnMoon1542 commented Jul 14, 2026

Copy link
Copy Markdown

Note

This PR was authored with AI assistance.

📝 变更描述 / Description

The user management table applied client-side sorting to the current page slice while pagination was handled server-side, causing ID-asc views to show pages out of order (e.g. 23-42, 3-22, 1-2).

  • Backend: GetAllUsers and SearchUsers now accept sort_by / sort_order query params with a column whitelist generating safe ORDER BY via GORM clause.OrderByColumn.
  • Frontend: users-table passes sorting state to the API and resets to page 1 on sort change.
  • Generic useDataTable hook disables the client-side sorted row model and sort UI for any table with manualPagination but no server-side sort handler, preventing the same class of bug elsewhere.

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)

🔗 关联任务 / Related Issue

  • N/A

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • Bug fix 说明: No existing issue filed; discovered during development.
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

=== RUN   TestGetAllUsersSortsBeforePagination
--- PASS: TestGetAllUsersSortsBeforePagination (0.00s)
=== RUN   TestSearchUsersSortsBeforePagination
--- PASS: TestSearchUsersSortsBeforePagination (0.00s)
PASS
ok  	github.com/QuantumNous/new-api/model	0.861s

Summary by CodeRabbit

  • New Features
    • Added server-side sorting for user lists and user searches.
    • Users can sort by supported fields (ascending/descending) using URL-backed parameters.
    • Sorting integrates with the data table UI and updates results while returning to the first page on sort changes.
  • Bug Fixes
    • Sorting is consistently applied before pagination to ensure correct page ordering.
    • Improved table behavior when using server-side pagination.
  • Tests
    • Added coverage to verify sorting order is respected across paged results for both list and search.

…n paged data

The user management table applied client-side sorting to the current
page slice while pagination was handled server-side, causing ID-asc
views to show pages out of order (e.g. 23-42, 3-22, 1-2).

Backend: add sort_by/sort_order query params to GetAllUsers and
SearchUsers with a column whitelist for safe ORDER BY generation.
Frontend: pass sorting state to the API and reset to page 1 on sort
change. Generic useDataTable hook now disables client-side sorted row
model and sort UI for tables with manual pagination but no server-side
sort handler.
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a36e6b92-b943-49a2-97ee-d185701e968b

📥 Commits

Reviewing files that changed from the base of the PR and between 19453c8 and 2dcc76a.

📒 Files selected for processing (2)
  • model/user.go
  • web/default/src/features/users/components/users-table.tsx
🚧 Files skipped from review as they are similar to previous changes (2)
  • model/user.go
  • web/default/src/features/users/components/users-table.tsx

Walkthrough

User list and search now support validated server-side sorting. The users table tracks sorting state, sends sort parameters through the API, and resets pagination when sorting changes. Backend queries apply ordering before pagination, with tests covering ordered pages.

Changes

User sorting flow

Layer / File(s) Summary
Backend sorting contract and pagination
model/user.go, model/user_pagination_test.go
Adds validated user sort options, applies dynamic ordering before pagination, and tests ordered list and search pages.
Sorting request contracts and transport
web/default/src/features/users/types.ts, web/default/src/features/users/api.ts, controller/user.go
Defines typed sorting parameters, forwards them through list/search requests, and passes normalized options to model queries.
Users table server-side sorting
web/default/src/features/users/components/users-table.tsx
Tracks sortable columns, includes sorting in query keys and API parameters, and resets pagination when sorting changes.
Shared table sorting behavior
web/default/src/components/data-table/hooks/use-data-table.ts, web/default/src/features/models/components/models-table.tsx
Adds explicit sorting enablement, disables local sorted rows with manual pagination, and removes manual sorting from ModelsTable.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant UsersTable
  participant UsersAPI
  participant UserController
  participant UserModel
  participant Database
  User->>UsersTable: Select column sort
  UsersTable->>UsersAPI: Request sort_by and sort_order
  UsersAPI->>UserController: Send sorted list/search request
  UserController->>UserModel: Construct and pass UserSortOptions
  UserModel->>Database: Order results before pagination
  Database-->>UsersTable: Return ordered users
Loading

Poem

A rabbit sorts the rows in line,
Ascending carrots, fields align.
Pages hop back to page one,
Queries carry sorting fun.
The database follows the sign!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: server-side sorting for the user list and disabling client-side sorting on paged data.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@model/user.go`:
- Around line 52-61: Update UserSortOptions.Apply to append a secondary
ascending sort by the unique id column after the requested sort, while
preserving the selected column and sort direction as the primary ordering.

In `@web/default/src/features/users/components/users-table.tsx`:
- Around line 115-123: Update handleSortingChange so the setSorting updater only
computes and returns the next sorting state; move the conditional
onPaginationChange call outside that updater closure while preserving the page
reset when pagination.pageIndex is greater than zero.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f6056100-43a4-403b-8c8e-2e37e161a306

📥 Commits

Reviewing files that changed from the base of the PR and between b6b97a6 and 19453c8.

📒 Files selected for processing (8)
  • controller/user.go
  • model/user.go
  • model/user_pagination_test.go
  • web/default/src/components/data-table/hooks/use-data-table.ts
  • web/default/src/features/models/components/models-table.tsx
  • web/default/src/features/users/api.ts
  • web/default/src/features/users/components/users-table.tsx
  • web/default/src/features/users/types.ts
💤 Files with no reviewable changes (1)
  • web/default/src/features/models/components/models-table.tsx

Comment thread model/user.go
Comment thread web/default/src/features/users/components/users-table.tsx
…t from state updater

Append secondary ORDER BY id DESC when sorting by non-unique columns
(quota, created_at, etc.) to prevent row duplication/skipping across
OFFSET pages.

Move onPaginationChange out of setSorting updater to avoid side effects
inside a pure function (React Strict Mode double-invocation safety).
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