index.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import type { FC } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useContext } from 'use-context-selector'
  4. import type {
  5. FormValue,
  6. Provider,
  7. ProviderConfigItem,
  8. ProviderWithConfig,
  9. } from '../declarations'
  10. import Indicator from '../../../indicator'
  11. import Selector from '../selector'
  12. import Quota from './Quota'
  13. import { IS_CE_EDITION } from '@/config'
  14. import I18n from '@/context/i18n'
  15. import { Plus } from '@/app/components/base/icons/src/vender/line/general'
  16. type ModelCardProps = {
  17. currentProvider?: Provider
  18. modelItem: ProviderConfigItem
  19. onOpenModal: (v?: FormValue) => void
  20. onOperate: (v: Record<string, any>) => void
  21. }
  22. const ModelCard: FC<ModelCardProps> = ({
  23. currentProvider,
  24. modelItem,
  25. onOpenModal,
  26. onOperate,
  27. }) => {
  28. const { locale } = useContext(I18n)
  29. const { t } = useTranslation()
  30. const custom = currentProvider?.providers.find(p => p.provider_type === 'custom') as ProviderWithConfig
  31. return (
  32. <div className='rounded-xl border-[0.5px] border-gray-200 shadow-xs'>
  33. <div className={`flex px-4 pt-4 pb-3 rounded-t-lg ${modelItem.bgColor}`}>
  34. <div className='grow mr-3'>
  35. <div className='mb-1'>
  36. {modelItem.titleIcon[locale]}
  37. </div>
  38. <div className='h-9 text-xs text-black opacity-60'>{modelItem.desc?.[locale]}</div>
  39. </div>
  40. {modelItem.subTitleIcon}
  41. </div>
  42. {
  43. !IS_CE_EDITION && currentProvider && <Quota currentProvider={currentProvider} />
  44. }
  45. {
  46. custom?.is_valid
  47. ? (
  48. <div className='flex items-center px-4 h-12'>
  49. <Indicator color='green' className='mr-2' />
  50. <div className='grow text-[13px] font-medium text-gray-700'>API key</div>
  51. <div
  52. className='mr-1 px-2 leading-6 rounded-md text-xs font-medium text-gray-500 hover:bg-gray-50 cursor-pointer'
  53. onClick={() => onOpenModal(custom?.config)}
  54. >
  55. {t('common.operation.edit')}
  56. </div>
  57. <Selector
  58. onOperate={onOperate}
  59. value={currentProvider?.preferred_provider_type}
  60. hiddenOptions={IS_CE_EDITION}
  61. />
  62. </div>
  63. )
  64. : (
  65. <div
  66. className='inline-flex items-center px-4 h-12 text-gray-500 cursor-pointer hover:text-primary-600'
  67. onClick={() => onOpenModal()}
  68. >
  69. <Plus className='mr-1.5 w-4 h-4'/>
  70. <div className='text-xs font-medium'>{t('common.modelProvider.addApiKey')}</div>
  71. </div>
  72. )
  73. }
  74. </div>
  75. )
  76. }
  77. export default ModelCard