FreeQuota.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { useState } from 'react'
  2. import type { FC } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import type { ProviderConfigItem, TypeWithI18N } from '../declarations'
  6. import { ProviderEnum as ProviderEnumValue } from '../declarations'
  7. import s from './index.module.css'
  8. import I18n from '@/context/i18n'
  9. import Button from '@/app/components/base/button'
  10. import { submitFreeQuota } from '@/service/common'
  11. import { LinkExternal01 } from '@/app/components/base/icons/src/vender/line/general'
  12. const TIP_MAP: { [k: string]: TypeWithI18N } = {
  13. [ProviderEnumValue.minimax]: {
  14. 'en': 'Earn 1 million tokens for free',
  15. 'zh-Hans': '免费获取 100 万个 token',
  16. },
  17. [ProviderEnumValue.spark]: {
  18. 'en': 'Earn 3 million tokens for free',
  19. 'zh-Hans': '免费获取 300 万个 token',
  20. },
  21. [ProviderEnumValue.zhipuai]: {
  22. 'en': 'Earn 10 million tokens for free',
  23. 'zh-Hans': '免费获取 1000 万个 token',
  24. },
  25. }
  26. type FreeQuotaProps = {
  27. modelItem: ProviderConfigItem
  28. onUpdate: () => void
  29. }
  30. const FreeQuota: FC<FreeQuotaProps> = ({
  31. modelItem,
  32. onUpdate,
  33. }) => {
  34. const { locale } = useContext(I18n)
  35. const { t } = useTranslation()
  36. const [loading, setLoading] = useState(false)
  37. const handleClick = async () => {
  38. try {
  39. setLoading(true)
  40. const res = await submitFreeQuota(`/workspaces/current/model-providers/${modelItem.key}/free-quota-submit`)
  41. if (res.type === 'redirect' && res.redirect_url)
  42. window.location.href = res.redirect_url
  43. else if (res.type === 'submit' && res.result === 'success')
  44. onUpdate()
  45. }
  46. finally {
  47. setLoading(false)
  48. }
  49. }
  50. return (
  51. <div className='flex items-center'>
  52. 📣
  53. <div className={`${s.vender} ml-1 text-xs font-medium text-transparent`}>{TIP_MAP[modelItem.key][locale]}</div>
  54. <div className='mx-1 text-xs font-medium text-gray-400'>·</div>
  55. <a
  56. href='https://docs.dify.ai/v/zh-hans/getting-started/faq/llms-use-faq#8.-ru-he-mian-fei-shen-ling-xun-fei-xing-huo-minimax-mo-xing-de-ti-yanedu'
  57. target='_blank'
  58. className='flex items-center text-xs font-medium text-[#155EEF]'>
  59. {t('common.modelProvider.freeQuota.howToEarn')}
  60. <LinkExternal01 className='ml-0.5 w-3 h-3' />
  61. </a>
  62. <Button
  63. type='primary'
  64. className='ml-3 !px-3 !h-7 !rounded-md !text-xs !font-medium'
  65. onClick={handleClick}
  66. disabled={loading}
  67. >
  68. {t('common.operation.getForFree')}
  69. </Button>
  70. <div className='mx-2 w-[1px] h-4 bg-black/5' />
  71. </div>
  72. )
  73. }
  74. export default FreeQuota