config-credentials.tsx 3.7 KB

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