test-api.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. tool_name: toolName,
  43. credentials: tempCredential,
  44. schema_type: customCollection.schema_type,
  45. schema: customCollection.schema,
  46. parameters: parametersValue,
  47. }
  48. const res = await testAPIAvailable(data) as any
  49. setResult(res.error || res.result)
  50. }
  51. return (
  52. <>
  53. <Drawer
  54. isShow
  55. onHide={onHide}
  56. title={`${t('tools.test.title')} ${toolName}`}
  57. panelClassName='mt-2 !w-[600px]'
  58. maxWidthClassName='!max-w-[600px]'
  59. height='calc(100vh - 16px)'
  60. headerClassName='!border-b-black/5'
  61. body={
  62. <div className='pt-2 px-6 overflow-y-auto'>
  63. <div className='space-y-4'>
  64. <div>
  65. <div className={keyClassNames}>{t('tools.createTool.authMethod.title')}</div>
  66. <div className='flex items-center h-9 justify-between px-2.5 bg-gray-100 rounded-lg cursor-pointer' onClick={() => setCredentialsModalShow(true)}>
  67. <div className='text-sm font-normal text-gray-900'>{t(`tools.createTool.authMethod.types.${tempCredential.auth_type}`)}</div>
  68. <Settings01 className='w-4 h-4 text-gray-700 opacity-60' />
  69. </div>
  70. </div>
  71. <div>
  72. <div className={keyClassNames}>{t('tools.test.parametersValue')}</div>
  73. <div className='rounded-lg border border-gray-200'>
  74. <table className='w-full leading-[18px] text-xs text-gray-700 font-normal'>
  75. <thead className='text-gray-500 uppercase'>
  76. <tr className='border-b border-gray-200'>
  77. <th className="p-2 pl-3 font-medium">{t('tools.test.parameters')}</th>
  78. <th className="p-2 pl-3 font-medium">{t('tools.test.value')}</th>
  79. </tr>
  80. </thead>
  81. <tbody>
  82. {parameters.map((item, index) => (
  83. <tr key={index} className='border-b last:border-0 border-gray-200'>
  84. <td className="py-2 pl-3 pr-2.5">
  85. {item.label[language]}
  86. </td>
  87. <td className="">
  88. <input
  89. value={parametersValue[item.name] || ''}
  90. onChange={e => setParametersValue({ ...parametersValue, [item.name]: e.target.value })}
  91. type='text' className='px-3 h-[34px] w-full outline-none focus:bg-gray-100' ></input>
  92. </td>
  93. </tr>
  94. ))}
  95. </tbody>
  96. </table>
  97. </div>
  98. </div>
  99. </div>
  100. <Button type='primary' className=' mt-4 w-full h-10 !text-[13px] leading-[18px] font-medium' onClick={handleTest}>{t('tools.test.title')}</Button>
  101. <div className='mt-6'>
  102. <div className='flex items-center space-x-3'>
  103. <div className='leading-[18px] text-xs font-semibold text-gray-500'>{t('tools.test.testResult')}</div>
  104. <div className='grow w-0 h-px bg-[rgb(243, 244, 246)]'></div>
  105. </div>
  106. <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'>
  107. {result || <span className='text-gray-400'>{t('tools.test.testResultPlaceholder')}</span>}
  108. </div>
  109. </div>
  110. </div>
  111. }
  112. />
  113. {credentialsModalShow && (
  114. <ConfigCredentials
  115. credential={tempCredential}
  116. onChange={setTempCredential}
  117. onHide={() => setCredentialsModalShow(false)}
  118. />)
  119. }
  120. </>
  121. )
  122. }
  123. export default React.memo(TestApi)