popup-item.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. ConfigurationMethodEnum,
  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. currentConfigurationMethod: ConfigurationMethodEnum.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. key={modelItem.model}
  69. popupContent={modelItem.status !== ModelStatusEnum.active ? MODEL_STATUS_TEXT[modelItem.status][language] : undefined}
  70. position='right'
  71. >
  72. <div
  73. key={modelItem.model}
  74. className={`
  75. group relative flex items-center px-3 py-1.5 h-8 rounded-lg
  76. ${modelItem.status === ModelStatusEnum.active ? 'cursor-pointer hover:bg-gray-50' : 'cursor-not-allowed hover:bg-gray-50/60'}
  77. `}
  78. onClick={() => handleSelect(model.provider, modelItem)}
  79. >
  80. <ModelIcon
  81. className={`
  82. shrink-0 mr-2 w-4 h-4
  83. ${modelItem.status !== ModelStatusEnum.active && 'opacity-60'}
  84. `}
  85. provider={model}
  86. modelName={modelItem.model}
  87. />
  88. <ModelName
  89. className={`
  90. grow text-sm font-normal text-gray-900
  91. ${modelItem.status !== ModelStatusEnum.active && 'opacity-60'}
  92. `}
  93. modelItem={modelItem}
  94. showMode
  95. showFeatures
  96. />
  97. {
  98. defaultModel?.model === modelItem.model && defaultModel.provider === currentProvider.provider && (
  99. <Check className='shrink-0 w-4 h-4 text-primary-600' />
  100. )
  101. }
  102. {
  103. modelItem.status === ModelStatusEnum.noConfigure && (
  104. <div
  105. className='hidden group-hover:block text-xs font-medium text-primary-600 cursor-pointer'
  106. onClick={handleOpenModelModal}
  107. >
  108. {t('common.operation.add').toLocaleUpperCase()}
  109. </div>
  110. )
  111. }
  112. </div>
  113. </Tooltip>
  114. ))
  115. }
  116. </div>
  117. )
  118. }
  119. export default PopupItem