fix: use getUserGroups for ratio display to respect GroupGroupRatio (#4772)
fix(web/default): use getUserGroups for ratio display to respect GroupGroupRatio Unifies admin/user ratio display so API key list matches the actual billing ratio.
This commit is contained in:
parent
3057f04a17
commit
7fe896d2f8
@ -16,11 +16,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
|
|
||||||
For commercial licensing, please contact support@quantumnous.com
|
For commercial licensing, please contact support@quantumnous.com
|
||||||
*/
|
*/
|
||||||
import { useMemo } from 'react'
|
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { type ColumnDef } from '@tanstack/react-table'
|
import { type ColumnDef } from '@tanstack/react-table'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { useAuthStore } from '@/stores/auth-store'
|
|
||||||
import { getUserGroups } from '@/lib/api'
|
import { getUserGroups } from '@/lib/api'
|
||||||
import { formatQuota, formatTimestampToDate } from '@/lib/format'
|
import { formatQuota, formatTimestampToDate } from '@/lib/format'
|
||||||
import { cn } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
@ -34,7 +32,6 @@ import {
|
|||||||
import { DataTableColumnHeader } from '@/components/data-table'
|
import { DataTableColumnHeader } from '@/components/data-table'
|
||||||
import { GroupBadge } from '@/components/group-badge'
|
import { GroupBadge } from '@/components/group-badge'
|
||||||
import { StatusBadge } from '@/components/status-badge'
|
import { StatusBadge } from '@/components/status-badge'
|
||||||
import { getSystemOptions } from '@/features/system-settings/api'
|
|
||||||
import { API_KEY_STATUSES } from '../constants'
|
import { API_KEY_STATUSES } from '../constants'
|
||||||
import { type ApiKey } from '../types'
|
import { type ApiKey } from '../types'
|
||||||
import {
|
import {
|
||||||
@ -51,31 +48,9 @@ function getQuotaProgressColor(percentage: number): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function useGroupRatios(): Record<string, number> {
|
function useGroupRatios(): Record<string, number> {
|
||||||
const isAdmin = useAuthStore((s) =>
|
const { data } = useQuery({
|
||||||
Boolean(s.auth.user?.role && s.auth.user.role >= 10)
|
|
||||||
)
|
|
||||||
|
|
||||||
const { data: adminData } = useQuery({
|
|
||||||
queryKey: ['system-options-group-ratio'],
|
|
||||||
queryFn: getSystemOptions,
|
|
||||||
enabled: isAdmin,
|
|
||||||
staleTime: 5 * 60 * 1000,
|
|
||||||
select: (res) => {
|
|
||||||
if (!res.success || !res.data) return {}
|
|
||||||
const option = res.data.find((o) => o.key === 'GroupRatio')
|
|
||||||
if (!option?.value) return {}
|
|
||||||
try {
|
|
||||||
return JSON.parse(option.value) as Record<string, number>
|
|
||||||
} catch {
|
|
||||||
return {}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const { data: userGroupsData } = useQuery({
|
|
||||||
queryKey: ['user-self-groups'],
|
queryKey: ['user-self-groups'],
|
||||||
queryFn: getUserGroups,
|
queryFn: getUserGroups,
|
||||||
enabled: !isAdmin,
|
|
||||||
staleTime: 5 * 60 * 1000,
|
staleTime: 5 * 60 * 1000,
|
||||||
select: (res) => {
|
select: (res) => {
|
||||||
if (!res.success || !res.data) return {}
|
if (!res.success || !res.data) return {}
|
||||||
@ -89,10 +64,7 @@ function useGroupRatios(): Record<string, number> {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return useMemo(
|
return data ?? {}
|
||||||
() => (isAdmin ? adminData : userGroupsData) ?? {},
|
|
||||||
[isAdmin, adminData, userGroupsData]
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useApiKeysColumns(): ColumnDef<ApiKey>[] {
|
export function useApiKeysColumns(): ColumnDef<ApiKey>[] {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user