FreeQuota.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  22. type FreeQuotaProps = {
  23. modelItem: ProviderConfigItem
  24. onUpdate: () => void
  25. }
  26. const FreeQuota: FC<FreeQuotaProps> = ({
  27. modelItem,
  28. onUpdate,
  29. }) => {
  30. const { locale } = useContext(I18n)
  31. const { t } = useTranslation()
  32. const [loading, setLoading] = useState(false)
  33. const handleClick = async () => {
  34. try {
  35. setLoading(true)
  36. const res = await submitFreeQuota(`/workspaces/current/model-providers/${modelItem.key}/free-quota-submit`)
  37. if (res.type === 'redirect' && res.redirect_url)
  38. window.location.href = res.redirect_url
  39. else if (res.type === 'submit' && res.result === 'success')
  40. onUpdate()
  41. }
  42. finally {
  43. setLoading(false)
  44. }
  45. }
  46. return (
  47. <div className='flex items-center'>
  48. 📣
  49. <div className={`${s.vender} ml-1 text-xs font-medium text-transparent`}>{TIP_MAP[modelItem.key][locale]}</div>
  50. <div className='mx-1 text-xs font-medium text-gray-400'>·</div>
  51. <a
  52. 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'
  53. target='_blank'
  54. className='flex items-center text-xs font-medium text-[#155EEF]'>
  55. {t('common.modelProvider.freeQuota.howToEarn')}
  56. <LinkExternal01 className='ml-0.5 w-3 h-3' />
  57. </a>
  58. <Button
  59. type='primary'
  60. className='ml-3 !px-3 !h-7 !rounded-md !text-xs !font-medium'
  61. onClick={handleClick}
  62. disabled={loading}
  63. >
  64. {t('common.operation.getForFree')}
  65. </Button>
  66. <div className='mx-2 w-[1px] h-4 bg-black/5' />
  67. </div>
  68. )
  69. }
  70. export default FreeQuota