credential-panel.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type { FC } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useSWRConfig } from 'swr'
  4. import type { ModelProvider } from '../declarations'
  5. import {
  6. CustomConfigurationStatusEnum,
  7. PreferredProviderTypeEnum,
  8. } from '../declarations'
  9. import { useUpdateModelList } from '../hooks'
  10. import PrioritySelector from './priority-selector'
  11. import PriorityUseTip from './priority-use-tip'
  12. import Indicator from '@/app/components/header/indicator'
  13. import { Settings01 } from '@/app/components/base/icons/src/vender/line/general'
  14. import Button from '@/app/components/base/button'
  15. import { changeModelProviderPriority } from '@/service/common'
  16. import { useToastContext } from '@/app/components/base/toast'
  17. type CredentialPanelProps = {
  18. provider: ModelProvider
  19. onSetup: () => void
  20. }
  21. const CredentialPanel: FC<CredentialPanelProps> = ({
  22. provider,
  23. onSetup,
  24. }) => {
  25. const { t } = useTranslation()
  26. const { notify } = useToastContext()
  27. const { mutate } = useSWRConfig()
  28. const updateModelList = useUpdateModelList()
  29. const customConfig = provider.custom_configuration
  30. const systemConfig = provider.system_configuration
  31. const priorityUseType = provider.preferred_provider_type
  32. const customConfiged = customConfig.status === CustomConfigurationStatusEnum.active
  33. const handleChangePriority = async (key: PreferredProviderTypeEnum) => {
  34. const res = await changeModelProviderPriority({
  35. url: `/workspaces/current/model-providers/${provider.provider}/preferred-provider-type`,
  36. body: {
  37. preferred_provider_type: key,
  38. },
  39. })
  40. if (res.result === 'success') {
  41. notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') })
  42. mutate('/workspaces/current/model-providers')
  43. updateModelList(1)
  44. }
  45. }
  46. return (
  47. <div className='shrink-0 relative ml-1 p-1 w-[112px] rounded-lg bg-white/[0.3] border-[0.5px] border-black/5'>
  48. <div className='flex items-center justify-between mb-1 pt-1 pl-2 pr-[7px] h-5 text-xs font-medium text-gray-500'>
  49. API-KEY
  50. <Indicator color={customConfiged ? 'green' : 'gray'} />
  51. </div>
  52. <div className='flex items-center gap-0.5'>
  53. <Button
  54. className='grow px-0 h-6 bg-white text-xs font-medium rounded-md'
  55. onClick={onSetup}
  56. >
  57. <Settings01 className='mr-1 w-3 h-3' />
  58. {t('common.operation.setup')}
  59. </Button>
  60. {
  61. systemConfig.enabled && customConfiged && (
  62. <PrioritySelector
  63. value={priorityUseType}
  64. onSelect={handleChangePriority}
  65. />
  66. )
  67. }
  68. </div>
  69. {
  70. priorityUseType === PreferredProviderTypeEnum.custom && systemConfig.enabled && (
  71. <PriorityUseTip />
  72. )
  73. }
  74. </div>
  75. )
  76. }
  77. export default CredentialPanel