config-credentials.tsx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. type Props = {
  14. collection: Collection
  15. onCancel: () => void
  16. onSaved: (value: Record<string, any>) => void
  17. onRemove: () => void
  18. }
  19. const ConfigCredential: FC<Props> = ({
  20. collection,
  21. onCancel,
  22. onSaved,
  23. onRemove,
  24. }) => {
  25. const { t } = useTranslation()
  26. const [credentialSchema, setCredentialSchema] = useState<any>(null)
  27. const { team_credentials: credentialValue, name: collectionName } = collection
  28. useEffect(() => {
  29. fetchBuiltInToolCredentialSchema(collectionName).then((res) => {
  30. setCredentialSchema(toolCredentialToFormSchemas(res))
  31. })
  32. }, [])
  33. const [tempCredential, setTempCredential] = React.useState<any>(credentialValue)
  34. return (
  35. <Drawer
  36. isShow
  37. onHide={onCancel}
  38. title={t('tools.auth.setupModalTitle') as string}
  39. titleDescription={t('tools.auth.setupModalTitleDescription') as string}
  40. panelClassName='mt-2 !w-[480px]'
  41. maxWidthClassName='!max-w-[480px]'
  42. height='calc(100vh - 16px)'
  43. contentClassName='!bg-gray-100'
  44. headerClassName='!border-b-black/5'
  45. body={
  46. <div className='px-6 py-3 h-full'>
  47. {!credentialSchema
  48. ? <Loading type='app' />
  49. : (
  50. <>
  51. <Form
  52. value={tempCredential}
  53. onChange={(v) => {
  54. setTempCredential(v)
  55. }}
  56. formSchemas={credentialSchema}
  57. isEditMode={true}
  58. showOnVariableMap={{}}
  59. validating={false}
  60. inputClassName='!bg-gray-50'
  61. />
  62. <div className={cn(collection.is_team_authorization ? 'justify-between' : 'justify-end', 'mt-2 flex ')} >
  63. {
  64. collection.is_team_authorization && (
  65. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onRemove}>{t('common.operation.remove')}</Button>
  66. )
  67. }
  68. < div className='flex space-x-2'>
  69. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onCancel}>{t('common.operation.cancel')}</Button>
  70. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium' type='primary' onClick={() => onSaved(tempCredential)}>{t('common.operation.save')}</Button>
  71. </div>
  72. </div>
  73. </>
  74. )
  75. }
  76. </div >
  77. }
  78. isShowMask={true}
  79. clickOutsideNotOpen={false}
  80. />
  81. )
  82. }
  83. export default React.memo(ConfigCredential)