tool-credentials-form.tsx 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import {
  6. RiArrowRightUpLine,
  7. } from '@remixicon/react'
  8. import { addDefaultValue, toolCredentialToFormSchemas } from '@/app/components/tools/utils/to-form-schema'
  9. import type { Collection } from '@/app/components/tools/types'
  10. import Button from '@/app/components/base/button'
  11. import Toast from '@/app/components/base/toast'
  12. import { fetchBuiltInToolCredential, fetchBuiltInToolCredentialSchema } from '@/service/tools'
  13. import Loading from '@/app/components/base/loading'
  14. import Form from '@/app/components/header/account-setting/model-provider-page/model-modal/Form'
  15. import { useRenderI18nObject } from '@/hooks/use-i18n'
  16. import cn from '@/utils/classnames'
  17. type Props = {
  18. collection: Collection
  19. onCancel: () => void
  20. onSaved: (value: Record<string, any>) => void
  21. }
  22. const ToolCredentialForm: FC<Props> = ({
  23. collection,
  24. onCancel,
  25. onSaved,
  26. }) => {
  27. const getValueFromI18nObject = useRenderI18nObject()
  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. const handleSave = () => {
  43. for (const field of credentialSchema) {
  44. if (field.required && !tempCredential[field.name]) {
  45. Toast.notify({ type: 'error', message: t('common.errorMsg.fieldRequired', { field: getValueFromI18nObject(field.label) }) })
  46. return
  47. }
  48. }
  49. onSaved(tempCredential)
  50. }
  51. return (
  52. <>
  53. {!credentialSchema
  54. ? <div className='pt-3'><Loading type='app' /></div>
  55. : (
  56. <>
  57. <div className='px-4 max-h-[464px] overflow-y-auto'>
  58. <Form
  59. value={tempCredential}
  60. onChange={(v) => {
  61. setTempCredential(v)
  62. }}
  63. formSchemas={credentialSchema}
  64. isEditMode={true}
  65. showOnVariableMap={{}}
  66. validating={false}
  67. inputClassName='bg-components-input-bg-normal hover:bg-components-input-bg-hover'
  68. fieldMoreInfo={item => item.url
  69. ? (<a
  70. href={item.url}
  71. target='_blank' rel='noopener noreferrer'
  72. className='inline-flex items-center text-xs text-text-accent'
  73. >
  74. {t('tools.howToGet')}
  75. <RiArrowRightUpLine className='ml-1 w-3 h-3' />
  76. </a>)
  77. : null}
  78. />
  79. </div>
  80. <div className={cn('mt-1 flex justify-end px-4')} >
  81. <div className='flex space-x-2'>
  82. <Button onClick={onCancel}>{t('common.operation.cancel')}</Button>
  83. <Button variant='primary' onClick={handleSave}>{t('common.operation.save')}</Button>
  84. </div>
  85. </div>
  86. </>
  87. )
  88. }
  89. </>
  90. )
  91. }
  92. export default React.memo(ToolCredentialForm)