test-api.tsx 5.1 KB

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