config-credentials.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. type Props = {
  17. collection: Collection
  18. onCancel: () => void
  19. onSaved: (value: Record<string, any>) => void
  20. isHideRemoveBtn?: boolean
  21. onRemove?: () => void
  22. }
  23. const ConfigCredential: FC<Props> = ({
  24. collection,
  25. onCancel,
  26. onSaved,
  27. isHideRemoveBtn,
  28. onRemove = () => { },
  29. }) => {
  30. const { t } = useTranslation()
  31. const language = useLanguage()
  32. const [credentialSchema, setCredentialSchema] = useState<any>(null)
  33. const { name: collectionName } = collection
  34. const [tempCredential, setTempCredential] = React.useState<any>({})
  35. useEffect(() => {
  36. fetchBuiltInToolCredentialSchema(collectionName).then(async (res) => {
  37. const toolCredentialSchemas = toolCredentialToFormSchemas(res)
  38. const credentialValue = await fetchBuiltInToolCredential(collectionName)
  39. setTempCredential(credentialValue)
  40. const defaultCredentials = addDefaultValue(credentialValue, toolCredentialSchemas)
  41. setCredentialSchema(toolCredentialSchemas)
  42. setTempCredential(defaultCredentials)
  43. })
  44. }, [])
  45. const handleSave = () => {
  46. for (const field of credentialSchema) {
  47. if (field.required && !tempCredential[field.name]) {
  48. Toast.notify({ type: 'error', message: t('common.errorMsg.fieldRequired', { field: field.label[language] || field.label.en_US }) })
  49. return
  50. }
  51. }
  52. onSaved(tempCredential)
  53. }
  54. return (
  55. <Drawer
  56. isShow
  57. onHide={onCancel}
  58. title={t('tools.auth.setupModalTitle') as string}
  59. titleDescription={t('tools.auth.setupModalTitleDescription') as string}
  60. panelClassName='mt-2 !w-[405px]'
  61. maxWidthClassName='!max-w-[405px]'
  62. height='calc(100vh - 16px)'
  63. contentClassName='!bg-gray-100'
  64. headerClassName='!border-b-black/5'
  65. body={
  66. <div className='px-6 py-3 h-full'>
  67. {!credentialSchema
  68. ? <Loading type='app' />
  69. : (
  70. <>
  71. <Form
  72. value={tempCredential}
  73. onChange={(v) => {
  74. setTempCredential(v)
  75. }}
  76. formSchemas={credentialSchema}
  77. isEditMode={true}
  78. showOnVariableMap={{}}
  79. validating={false}
  80. inputClassName='!bg-gray-50'
  81. fieldMoreInfo={item => item.url
  82. ? (<a
  83. href={item.url}
  84. target='_blank' rel='noopener noreferrer'
  85. className='inline-flex items-center text-xs text-primary-600'
  86. >
  87. {t('tools.howToGet')}
  88. <LinkExternal02 className='ml-1 w-3 h-3' />
  89. </a>)
  90. : null}
  91. />
  92. <div className={cn((collection.is_team_authorization && !isHideRemoveBtn) ? 'justify-between' : 'justify-end', 'mt-2 flex ')} >
  93. {
  94. (collection.is_team_authorization && !isHideRemoveBtn) && (
  95. <Button onClick={onRemove}>{t('common.operation.remove')}</Button>
  96. )
  97. }
  98. < div className='flex space-x-2'>
  99. <Button onClick={onCancel}>{t('common.operation.cancel')}</Button>
  100. <Button variant='primary' onClick={handleSave}>{t('common.operation.save')}</Button>
  101. </div>
  102. </div>
  103. </>
  104. )
  105. }
  106. </div >
  107. }
  108. isShowMask={true}
  109. clickOutsideNotOpen={false}
  110. />
  111. )
  112. }
  113. export default React.memo(ConfigCredential)