test-api.tsx 5.6 KB

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