manage-custom-model-credentials.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {
  2. memo,
  3. useCallback,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. Button,
  8. } from '@/app/components/base/button'
  9. import type {
  10. CustomConfigurationModelFixedFields,
  11. ModelProvider,
  12. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  13. import {
  14. ConfigurationMethodEnum,
  15. ModelModalModeEnum,
  16. } from '@/app/components/header/account-setting/model-provider-page/declarations'
  17. import Authorized from './authorized'
  18. import {
  19. useCustomModels,
  20. } from './hooks'
  21. import cn from '@/utils/classnames'
  22. type ManageCustomModelCredentialsProps = {
  23. provider: ModelProvider,
  24. currentCustomConfigurationModelFixedFields?: CustomConfigurationModelFixedFields,
  25. }
  26. const ManageCustomModelCredentials = ({
  27. provider,
  28. currentCustomConfigurationModelFixedFields,
  29. }: ManageCustomModelCredentialsProps) => {
  30. const { t } = useTranslation()
  31. const customModels = useCustomModels(provider)
  32. const noModels = !customModels.length
  33. const renderTrigger = useCallback((open?: boolean) => {
  34. const Item = (
  35. <Button
  36. variant='ghost'
  37. size='small'
  38. className={cn(
  39. 'mr-0.5 text-text-tertiary',
  40. open && 'bg-components-button-ghost-bg-hover',
  41. )}
  42. >
  43. {t('common.modelProvider.auth.manageCredentials')}
  44. </Button>
  45. )
  46. return Item
  47. }, [t])
  48. if (noModels)
  49. return null
  50. return (
  51. <Authorized
  52. provider={provider}
  53. configurationMethod={ConfigurationMethodEnum.customizableModel}
  54. currentCustomConfigurationModelFixedFields={currentCustomConfigurationModelFixedFields}
  55. items={customModels.map(model => ({
  56. model,
  57. credentials: model.available_model_credentials ?? [],
  58. selectedCredential: model.current_credential_id ? {
  59. credential_id: model.current_credential_id,
  60. credential_name: model.current_credential_name,
  61. } : undefined,
  62. }))}
  63. renderTrigger={renderTrigger}
  64. authParams={{
  65. isModelCredential: true,
  66. mode: ModelModalModeEnum.configModelCredential,
  67. }}
  68. hideAddAction
  69. disableItemClick
  70. popupTitle={t('common.modelProvider.auth.customModelCredentials')}
  71. showModelTitle
  72. disableDeleteButShowAction
  73. disableDeleteTip={t('common.modelProvider.auth.customModelCredentialsDeleteTip')}
  74. />
  75. )
  76. }
  77. export default memo(ManageCustomModelCredentials)