config-credentials.tsx 4.2 KB

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