test-api.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useContext } from 'use-context-selector'
  6. import { Settings01 } from '../../base/icons/src/vender/line/general'
  7. import ConfigCredentials from './config-credentials'
  8. import { AuthType, type Credential, type CustomCollectionBackend, type CustomParamSchema } from '@/app/components/tools/types'
  9. import Button from '@/app/components/base/button'
  10. import Drawer from '@/app/components/base/drawer-plus'
  11. import I18n from '@/context/i18n'
  12. import { testAPIAvailable } from '@/service/tools'
  13. import { getLanguage } from '@/i18n/language'
  14. type Props = {
  15. customCollection: CustomCollectionBackend
  16. tool: CustomParamSchema
  17. onHide: () => void
  18. }
  19. const keyClassNames = 'py-2 leading-5 text-sm font-medium text-gray-900'
  20. const TestApi: FC<Props> = ({
  21. customCollection,
  22. tool,
  23. onHide,
  24. }) => {
  25. const { t } = useTranslation()
  26. const { locale } = useContext(I18n)
  27. const language = getLanguage(locale)
  28. const [credentialsModalShow, setCredentialsModalShow] = useState(false)
  29. const [tempCredential, setTempCredential] = React.useState<Credential>(customCollection.credentials)
  30. const [result, setResult] = useState<string>('')
  31. const { operation_id: toolName, parameters } = tool
  32. const [parametersValue, setParametersValue] = useState<Record<string, string>>({})
  33. const handleTest = async () => {
  34. // clone test schema
  35. const credentials = JSON.parse(JSON.stringify(tempCredential)) as Credential
  36. if (credentials.auth_type === AuthType.none) {
  37. delete credentials.api_key_header_prefix
  38. delete credentials.api_key_header
  39. delete credentials.api_key_value
  40. }
  41. const data = {
  42. provider_name: customCollection.provider,
  43. tool_name: toolName,
  44. credentials,
  45. schema_type: customCollection.schema_type,
  46. schema: customCollection.schema,
  47. parameters: parametersValue,
  48. }
  49. const res = await testAPIAvailable(data) as any
  50. setResult(res.error || res.result)
  51. }
  52. return (
  53. <>
  54. <Drawer
  55. isShow
  56. onHide={onHide}
  57. title={`${t('tools.test.title')} ${toolName}`}
  58. panelClassName='mt-2 !w-[600px]'
  59. maxWidthClassName='!max-w-[600px]'
  60. height='calc(100vh - 16px)'
  61. headerClassName='!border-b-black/5'
  62. body={
  63. <div className='pt-2 px-6 overflow-y-auto'>
  64. <div className='space-y-4'>
  65. <div>
  66. <div className={keyClassNames}>{t('tools.createTool.authMethod.title')}</div>
  67. <div className='flex items-center h-9 justify-between px-2.5 bg-gray-100 rounded-lg cursor-pointer' onClick={() => setCredentialsModalShow(true)}>
  68. <div className='text-sm font-normal text-gray-900'>{t(`tools.createTool.authMethod.types.${tempCredential.auth_type}`)}</div>
  69. <Settings01 className='w-4 h-4 text-gray-700 opacity-60' />
  70. </div>
  71. </div>
  72. <div>
  73. <div className={keyClassNames}>{t('tools.test.parametersValue')}</div>
  74. <div className='rounded-lg border border-gray-200'>
  75. <table className='w-full leading-[18px] text-xs text-gray-700 font-normal'>
  76. <thead className='text-gray-500 uppercase'>
  77. <tr className='border-b border-gray-200'>
  78. <th className="p-2 pl-3 font-medium">{t('tools.test.parameters')}</th>
  79. <th className="p-2 pl-3 font-medium">{t('tools.test.value')}</th>
  80. </tr>
  81. </thead>
  82. <tbody>
  83. {parameters.map((item, index) => (
  84. <tr key={index} className='border-b last:border-0 border-gray-200'>
  85. <td className="py-2 pl-3 pr-2.5">
  86. {item.label[language]}
  87. </td>
  88. <td className="">
  89. <input
  90. value={parametersValue[item.name] || ''}
  91. onChange={e => setParametersValue({ ...parametersValue, [item.name]: e.target.value })}
  92. type='text' className='px-3 h-[34px] w-full outline-none focus:bg-gray-100' ></input>
  93. </td>
  94. </tr>
  95. ))}
  96. </tbody>
  97. </table>
  98. </div>
  99. </div>
  100. </div>
  101. <Button type='primary' className=' mt-4 w-full h-10 !text-[13px] leading-[18px] font-medium' onClick={handleTest}>{t('tools.test.title')}</Button>
  102. <div className='mt-6'>
  103. <div className='flex items-center space-x-3'>
  104. <div className='leading-[18px] text-xs font-semibold text-gray-500'>{t('tools.test.testResult')}</div>
  105. <div className='grow w-0 h-px bg-[rgb(243, 244, 246)]'></div>
  106. </div>
  107. <div className='mt-2 px-3 py-2 h-[200px] overflow-y-auto overflow-x-hidden rounded-lg bg-gray-100 leading-4 text-xs font-normal text-gray-700'>
  108. {result || <span className='text-gray-400'>{t('tools.test.testResultPlaceholder')}</span>}
  109. </div>
  110. </div>
  111. </div>
  112. }
  113. />
  114. {credentialsModalShow && (
  115. <ConfigCredentials
  116. credential={tempCredential}
  117. onChange={setTempCredential}
  118. onHide={() => setCredentialsModalShow(false)}
  119. />)
  120. }
  121. </>
  122. )
  123. }
  124. export default React.memo(TestApi)