forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-pricing-data.ts
More file actions
78 lines (65 loc) · 2.25 KB
/
Copy pathuse-pricing-data.ts
File metadata and controls
78 lines (65 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
import { useStatus } from '@/hooks/use-status'
import { getPricing } from '../api'
export function usePricingData() {
const { status } = useStatus()
const { data, isLoading, error, refetch } = useQuery({
queryKey: ['pricing'],
queryFn: getPricing,
staleTime: 5 * 60 * 1000,
})
// Ensure rates never reach zero to prevent division errors
const priceRate = useMemo(
() => Math.max((status?.price as number) ?? 1, 0.001),
[status?.price]
)
const usdExchangeRate = useMemo(
() => Math.max((status?.usd_exchange_rate as number) ?? priceRate, 0.001),
[status?.usd_exchange_rate, priceRate]
)
const models = useMemo(() => {
if (!data?.data || !data?.vendors) return []
const vendorMap = new Map(data.vendors.map((v) => [v.id, v]))
return data.data.map((model) => {
const vendor = model.vendor_id
? vendorMap.get(model.vendor_id)
: undefined
return {
...model,
key: model.model_name,
vendor_name: vendor?.name,
vendor_icon: vendor?.icon,
vendor_description: vendor?.description,
group_ratio: data.group_ratio,
}
})
}, [data])
return {
models,
vendors: data?.vendors ?? [],
groupRatio: data?.group_ratio ?? {},
usableGroup: data?.usable_group ?? {},
endpointMap: data?.supported_endpoint ?? {},
autoGroups: data?.auto_groups ?? [],
isLoading,
error,
refetch,
priceRate,
usdExchangeRate,
}
}