Setting.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import type { FC } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useContext } from 'use-context-selector'
  4. import type { FormValue, Provider, ProviderConfigItem, ProviderWithConfig, ProviderWithQuota } from '../declarations'
  5. import { ProviderEnum } from '../declarations'
  6. import Indicator from '../../../indicator'
  7. import Selector from '../selector'
  8. import FreeQuota from './FreeQuota'
  9. import I18n from '@/context/i18n'
  10. import Button from '@/app/components/base/button'
  11. import { IS_CE_EDITION } from '@/config'
  12. type SettingProps = {
  13. currentProvider?: Provider
  14. modelItem: ProviderConfigItem
  15. onOpenModal: (v?: FormValue) => void
  16. onOperate: (v: Record<string, any>) => void
  17. onUpdate: () => void
  18. }
  19. const Setting: FC<SettingProps> = ({
  20. currentProvider,
  21. modelItem,
  22. onOpenModal,
  23. onOperate,
  24. onUpdate,
  25. }) => {
  26. const { locale } = useContext(I18n)
  27. const { t } = useTranslation()
  28. const configurable = currentProvider?.model_flexibility === 'configurable'
  29. const systemFree = currentProvider?.providers.find(p => p.provider_type === 'system' && (p as ProviderWithQuota).quota_type === 'free') as ProviderWithQuota
  30. const custom = currentProvider?.providers.find(p => p.provider_type === 'custom') as ProviderWithConfig
  31. return (
  32. <div className='flex items-center'>
  33. {
  34. (modelItem.key === ProviderEnum.minimax || modelItem.key === ProviderEnum.spark) && systemFree && !systemFree?.is_valid && !IS_CE_EDITION && (
  35. <FreeQuota
  36. modelItem={modelItem}
  37. freeProvider={systemFree}
  38. onUpdate={onUpdate}
  39. />
  40. )
  41. }
  42. {
  43. modelItem.disable && !IS_CE_EDITION && (
  44. <div className='flex items-center text-xs text-gray-500'>
  45. {modelItem.disable.tip[locale]}
  46. <a
  47. className={`${locale === 'en' && 'ml-1'} text-primary-600 cursor-pointer`}
  48. href={modelItem.disable.link.href[locale]}
  49. target='_blank'
  50. >
  51. {modelItem.disable.link.label[locale]}
  52. </a>
  53. <div className='mx-2 w-[1px] h-4 bg-black/5' />
  54. </div>
  55. )
  56. }
  57. {
  58. configurable && (
  59. <Button
  60. className={`!px-3 !h-7 rounded-md bg-white !text-xs font-medium text-gray-700 ${!!modelItem.disable && '!text-gray-300'}`}
  61. onClick={() => onOpenModal()}
  62. >
  63. {t('common.operation.add')}
  64. </Button>
  65. )
  66. }
  67. {
  68. !configurable && custom?.config && (
  69. <div className='flex items-center'>
  70. <Indicator className='mr-3' />
  71. <Button
  72. className='mr-1 !px-3 !h-7 rounded-md bg-white !text-xs font-medium text-gray-700'
  73. onClick={() => onOpenModal(custom.config)}
  74. >
  75. {t('common.operation.edit')}
  76. </Button>
  77. <Selector
  78. hiddenOptions={!systemFree?.is_valid || IS_CE_EDITION}
  79. value={currentProvider?.preferred_provider_type}
  80. onOperate={onOperate}
  81. className={open => `${open && '!bg-gray-100 shadow-none'} flex justify-center items-center w-7 h-7 bg-white rounded-md border-[0.5px] border-gray-200 shadow-xs cursor-pointer hover:bg-gray-100`}
  82. />
  83. </div>
  84. )
  85. }
  86. {
  87. !configurable && !custom?.config && (
  88. <Button
  89. className={`!px-3 !h-7 rounded-md bg-white !text-xs font-medium text-gray-700 ${!!modelItem.disable && !IS_CE_EDITION && '!text-gray-300'}`}
  90. onClick={() => onOpenModal()}
  91. disabled={!!modelItem.disable && !IS_CE_EDITION}
  92. >
  93. {t('common.operation.setup')}
  94. </Button>
  95. )
  96. }
  97. </div>
  98. )
  99. }
  100. export default Setting