config-credentials.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import cn from 'classnames'
  6. import { addDefaultValue, toolCredentialToFormSchemas } from '../../utils/to-form-schema'
  7. import type { Collection } from '../../types'
  8. import Drawer from '@/app/components/base/drawer-plus'
  9. import Button from '@/app/components/base/button'
  10. import { fetchBuiltInToolCredentialSchema } from '@/service/tools'
  11. import Loading from '@/app/components/base/loading'
  12. import Form from '@/app/components/header/account-setting/model-provider-page/model-modal/Form'
  13. import { LinkExternal02 } from '@/app/components/base/icons/src/vender/line/general'
  14. type Props = {
  15. collection: Collection
  16. onCancel: () => void
  17. onSaved: (value: Record<string, any>) => void
  18. onRemove: () => void
  19. }
  20. const ConfigCredential: FC<Props> = ({
  21. collection,
  22. onCancel,
  23. onSaved,
  24. onRemove,
  25. }) => {
  26. const { t } = useTranslation()
  27. const [credentialSchema, setCredentialSchema] = useState<any>(null)
  28. const { team_credentials: credentialValue, name: collectionName } = collection
  29. const [tempCredential, setTempCredential] = React.useState<any>(credentialValue)
  30. useEffect(() => {
  31. fetchBuiltInToolCredentialSchema(collectionName).then((res) => {
  32. const toolCredentialSchemas = toolCredentialToFormSchemas(res)
  33. const defaultCredentials = addDefaultValue(credentialValue, toolCredentialSchemas)
  34. setCredentialSchema(toolCredentialSchemas)
  35. setTempCredential(defaultCredentials)
  36. })
  37. }, [])
  38. return (
  39. <Drawer
  40. isShow
  41. onHide={onCancel}
  42. title={t('tools.auth.setupModalTitle') as string}
  43. titleDescription={t('tools.auth.setupModalTitleDescription') as string}
  44. panelClassName='mt-2 !w-[480px]'
  45. maxWidthClassName='!max-w-[480px]'
  46. height='calc(100vh - 16px)'
  47. contentClassName='!bg-gray-100'
  48. headerClassName='!border-b-black/5'
  49. body={
  50. <div className='px-6 py-3 h-full'>
  51. {!credentialSchema
  52. ? <Loading type='app' />
  53. : (
  54. <>
  55. <Form
  56. value={tempCredential}
  57. onChange={(v) => {
  58. setTempCredential(v)
  59. }}
  60. formSchemas={credentialSchema}
  61. isEditMode={true}
  62. showOnVariableMap={{}}
  63. validating={false}
  64. inputClassName='!bg-gray-50'
  65. fieldMoreInfo={item => item.url
  66. ? (<a
  67. href={item.url}
  68. target='_blank' rel='noopener noreferrer'
  69. className='inline-flex items-center text-xs text-primary-600'
  70. >
  71. {t('tools.howToGet')}
  72. <LinkExternal02 className='ml-1 w-3 h-3' />
  73. </a>)
  74. : null}
  75. />
  76. <div className={cn(collection.is_team_authorization ? 'justify-between' : 'justify-end', 'mt-2 flex ')} >
  77. {
  78. collection.is_team_authorization && (
  79. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onRemove}>{t('common.operation.remove')}</Button>
  80. )
  81. }
  82. < div className='flex space-x-2'>
  83. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onCancel}>{t('common.operation.cancel')}</Button>
  84. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium' type='primary' onClick={() => onSaved(tempCredential)}>{t('common.operation.save')}</Button>
  85. </div>
  86. </div>
  87. </>
  88. )
  89. }
  90. </div >
  91. }
  92. isShowMask={true}
  93. clickOutsideNotOpen={false}
  94. />
  95. )
  96. }
  97. export default React.memo(ConfigCredential)