Setting.tsx 3.3 KB

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