Track admin users pagination in frontend data layer

This commit is contained in:
RWDai
2026-05-20 16:51:46 +08:00
parent 5130da9710
commit 1be703b56e
2 changed files with 48 additions and 6 deletions

View File

@@ -3,7 +3,7 @@ import { cachedRequest } from '@/utils/cache'
import type { UserSession as SessionRecord } from '@/types/session'
import type { BillingPlan, UserPlanEntitlement } from './billing'
export type UserRole = 'admin' | 'user'
export type UserRole = 'admin' | 'audit_admin' | 'user'
export type ListPolicyMode = 'inherit' | 'unrestricted' | 'specific' | 'deny_all'
export type RateLimitPolicyMode = 'inherit' | 'system' | 'custom'
export type FeatureSettings = Record<string, unknown>
@@ -264,8 +264,29 @@ export interface GetAllUsersOptions {
cacheTtlMs?: number
}
export interface AdminUsersListResponse {
items: User[]
total: number
skip: number
limit: number
has_more: boolean
}
function normalizeAdminUsersListResponse(payload: User[] | AdminUsersListResponse): AdminUsersListResponse {
if (Array.isArray(payload)) {
return {
items: payload,
total: payload.length,
skip: 0,
limit: payload.length,
has_more: false,
}
}
return payload
}
export const usersApi = {
async getAllUsers(options: GetAllUsersOptions = {}): Promise<User[]> {
async getAllUsersPage(options: GetAllUsersOptions = {}): Promise<AdminUsersListResponse> {
const cacheTtlMs = options.cacheTtlMs ?? 0
const params: Record<string, string | number> = {}
const search = options.search?.trim()
@@ -292,15 +313,20 @@ export const usersApi = {
return cachedRequest(
cacheKey,
async () => {
const response = await apiClient.get<User[]>('/api/admin/users', {
const response = await apiClient.get<User[] | AdminUsersListResponse>('/api/admin/users', {
params: Object.keys(params).length > 0 ? params : undefined,
})
return response.data
return normalizeAdminUsersListResponse(response.data)
},
cacheTtlMs,
)
},
async getAllUsers(options: GetAllUsersOptions = {}): Promise<User[]> {
const response = await this.getAllUsersPage(options)
return response.items
},
async getUser(userId: string): Promise<User> {
const response = await apiClient.get<User>(`/api/admin/users/${userId}`)
return response.data