You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config-credentials.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { addDefaultValue, toolCredentialToFormSchemas } from '../../utils/to-form-schema'
  6. import type { Collection } from '../../types'
  7. import cn from '@/utils/classnames'
  8. import Drawer from '@/app/components/base/drawer-plus'
  9. import Button from '@/app/components/base/button'
  10. import Toast from '@/app/components/base/toast'
  11. import { fetchBuiltInToolCredential, fetchBuiltInToolCredentialSchema } from '@/service/tools'
  12. import Loading from '@/app/components/base/loading'
  13. import Form from '@/app/components/header/account-setting/model-provider-page/model-modal/Form'
  14. import { LinkExternal02 } from '@/app/components/base/icons/src/vender/line/general'
  15. import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
  16. import { noop } from 'lodash-es'
  17. type Props = {
  18. collection: Collection
  19. onCancel: () => void
  20. onSaved: (value: Record<string, any>) => void
  21. isHideRemoveBtn?: boolean
  22. onRemove?: () => void
  23. isSaving?: boolean
  24. }
  25. const ConfigCredential: FC<Props> = ({
  26. collection,
  27. onCancel,
  28. onSaved,
  29. isHideRemoveBtn,
  30. onRemove = noop,
  31. isSaving,
  32. }) => {
  33. const { t } = useTranslation()
  34. const language = useLanguage()
  35. const [credentialSchema, setCredentialSchema] = useState<any>(null)
  36. const { name: collectionName } = collection
  37. const [tempCredential, setTempCredential] = React.useState<any>({})
  38. const [isLoading, setIsLoading] = React.useState(false)
  39. useEffect(() => {
  40. fetchBuiltInToolCredentialSchema(collectionName).then(async (res) => {
  41. const toolCredentialSchemas = toolCredentialToFormSchemas(res)
  42. const credentialValue = await fetchBuiltInToolCredential(collectionName)
  43. setTempCredential(credentialValue)
  44. const defaultCredentials = addDefaultValue(credentialValue, toolCredentialSchemas)
  45. setCredentialSchema(toolCredentialSchemas)
  46. setTempCredential(defaultCredentials)
  47. })
  48. }, [])
  49. const handleSave = async () => {
  50. for (const field of credentialSchema) {
  51. if (field.required && !tempCredential[field.name]) {
  52. Toast.notify({ type: 'error', message: t('common.errorMsg.fieldRequired', { field: field.label[language] || field.label.en_US }) })
  53. return
  54. }
  55. }
  56. setIsLoading(true)
  57. try {
  58. await onSaved(tempCredential)
  59. setIsLoading(false)
  60. }
  61. finally {
  62. setIsLoading(false)
  63. }
  64. }
  65. return (
  66. <Drawer
  67. isShow
  68. onHide={onCancel}
  69. title={t('tools.auth.setupModalTitle') as string}
  70. titleDescription={t('tools.auth.setupModalTitleDescription') as string}
  71. panelClassName='mt-[64px] mb-2 !w-[420px] border-components-panel-border'
  72. maxWidthClassName='!max-w-[420px]'
  73. height='calc(100vh - 64px)'
  74. contentClassName='!bg-components-panel-bg'
  75. headerClassName='!border-b-divider-subtle'
  76. body={
  77. <div className='h-full px-6 py-3'>
  78. {!credentialSchema
  79. ? <Loading type='app' />
  80. : (
  81. <>
  82. <Form
  83. value={tempCredential}
  84. onChange={(v) => {
  85. setTempCredential(v)
  86. }}
  87. formSchemas={credentialSchema}
  88. isEditMode={true}
  89. showOnVariableMap={{}}
  90. validating={false}
  91. inputClassName='!bg-components-input-bg-normal'
  92. fieldMoreInfo={item => item.url
  93. ? (<a
  94. href={item.url}
  95. target='_blank' rel='noopener noreferrer'
  96. className='inline-flex items-center text-xs text-text-accent'
  97. >
  98. {t('tools.howToGet')}
  99. <LinkExternal02 className='ml-1 h-3 w-3' />
  100. </a>)
  101. : null}
  102. />
  103. <div className={cn((collection.is_team_authorization && !isHideRemoveBtn) ? 'justify-between' : 'justify-end', 'mt-2 flex ')} >
  104. {
  105. (collection.is_team_authorization && !isHideRemoveBtn) && (
  106. <Button onClick={onRemove}>{t('common.operation.remove')}</Button>
  107. )
  108. }
  109. <div className='flex space-x-2'>
  110. <Button onClick={onCancel}>{t('common.operation.cancel')}</Button>
  111. <Button loading={isLoading || isSaving} disabled={isLoading || isSaving} variant='primary' onClick={handleSave}>{t('common.operation.save')}</Button>
  112. </div>
  113. </div>
  114. </>
  115. )
  116. }
  117. </div >
  118. }
  119. isShowMask={true}
  120. clickOutsideNotOpen={false}
  121. />
  122. )
  123. }
  124. export default React.memo(ConfigCredential)