quota-panel.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import type { FC } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import type { ModelProvider } from '../declarations'
  4. import {
  5. CustomConfigurationStatusEnum,
  6. PreferredProviderTypeEnum,
  7. QuotaUnitEnum,
  8. } from '../declarations'
  9. import {
  10. useAnthropicBuyQuota,
  11. useFreeQuota,
  12. useUpdateModelProviders,
  13. } from '../hooks'
  14. import {
  15. MODEL_PROVIDER_QUOTA_GET_FREE,
  16. MODEL_PROVIDER_QUOTA_GET_PAID,
  17. } from '../utils'
  18. import PriorityUseTip from './priority-use-tip'
  19. import { InfoCircle } from '@/app/components/base/icons/src/vender/line/general'
  20. import Button from '@/app/components/base/button'
  21. import TooltipPlus from '@/app/components/base/tooltip-plus'
  22. import { formatNumber } from '@/utils/format'
  23. type QuotaPanelProps = {
  24. provider: ModelProvider
  25. }
  26. const QuotaPanel: FC<QuotaPanelProps> = ({
  27. provider,
  28. }) => {
  29. const { t } = useTranslation()
  30. const updateModelProviders = useUpdateModelProviders()
  31. const handlePay = useAnthropicBuyQuota()
  32. const handleFreeQuotaSuccess = () => {
  33. updateModelProviders()
  34. }
  35. const handleFreeQuota = useFreeQuota(handleFreeQuotaSuccess)
  36. const customConfig = provider.custom_configuration
  37. const priorityUseType = provider.preferred_provider_type
  38. const systemConfig = provider.system_configuration
  39. const currentQuota = systemConfig.enabled && systemConfig.quota_configurations.find(item => item.quota_type === systemConfig.current_quota_type)
  40. const openaiOrAnthropic = MODEL_PROVIDER_QUOTA_GET_PAID.includes(provider.provider)
  41. return (
  42. <div className='group relative shrink-0 min-w-[112px] px-3 py-2 rounded-lg bg-white/[0.3] border-[0.5px] border-black/5'>
  43. <div className='flex items-center mb-2 h-4 text-xs font-medium text-gray-500'>
  44. {t('common.modelProvider.quota')}
  45. <TooltipPlus popupContent={
  46. openaiOrAnthropic
  47. ? t('common.modelProvider.card.tip')
  48. : t('common.modelProvider.quotaTip')
  49. }>
  50. <InfoCircle className='ml-0.5 w-3 h-3 text-gray-400' />
  51. </TooltipPlus>
  52. </div>
  53. {
  54. currentQuota && (
  55. <div className='flex items-center h-4 text-xs text-gray-500'>
  56. <span className='mr-0.5 text-sm font-semibold text-gray-700'>{formatNumber((currentQuota?.quota_limit || 0) - (currentQuota?.quota_used || 0))}</span>
  57. {
  58. currentQuota?.quota_unit === QuotaUnitEnum.tokens && 'Tokens'
  59. }
  60. {
  61. currentQuota?.quota_unit === QuotaUnitEnum.times && t('common.modelProvider.callTimes')
  62. }
  63. {
  64. currentQuota?.quota_unit === QuotaUnitEnum.credits && t('common.modelProvider.credits')
  65. }
  66. </div>
  67. )
  68. }
  69. {
  70. !currentQuota && MODEL_PROVIDER_QUOTA_GET_FREE.includes(provider.provider) && (
  71. <Button
  72. className='h-6 bg-white text-xs font-medium rounded-md'
  73. onClick={() => handleFreeQuota(provider.provider)}
  74. >
  75. {t('common.modelProvider.getFreeTokens')}
  76. </Button>
  77. )
  78. }
  79. {
  80. priorityUseType === PreferredProviderTypeEnum.system && customConfig.status === CustomConfigurationStatusEnum.active && (
  81. <PriorityUseTip />
  82. )
  83. }
  84. </div>
  85. )
  86. }
  87. export default QuotaPanel