Skip to content

Commit 6f54337

Browse files
committed
improvement(queries): clean up admin-users hooks per React Query best practices
- Remove unsafe unknown/Record casting, use better-auth typed response - Add placeholderData: keepPreviousData for paginated variable-key query - Remove nullable types where defaults are always applied
1 parent 2b6cb7e commit 6f54337

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

apps/sim/hooks/queries/admin-users.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createLogger } from '@sim/logger'
2-
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
2+
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
33
import { client } from '@/lib/auth/auth-client'
44

55
const logger = createLogger('AdminUsersQuery')
@@ -14,8 +14,8 @@ interface AdminUser {
1414
id: string
1515
name: string
1616
email: string
17-
role: string | null
18-
banned: boolean | null
17+
role: string
18+
banned: boolean
1919
banReason: string | null
2020
}
2121

@@ -29,21 +29,18 @@ async function fetchAdminUsers(offset: number, limit: number): Promise<AdminUser
2929
query: { limit, offset },
3030
})
3131
if (error) {
32-
throw new Error((error as { message?: string }).message || 'Failed to fetch users')
32+
throw new Error(error.message ?? 'Failed to fetch users')
3333
}
3434
return {
35-
users: ((data as { users?: unknown[] })?.users ?? []).map((u: unknown) => {
36-
const user = u as Record<string, unknown>
37-
return {
38-
id: user.id as string,
39-
name: (user.name as string) || '',
40-
email: (user.email as string) || '',
41-
role: (user.role as string) ?? 'user',
42-
banned: (user.banned as boolean) ?? false,
43-
banReason: (user.banReason as string) ?? null,
44-
}
45-
}),
46-
total: (data as { total?: number })?.total ?? 0,
35+
users: (data?.users ?? []).map((u) => ({
36+
id: u.id,
37+
name: u.name || '',
38+
email: u.email,
39+
role: u.role ?? 'user',
40+
banned: u.banned ?? false,
41+
banReason: u.banReason ?? null,
42+
})),
43+
total: data?.total ?? 0,
4744
}
4845
}
4946

@@ -53,6 +50,7 @@ export function useAdminUsers(offset: number, limit: number, enabled: boolean) {
5350
queryFn: () => fetchAdminUsers(offset, limit),
5451
enabled,
5552
staleTime: 30 * 1000,
53+
placeholderData: keepPreviousData,
5654
})
5755
}
5856

0 commit comments

Comments
 (0)