| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import React, { useState } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { useContext } from 'use-context-selector' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import { RiSettings2Line } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | import ConfigCredentials from './config-credentials' | 
					
						
							| 
									
										
										
										
											2024-02-29 12:58:12 +08:00
										 |  |  | import { AuthType, type Credential, type CustomCollectionBackend, type CustomParamSchema } from '@/app/components/tools/types' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | import Button from '@/app/components/base/button' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | import Input from '@/app/components/base/input' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | import Drawer from '@/app/components/base/drawer-plus' | 
					
						
							|  |  |  | import I18n from '@/context/i18n' | 
					
						
							|  |  |  | import { testAPIAvailable } from '@/service/tools' | 
					
						
							| 
									
										
										
										
											2024-02-23 14:31:06 +08:00
										 |  |  | import { getLanguage } from '@/i18n/language' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type Props = { | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |   positionCenter?: boolean | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |   customCollection: CustomCollectionBackend | 
					
						
							|  |  |  |   tool: CustomParamSchema | 
					
						
							|  |  |  |   onHide: () => void | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const TestApi: FC<Props> = ({ | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |   positionCenter, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |   customCollection, | 
					
						
							|  |  |  |   tool, | 
					
						
							|  |  |  |   onHide, | 
					
						
							|  |  |  | }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const { locale } = useContext(I18n) | 
					
						
							| 
									
										
										
										
											2024-02-23 14:31:06 +08:00
										 |  |  |   const language = getLanguage(locale) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |   const [credentialsModalShow, setCredentialsModalShow] = useState(false) | 
					
						
							|  |  |  |   const [tempCredential, setTempCredential] = React.useState<Credential>(customCollection.credentials) | 
					
						
							|  |  |  |   const [result, setResult] = useState<string>('') | 
					
						
							|  |  |  |   const { operation_id: toolName, parameters } = tool | 
					
						
							|  |  |  |   const [parametersValue, setParametersValue] = useState<Record<string, string>>({}) | 
					
						
							|  |  |  |   const handleTest = async () => { | 
					
						
							| 
									
										
										
										
											2024-02-29 12:58:12 +08:00
										 |  |  |     // clone test schema
 | 
					
						
							|  |  |  |     const credentials = JSON.parse(JSON.stringify(tempCredential)) as Credential | 
					
						
							|  |  |  |     if (credentials.auth_type === AuthType.none) { | 
					
						
							|  |  |  |       delete credentials.api_key_header_prefix | 
					
						
							|  |  |  |       delete credentials.api_key_header | 
					
						
							|  |  |  |       delete credentials.api_key_value | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |     const data = { | 
					
						
							| 
									
										
										
										
											2024-03-04 14:16:47 +08:00
										 |  |  |       provider_name: customCollection.provider, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       tool_name: toolName, | 
					
						
							| 
									
										
										
										
											2024-02-29 14:39:05 +08:00
										 |  |  |       credentials, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       schema_type: customCollection.schema_type, | 
					
						
							|  |  |  |       schema: customCollection.schema, | 
					
						
							|  |  |  |       parameters: parametersValue, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const res = await testAPIAvailable(data) as any | 
					
						
							|  |  |  |     setResult(res.error || res.result) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <> | 
					
						
							|  |  |  |       <Drawer | 
					
						
							|  |  |  |         isShow | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |         positionCenter={positionCenter} | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |         onHide={onHide} | 
					
						
							|  |  |  |         title={`${t('tools.test.title')}  ${toolName}`} | 
					
						
							|  |  |  |         panelClassName='mt-2 !w-[600px]' | 
					
						
							|  |  |  |         maxWidthClassName='!max-w-[600px]' | 
					
						
							|  |  |  |         height='calc(100vh - 16px)' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         headerClassName='!border-b-divider-regular' | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |         body={ | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='overflow-y-auto px-6 pt-2'> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |             <div className='space-y-4'> | 
					
						
							|  |  |  |               <div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <div className='system-sm-medium py-2 text-text-primary'>{t('tools.createTool.authMethod.title')}</div> | 
					
						
							|  |  |  |                 <div className='flex h-9 cursor-pointer items-center justify-between rounded-lg bg-components-input-bg-normal px-2.5' onClick={() => setCredentialsModalShow(true)}> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                   <div className='system-xs-regular text-text-primary'>{t(`tools.createTool.authMethod.types.${tempCredential.auth_type}`)}</div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   <RiSettings2Line className='h-4 w-4 text-text-secondary' /> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                 </div> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               <div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <div className='system-sm-medium py-2 text-text-primary'>{t('tools.test.parametersValue')}</div> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 <div className='rounded-lg border border-divider-regular'> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   <table className='system-xs-regular w-full font-normal text-text-secondary'> | 
					
						
							|  |  |  |                     <thead className='uppercase text-text-tertiary'> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                       <tr className='border-b border-divider-regular'> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                         <th className="p-2 pl-3 font-medium">{t('tools.test.parameters')}</th> | 
					
						
							|  |  |  |                         <th className="p-2 pl-3 font-medium">{t('tools.test.value')}</th> | 
					
						
							|  |  |  |                       </tr> | 
					
						
							|  |  |  |                     </thead> | 
					
						
							|  |  |  |                     <tbody> | 
					
						
							|  |  |  |                       {parameters.map((item, index) => ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                         <tr key={index} className='border-b border-divider-regular last:border-0'> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                           <td className="py-2 pl-3 pr-2.5"> | 
					
						
							| 
									
										
										
										
											2024-01-24 11:08:11 +08:00
										 |  |  |                             {item.label[language]} | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                           </td> | 
					
						
							|  |  |  |                           <td className=""> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                             <Input | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                               value={parametersValue[item.name] || ''} | 
					
						
							|  |  |  |                               onChange={e => setParametersValue({ ...parametersValue, [item.name]: e.target.value })} | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                               type='text' | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                               className='!hover:border-transparent !hover:bg-transparent !focus:border-transparent !focus:bg-transparent !border-transparent !bg-transparent' /> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |                           </td> | 
					
						
							|  |  |  |                         </tr> | 
					
						
							|  |  |  |                       ))} | 
					
						
							|  |  |  |                     </tbody> | 
					
						
							|  |  |  |                   </table> | 
					
						
							|  |  |  |                 </div> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             <Button variant='primary' className=' mt-4 h-10 w-full' onClick={handleTest}>{t('tools.test.title')}</Button> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |             <div className='mt-6'> | 
					
						
							|  |  |  |               <div className='flex items-center space-x-3'> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 <div className='system-xs-semibold text-text-tertiary'>{t('tools.test.testResult')}</div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <div className='bg-[rgb(243, 244, 246)] h-px w-0 grow'></div> | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |               </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               <div className='system-xs-regular mt-2 h-[200px] overflow-y-auto overflow-x-hidden rounded-lg bg-components-input-bg-normal px-3 py-2 text-text-secondary'> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 {result || <span className='text-text-quaternary'>{t('tools.test.testResultPlaceholder')}</span>} | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |               </div> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       /> | 
					
						
							|  |  |  |       {credentialsModalShow && ( | 
					
						
							|  |  |  |         <ConfigCredentials | 
					
						
							| 
									
										
										
										
											2024-05-27 21:57:08 +08:00
										 |  |  |           positionCenter={positionCenter} | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |           credential={tempCredential} | 
					
						
							|  |  |  |           onChange={setTempCredential} | 
					
						
							|  |  |  |           onHide={() => setCredentialsModalShow(false)} | 
					
						
							|  |  |  |         />) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     </> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export default React.memo(TestApi) |