config-credentials.tsx 4.1 KB

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