'use client' import type { FC } from 'react' import React, { useState } from 'react' import { useTranslation } from 'react-i18next' import { useContext } from 'use-context-selector' import { Settings01 } from '../../base/icons/src/vender/line/general' import ConfigCredentials from './config-credentials' import { AuthType, type Credential, type CustomCollectionBackend, type CustomParamSchema } from '@/app/components/tools/types' import Button from '@/app/components/base/button' import Drawer from '@/app/components/base/drawer-plus' import I18n from '@/context/i18n' import { testAPIAvailable } from '@/service/tools' import { getLanguage } from '@/i18n/language' type Props = { positionCenter?: boolean customCollection: CustomCollectionBackend tool: CustomParamSchema onHide: () => void } const keyClassNames = 'py-2 leading-5 text-sm font-medium text-gray-900' const TestApi: FC = ({ positionCenter, customCollection, tool, onHide, }) => { const { t } = useTranslation() const { locale } = useContext(I18n) const language = getLanguage(locale) const [credentialsModalShow, setCredentialsModalShow] = useState(false) const [tempCredential, setTempCredential] = React.useState(customCollection.credentials) const [result, setResult] = useState('') const { operation_id: toolName, parameters } = tool const [parametersValue, setParametersValue] = useState>({}) const handleTest = async () => { // 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 } const data = { provider_name: customCollection.provider, tool_name: toolName, credentials, schema_type: customCollection.schema_type, schema: customCollection.schema, parameters: parametersValue, } const res = await testAPIAvailable(data) as any setResult(res.error || res.result) } return ( <>
{t('tools.createTool.authMethod.title')}
setCredentialsModalShow(true)}>
{t(`tools.createTool.authMethod.types.${tempCredential.auth_type}`)}
{t('tools.test.parametersValue')}
{parameters.map((item, index) => ( ))}
{t('tools.test.parameters')} {t('tools.test.value')}
{item.label[language]} setParametersValue({ ...parametersValue, [item.name]: e.target.value })} type='text' className='px-3 h-[34px] w-full outline-none focus:bg-gray-100' >
{t('tools.test.testResult')}
{result || {t('tools.test.testResultPlaceholder')}}
} /> {credentialsModalShow && ( setCredentialsModalShow(false)} />) } ) } export default React.memo(TestApi)