test-api.tsx 5.2 KB

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