popup-item.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import type { FC } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import type {
  4. DefaultModel,
  5. Model,
  6. ModelItem,
  7. } from '../declarations'
  8. import {
  9. useLanguage,
  10. useUpdateModelList,
  11. useUpdateModelProviders,
  12. } from '../hooks'
  13. import ModelIcon from '../model-icon'
  14. import ModelName from '../model-name'
  15. import {
  16. ConfigurateMethodEnum,
  17. MODEL_STATUS_TEXT,
  18. ModelStatusEnum,
  19. } from '../declarations'
  20. import { Check } from '@/app/components/base/icons/src/vender/line/general'
  21. import { useModalContext } from '@/context/modal-context'
  22. import { useProviderContext } from '@/context/provider-context'
  23. import Tooltip from '@/app/components/base/tooltip'
  24. type PopupItemProps = {
  25. defaultModel?: DefaultModel
  26. model: Model
  27. onSelect: (provider: string, model: ModelItem) => void
  28. }
  29. const PopupItem: FC<PopupItemProps> = ({
  30. defaultModel,
  31. model,
  32. onSelect,
  33. }) => {
  34. const { t } = useTranslation()
  35. const language = useLanguage()
  36. const { setShowModelModal } = useModalContext()
  37. const { modelProviders } = useProviderContext()
  38. const updateModelList = useUpdateModelList()
  39. const updateModelProviders = useUpdateModelProviders()
  40. const currentProvider = modelProviders.find(provider => provider.provider === model.provider)!
  41. const handleSelect = (provider: string, modelItem: ModelItem) => {
  42. if (modelItem.status !== ModelStatusEnum.active)
  43. return
  44. onSelect(provider, modelItem)
  45. }
  46. const handleOpenModelModal = () => {
  47. setShowModelModal({
  48. payload: {
  49. currentProvider,
  50. currentConfigurateMethod: ConfigurateMethodEnum.predefinedModel,
  51. },
  52. onSaveCallback: () => {
  53. updateModelProviders()
  54. const modelType = model.models[0].model_type
  55. if (modelType)
  56. updateModelList(modelType)
  57. },
  58. })
  59. }
  60. return (
  61. <div className='mb-1'>
  62. <div className='flex items-center px-3 h-[22px] text-xs font-medium text-gray-500'>
  63. {model.label[language] || model.label.en_US}
  64. </div>
  65. {
  66. model.models.map(modelItem => (
  67. <Tooltip
  68. selector={`${modelItem.model}-${modelItem.status}`}
  69. key={modelItem.model}
  70. content={modelItem.status !== ModelStatusEnum.active ? MODEL_STATUS_TEXT[modelItem.status][language] : undefined}
  71. position='right'
  72. >
  73. <div
  74. key={modelItem.model}
  75. className={`
  76. group relative flex items-center px-3 py-1.5 h-8 rounded-lg
  77. ${modelItem.status === ModelStatusEnum.active ? 'cursor-pointer hover:bg-gray-50' : 'cursor-not-allowed hover:bg-gray-50/60'}
  78. `}
  79. onClick={() => handleSelect(model.provider, modelItem)}
  80. >
  81. <ModelIcon
  82. className={`
  83. shrink-0 mr-2 w-4 h-4
  84. ${modelItem.status !== ModelStatusEnum.active && 'opacity-60'}
  85. `}
  86. provider={model}
  87. modelName={modelItem.model}
  88. />
  89. <ModelName
  90. className={`
  91. grow text-sm font-normal text-gray-900
  92. ${modelItem.status !== ModelStatusEnum.active && 'opacity-60'}
  93. `}
  94. modelItem={modelItem}
  95. showMode
  96. showFeatures
  97. />
  98. {
  99. defaultModel?.model === modelItem.model && defaultModel.provider === currentProvider.provider && (
  100. <Check className='shrink-0 w-4 h-4 text-primary-600' />
  101. )
  102. }
  103. {
  104. modelItem.status === ModelStatusEnum.noConfigure && (
  105. <div
  106. className='hidden group-hover:block text-xs font-medium text-primary-600 cursor-pointer'
  107. onClick={handleOpenModelModal}
  108. >
  109. {t('common.operation.add').toLocaleUpperCase()}
  110. </div>
  111. )
  112. }
  113. </div>
  114. </Tooltip>
  115. ))
  116. }
  117. </div>
  118. )
  119. }
  120. export default PopupItem