quota-panel.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. </div>
  64. )
  65. }
  66. {
  67. !currentQuota && provider.provider === 'anthropic' && (
  68. <Button
  69. className='h-6 bg-white text-xs font-medium rounded-md'
  70. onClick={handlePay}
  71. >
  72. {t('common.modelProvider.buyQuota')}
  73. </Button>
  74. )
  75. }
  76. {
  77. !currentQuota && MODEL_PROVIDER_QUOTA_GET_FREE.includes(provider.provider) && (
  78. <Button
  79. className='h-6 bg-white text-xs font-medium rounded-md'
  80. onClick={() => handleFreeQuota(provider.provider)}
  81. >
  82. {t('common.modelProvider.getFreeTokens')}
  83. </Button>
  84. )
  85. }
  86. {
  87. provider.provider === 'anthropic' && systemConfig.enabled && (
  88. <div
  89. className={`
  90. absolute left-0 bottom-0 hidden group-hover:flex items-center justify-center
  91. w-full h-[30px] backdrop-blur-[2px] bg-gradient-to-r from-[rgba(238,244,255,0.80)] to-[rgba(237,237,240,0.70)]
  92. text-xs font-medium text-primary-600 cursor-pointer rounded-b-lg
  93. `}
  94. onClick={handlePay}
  95. >
  96. {t('common.modelProvider.buyQuota')}
  97. </div>
  98. )
  99. }
  100. {
  101. priorityUseType === PreferredProviderTypeEnum.system && customConfig.status === CustomConfigurationStatusEnum.active && (
  102. <PriorityUseTip />
  103. )
  104. }
  105. </div>
  106. )
  107. }
  108. export default QuotaPanel