Setting.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 && locale === 'zh-Hans' && (
  35. <FreeQuota
  36. modelItem={modelItem}
  37. onUpdate={onUpdate}
  38. />
  39. )
  40. }
  41. {
  42. modelItem.disable && !IS_CE_EDITION && (
  43. <div className='flex items-center text-xs text-gray-500'>
  44. {modelItem.disable.tip[locale]}
  45. <a
  46. className={`${locale === 'en' && 'ml-1'} text-primary-600 cursor-pointer`}
  47. href={modelItem.disable.link.href[locale]}
  48. target='_blank'
  49. >
  50. {modelItem.disable.link.label[locale]}
  51. </a>
  52. <div className='mx-2 w-[1px] h-4 bg-black/5' />
  53. </div>
  54. )
  55. }
  56. {
  57. configurable && (
  58. <Button
  59. className={`!px-3 !h-7 rounded-md bg-white !text-xs font-medium text-gray-700 ${!!modelItem.disable && '!text-gray-300'}`}
  60. onClick={() => onOpenModal()}
  61. >
  62. {t('common.operation.add')}
  63. </Button>
  64. )
  65. }
  66. {
  67. !configurable && custom?.config && (
  68. <div className='flex items-center'>
  69. <Indicator className='mr-3' />
  70. <Button
  71. className='mr-1 !px-3 !h-7 rounded-md bg-white !text-xs font-medium text-gray-700'
  72. onClick={() => onOpenModal(custom.config)}
  73. >
  74. {t('common.operation.edit')}
  75. </Button>
  76. <Selector
  77. hiddenOptions={!systemFree?.is_valid || IS_CE_EDITION}
  78. value={currentProvider?.preferred_provider_type}
  79. onOperate={onOperate}
  80. 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`}
  81. />
  82. </div>
  83. )
  84. }
  85. {
  86. !configurable && !custom?.config && (
  87. <Button
  88. className={`!px-3 !h-7 rounded-md bg-white !text-xs font-medium text-gray-700 ${!!modelItem.disable && !IS_CE_EDITION && '!text-gray-300'}`}
  89. onClick={() => onOpenModal()}
  90. disabled={!!modelItem.disable && !IS_CE_EDITION}
  91. >
  92. {t('common.operation.setup')}
  93. </Button>
  94. )
  95. }
  96. </div>
  97. )
  98. }
  99. export default Setting